I have following problem (linker error):
In header file I defined namespace
something like this:
Code:
namespace Klass{
class Test
{
public:
int itsInt;
public:
void set(int);
void show();
friend Test operator +(Test &,Test &);
};
}
In second i have this:
Code:
#include "Name.h"
#include <iostream>
using namespace Klass;
using namespace std;
void Test::set(int i)
{
itsInt=i;
}
void Test::show()
{
cout<<itsInt;
}
Test operator +(Test &A,Test &B)//Test Klass::operator+(....)
{
Test tmp;
tmp.itsInt=A.itsInt+B.itsInt;
return tmp;
}
And finally in Main file I have:
Code:
#include "Name.h"
using namespace Klass;
int main()
{
Test A;
A.set(2);
Test B;
B.set(3);
Test C=A+B;
C.show();
return 0;
}
If If try to compile and link I get linker error
Main1.obj : error LNK2001: unresolved external symbol "class Klass::Test __cdecl Klass::operator+(class Klass::Test &,class Klass::Test &)" (??HKlass@@YA?AVTest@0@AAV10@0@Z)
if I put Klass:: when I define operator + there are no error.
My question is why this stuff with namespace is not working when I deal with friend functions and why I have to explicitly put
Code:
Test Klass::operator +(Test &A,Test &B)
when I put using namespace Klass;
What is the reason?
Thanks in advance.