Unresolved external symbol, overloading operator== in my class
Jan 12, 2016 at 11:33am UTC
Hello.
I'm making a static library. I have a GString class in which I overload some operators (<<, =, [])
I'm now trying to overload the == operator, but I get an unresolved external error from the linker.
1 2 3 4 5 6 7 8 9 10 11 12 13
namespace ged
{
namespace Data
{
class GString
{
int size;
public :
/* OPERATOR OVERLOADING */
friend bool operator ==(const GString& first, const GString& second);
};
}
}
1 2 3 4
bool ged::Data::operator ==(const GString& first, const GString& second)
{
// implementation...
}
And this is a screenshot:
http://prntscr.com/9p4d53
This is the error i get:
unresolved external symbol "bool __cdecl ged::Data::operator==(class ged::Data::GString const &,class ged::Data::GString const &)" (??8Data@ged@@YA_NABVGString@01@0@Z)
This is the main() code where I get the error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <string>
#include <conio.h>
#include "gedamial.h"
using namespace std;
using namespace ged::Data;
using namespace ged::Mathematics;
int main()
{
GString my = "ciao" ;
GString my2 = "addio" ;
if (my == my2)
cout << "uguali" ;
else
cout << "non uguali" ;
_getch();
return 0;
}
Last edited on Jan 12, 2016 at 12:24pm UTC
Jan 12, 2016 at 12:15pm UTC
What compiler do you use?
The gcc shows this error:
gcc wrote:D:\temp\temp\test2\main.cpp|13|error: 'bool ged::Data::operator==(const ged::Data::GString&, const ged::Data::GString&)' should have been declared inside 'ged::Data' |
Your compiler obviously doesn't think that the implemented function is the friend function of
GString
.
Jan 12, 2016 at 12:20pm UTC
I use the MSVC++ compiler
Hmh, so what should I do? Put the implementation in the header file instead of in the separate .cpp?
Jan 12, 2016 at 12:22pm UTC
Your class GString needs to end with };
Jan 12, 2016 at 12:24pm UTC
It does. My bad copying&pasting the code in the forum.
Jan 12, 2016 at 12:30pm UTC
Hmh, so what should I do? Put the implementation in the header file instead of in the separate .cpp?
Yes.
Or alternatively the gcc is satified when you put it in the namespaces:
1 2 3 4 5 6 7 8 9 10
namespace ged
{
namespace Data
{
bool operator ==(const GString& first, const GString& second)
{
// implementation...
}
}
}
Last edited on Jan 12, 2016 at 12:35pm UTC
Jan 12, 2016 at 12:37pm UTC
That's strange, i can't understand why.
I have a bunch of operator overloads, even this which i declare as friend
.h
friend std::ostream& operator <<(std::ostream& s, const GString& other);
.cpp
1 2 3 4 5
std::ostream& ged::Data::operator <<(std::ostream & s, const GString & other)
{
// ... bla bla bla ...
return s;
}
But this one works fine.
Mhm, however nevermind
Thanks ;)
Last edited on Jan 12, 2016 at 12:38pm UTC
Topic archived. No new replies allowed.