I have 4 files, 2 headers and 2 cpp files.
Header file one:
#ifndef complex_2
#define complex_2
#include
#include
using
Example to repro LNK1169 & LNK2005 with structs: Place the following in the header file:
Struct Foo
{
std::wstring Bar[3]
};
Foo InitFoo[2]
{
{L"ConstBar1Val", L"ConstBar1Val2", L"ConstBar1Val3"},
{L"ConstBar2Val", L"ConstBar2Val2", L"ConstBar3Val3"}
};
Solution: Move the initialised struct & arrays to the corresponding cpp file.
Move the definition of the
std::ostream & operator<< (ostream &mm, const comp::complex &c)
in one of the CPP files.
The linker doesn't like this. Personally I'd create a new cpp file for this one too :)
When you define a standalone function (with a body) in header file you must use inline (functions defined within class definition are inline by default).
E.g. in header file one must be :
inline ostream& operator<< (ostream &mm, const complex &c){
if(c.get_im() >= 0){
mm << "(" << c.get_re() << " + " << c.get_im() << "i)" << endl;
}
if(c.get_im() < 0){
mm << "(" << c.get_re() << " - " << -(c.get_im()) << "i)" << endl;
}
return mm;
}
Otherwise you will define this function in each translation unit that includes that header and thus may get multiple definition error.