"Segmentation errors in linux " / 5 errors in Visual Studio 2015= Program : class matrix input , output and addition

Hi , the program errors is coming in Visual Studio 2015 in Windows 10 . I tried linux where it says "Segmentation fault" . The list of errors is mentioned first . Then the Header.h , Then Header.cpp and finally Source.cpp .

[ERRORS]


Severity Code Description Project File Line Suppression State
Error LNK2005 "class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > __cdecl definematrix(int,int)" (?definematrix@@YA?AV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@HH@Z) already defined in Header.obj Project10 C:\Users\chica\Documents\Visual Studio 2015\Projects\Project10\Project10\Source.obj 1
Error LNK2005 "class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > __cdecl defmatrix(class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > >,class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > >)" (?defmatrix@@YA?AV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@V12@0@Z) already defined in Header.obj Project10 C:\Users\chica\Documents\Visual Studio 2015\Projects\Project10\Project10\Source.obj 1
Error LNK2005 "public: class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > __thiscall matrix::matrixA(class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > &,class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > &)" (?matrixA@matrix@@QAE?AV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@AAV23@0@Z) already defined in Header.obj Project10 C:\Users\chica\Documents\Visual Studio 2015\Projects\Project10\Project10\Source.obj 1
Error LNK2019 unresolved external symbol "public: class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > __thiscall matrix::definematrix(int,int)" (?definematrix@matrix@@QAE?AV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@HH@Z) referenced in function _main Project10 C:\Users\chica\Documents\Visual Studio 2015\Projects\Project10\Project10\Source.obj 1
Error LNK1120 1 unresolved externals Project10 C:\Users\chica\Documents\Visual Studio 2015\Projects\Project10\Debug\Project10.exe 1


[Header. H]

#pragma once
#include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
class matrix {

public:

int definematrixrow;
int definematrixcolumn;

vector<vector<int>> matrix_shishir(int definematrixrow, int definematrixcolumn);
vector<vector<int>> matrix_shi(int definematrixrow, int definematrixcolumn);
vector<vector<int>> matrix_x(vector<vector<int>> matrix3);
vector<vector<int>> matrix_y(vector<vector<int>> &matrix4);

vector<vector<int>> defmatrix(vector<vector<int>> matrix1, vector<vector<int>>matrix2);

vector<vector<int>> definematrix(int definematrixrow, int definematrixcolumn);

vector<vector<int>> definematrixM(vector<vector<int>>&matrix1, vector<vector<int>>&matrix2);

vector<vector<int>> matrixS(vector<vector<int>>&matrix1, vector<vector<int>>&matrix2);


vector<vector<int>> matrixA(vector<vector<int>>&matrix1, vector<vector<int>>&matrix2);


vector<vector<int>> matrixT(vector<vector<int>> &matrix5);
void delmat(vector<vector<int>>* matrix_shishir)
{
delete[] matrix_shishir;
matrix_shishir = NULL;
}

void setRow(int rows)
{

matrixrow = rows;
}

void matrix::setColumn(int x)
{
matrixcolumn = x;

}
int getColumn(vector<vector<int>> matrix1)
{
int a;

a = matrix1[0].size();
return a;
}

int getRow(vector<vector<int>> matrix1)

{

int b = matrix1.size();
return b;
}

void setdefinematrix(vector<vector<int>> matrix4)
{
int a = matrix4[0].size();
int b = matrix4.size();

matrix4.resize(a);
for (int i = 0;i < b;i++)
matrix4[i].resize(b);

}



private:
int matrixrow;
int matrixcolumn;
vector<vector<int>> matrix_var;
};


[Header.cpp]

#pragma once
#include"Header.h"

/* fixing up size of matrix */

vector<vector<int>> defmatrix(vector<vector<int>> matrix1, vector<vector<int>> matrix2)
{
return vector<vector<int>>();
}


vector<vector<int>> definematrix(int definematrixrow, int definematrixcolumn)
{
int matrixcolumn = definematrixcolumn;
int matrixrow = definematrixrow;
vector<std::vector<int>> matrix_shi(definematrixrow, std::vector<int>(definematrixcolumn));
matrix_shi.resize(definematrixrow);
for (int i = 0;i < definematrixcolumn;i++)
matrix_shi[i].resize(definematrixcolumn);
return matrix_shi;
}
//matrix addition
vector<vector<int>> matrix::matrixA(vector<vector<int>>&matrix1, vector<vector<int>>&matrix2)
{
vector<vector<int>>matrix5;
int a = matrix1[0].size();
int b = matrix2[0].size();

if (a > b || a == b)
{
matrixA(matrix1, matrix2).resize(a);
}
else
{
matrixA(matrix1, matrix2).resize(b);
}

int c = matrix1.size();
int d = matrix2.size();
if (c > d || c == d)
{
matrixA(matrix1, matrix2).resize(c);
}
else
{
matrixA(matrix1, matrix2).resize(d);
}

for (int i = 0;i< matrixA(matrix1, matrix2).size();i++)
for (int j = 0;j < matrixA(matrix1, matrix2)[0].size();j++)
{
matrix5[i][j] = matrix1[i][j] + matrix2[i][j];
}
matrixA(matrix1, matrix2) = matrix5;
return matrixA(matrix1, matrix2);
};

[SOURCE.CPP]

#include "Header.h"
#include"Header.cpp"

#include<sstream>
#include<stdlib.h>
#include<conio.h>
#include<fstream>
#include<istream>
#include<string>




int main()
{

matrix my_matrix;
int m, n;
cout << "enter the matrix row number";
cin >> m;
my_matrix.setRow(m);
vector<vector<int>> matrix_shi;
vector<vector<int>> matrix_shishir;

cout << "enter the matrix column number";
cin >> n;
my_matrix.setColumn(n);

my_matrix.definematrix(m, n);

cout << "enter the matrix values";
for (int i = 0; i < m;i++)
{
for (int j = 0;j < n;j++)
{
vector<vector<int>> matrix_shi(i, std::vector<int>(j));
cin >> matrix_shi[i][j];

}

}

for (int i = 0; i < m;i++)
{
for (int j = 0;j < n;j++)
{
vector<vector<int>> matrix_shi(i, std::vector<int>(j));
cout << matrix_shi[i][j];

}

}

for (int p = 0; p < m;p++)
{
for (int q = 0;q < n;q++)
{

cin >> matrix_shishir[p][q];


}

}
for (int i = 0; i < m;i++)
{
for (int j = 0;j < n;j++)
{

cin >> matrix_shi[i][j];

}

}




my_matrix.matrixA(matrix_shishir, matrix_shi);

}
Last edited on
Windows build error.

Put simply, do not #include cpp files. Certainly do not stick #pragma once in a cpp file ; that just suggests you don't understand what a cpp file is for and you're just writing code at random hoping to make things work.
Last edited on
The two loops under cout << "enter the matrix values"; don't do anything (other than ask the user for numbers that are thrown away).

You should fix the logic before looking for the crash.
@Repeater . Thanks for your reply . I am a beginner . Would it be ok if i asked what #pragma once does ? can you please clarify about what you stated about cpp file . I would be most grateful
Topic archived. No new replies allowed.