error LNK2019-LKN2001

Hi,
Could anyone help me understand these errors please!!
It complies fine, but build failed :(

1>matrix.obj : error LNK2019: unresolved external symbol "public: __thiscall matrixClass::matrixClass(void)" (??0matrixClass@@QAE@XZ) referenced in function "class matrixClass __cdecl operator+(class matrixClass const &,class matrixClass const &)" (??H@YA?AVmatrixClass@@ABV0@0@Z)
1>program3.obj : error LNK2001: unresolved external symbol "public: __thiscall matrixClass::matrixClass(void)" (??0matrixClass@@QAE@XZ)
1>C:\Documents and Settings\Mostafa\my documents\visual studio 2010\Projects\program3\Debug\program3.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

Thanks
It's likely you have prototypes for your matrixClass constructor and your global operator+() but haven't linked in the source file with the implementation inside.
I am still confused. SO, what should I do to erase these errors!!
Do I have to copy my code here??
You need to link your source files. How are you building your project? An IDE?
I am dividing my program into matrix.h, matrix.cpp, and main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//(.h)
// The program that uses the class Defintions

#pragma once;

#include <iostream>
using namespace std;

class matrixClass
{
public:
	//methods or functions
	matrixClass();

	
    //accessors or getters
	
    //modifiers or setters

	//print and read
	void print(ostream &out) const;
    bool read(istream &in);
	
	 //friends
	friend void print(ostream &out,const matrixClass & right);
    friend ostream & operator<<(ostream & out,const matrixClass &right);
    friend istream & operator>>(istream &in, matrixClass &right);
	friend matrixClass operator+(const matrixClass &m1,
        const matrixClass &m2);
    friend matrixClass operator*(int sca,
        const matrixClass &m1);
	friend matrixClass operator*(const matrixClass &m1,
        const matrixClass &m2);

private:
	int num;
	int m1[4][4], m2[4][4], matrix[4][4];
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//(main)

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "matrix.h"
using namespace std;


int main()
{
	matrixClass matrix[4][4];

	ifstream fin("input3.txt");
    ofstream fout("output3.txt");
	if(fin.fail())	//Check failing of input file.
    {
        cerr<<"Unable to open fin file\n";
        exit(2);
    }

	int i=0,j=0, num=0;
	int sca;
	int m1[4][4], m2[4][4], m[4][4];

	while(i<4 && fin >> matrix[i][j])
	{
		i++;
		j++;
	}

	//Reading the operation number
	if (num=1)
	{
		for(i=0;i<4;i++)
		 for(j=0;j<4;j++)
			 m[i][j]=m1[i][j]+m2[i][j];
	}
	else
		if (num=2)
		{
			for(i=0;i<4;i++)
			 for(j=0;j<4;j++)
				m[i][j]=sca*m1[i][j];
		}
		else
			if (num=3)
			{
				for(i=0;i<4;i++)
					for(j=0;j<4;j++)
						m[i][j]=m1[i][0]*m2[0][j]+m1[i][1]*m2[1][j]
						+m1[i][2]*m2[2][j]+m1[i][3]*m2[3][j];
			} 


	 for(i=0;i<num;i++)
		 for(j=0;j<4;j++)
    {
        fout << num<<endl;
		fout<<m1[i][j]<< endl;
    }
	
	//close the file.
	fin.close();
	fout.close();

    return 0;	//End of function main.
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//(matrix.cpp)
//the implementation file"member function body"

#include "matrix.h"
#include <string>
#include <iomanip>

//The defintion of each of the member function
//Read and Print

//read the matix
 bool matrixClass::read(istream &in)
 {
	 return in.good();
 }
 istream & operator>>(istream &in,matrixClass &right)
{
	in>>right.num
	  >>right.m1[4][4]
	  >>right.m2[4][4];

	return in;
}
 //print the matrix
void print(ostream &out,const matrixClass & right)
 {
	  out<<right.num<<setw(30)
		 <<right.m1<<setw(20)
		 <<right.m2<<setw(20)
		 <<endl;
 }
ostream & operator<<(ostream & out, const matrixClass & right)
 {
	 out<<right.num<<setw(30)
		 <<right.m1<<setw(20)
		 <<right.m2<<setw(20)
		 <<endl;

	 return out;
 }

int i, j, num;

matrixClass operator+(const matrixClass &m1, const matrixClass &m2)
{
	matrixClass hold;

	for(i=0;i<4;i++)
		 for(j=0;j<4;j++)
			hold.matrix[i][j]=m1.matrix[i][j]+m2.matrix[i][j];

	return hold;
}
matrixClass operator*(int sca, const matrixClass &m1)
{
	matrixClass hold;

	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			hold.matrix[i][j] = sca*m1.matrix[i][j];
				
	return hold;

}
matrixClass operator*(const matrixClass &m1, const matrixClass &m2)
{
	matrixClass hold;

	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			hold.matrix[i][j]=m1.matrix[i][0]*m2.matrix[0][j]
							 +m1.matrix[i][1]*m2.matrix[1][j]
							 +m1.matrix[i][2]*m2.matrix[2][j]
							 +m1.matrix[i][3]*m2.matrix[3][j];

	 return hold;
}
Are you using an IDE? You need to tell your linker (or IDE, which will tell the linker) that all these source files need to go together. Usually this is done by making a "project" or something.
I don't know what an IDE is!!
so my code is corect I just need this "IDE"
How are you compiling your program? Visual Studio? Or something else?
Visual Studio 2010
Are all 3 files part of your project?

If not, go to Project | Add Existing Item and add them all. Then rebuild.
Yes they are all part of one project.
Topic archived. No new replies allowed.