Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello dear fellas,

I have this declaration in a header:

C#
class Comparator
{
public:
    inline bool operator () (const SGEEmotion& left, const SGEEmotion& right) const;
};


And an implementation in a .cpp file. Oddly enough, this gives me an unresolved external for this method.

However, if I don't mark it as inline everything is fine... Does this mean it can't be inline? Does this apply to all operators? It doesn't make much sense...

Thanks in advance.

PS: I'm using VS08!

Update:

Ok, now I'm really confused! This works:

C#
class SGEAction
{
    friend class SGEActionFactory;

public:
    class Comparator
    {
    public:
        inline bool operator () (const SGEAction& left, const SGEAction& right) const;
    };
};


This doesn't:

C#
class SGEEmotion
{
public:
    class Comparator
    {
    public:
        inline bool operator () (const SGEEmotion& left, const SGEEmotion& right) const;
    };
};


Didn't think the implementation was important... I don't know anymore:

C#
bool SGEAction::Comparator::operator () (const SGEAction& left, const SGEAction& right) const
{ return left.id() < right.id(); }


C#
bool SGEEmotion::Comparator::operator () (const SGEEmotion& left, const SGEEmotion& right) const
{ return left.id() < right.id(); }


Don't get it...
Posted
Updated 27-Mar-12 15:12pm
v2
Comments
Malli_S 28-Mar-12 2:55am    
For which function/object you get linking error? Can you please post the error here (with reference to the code)?
AndreFratelli 28-Mar-12 8:10am    
It's SGEEmotion::Comparator::operator ()


Error 1 error LNK2019: unresolved external symbol "public: bool __thiscall SGEEmotion::Comparator::operator()(class SGEEmotion const &,class SGEEmotion const &)const " (??RComparator@SGEEmotion@@QBE_NABV1@0@Z) referenced in function "public: float & __thiscall std::map<class sgeemotion,float,class="" sgeemotion::comparator,class="" std::allocator<struct="" std::pair<class="" sgeemotion="" const="" ,float=""> > >::operator[](class SGEEmotion const &)" (??A?$map@VSGEEmotion@@MVComparator@1@V?$allocator@U?$pair@$$CBVSGEEmotion@@M@std@@@std@@@std@@QAEAAMABVSGEEmotion@@@Z) SGEMood.obj
Stefan_Lang 28-Mar-12 10:30am    
Hm, apparently you're passing that comparator to a map for sorting. That's where it's called, and that's where the compiler fails to resolve it (in std::map::operator[](), to be precise). If that comparator definition is somewhere, the linker should be able to resolve it - that's definitely odd. Sorry, for all I know this should either work or cause a (more helpful) compiler error, not a linker error.

Personally I'd forget about inline. It's not reliable anyway, and you probably don't even know for sure if this piece of code requires performance optimization.
AndreFratelli 28-Mar-12 10:47am    
Indeed, I'm not inlining it anymore. I posted because I think it's odd and couldn't understand the reason. Right know I just wan't to understand it =)

regards
Malli_S 29-Mar-12 1:45am    
I'm surprised that how it survived the compiler pass. If possible can you send me the source code?

Have you read this carefully.

http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx[^]

The inline keyword is part of the language.

What actually happens is entirely compiler dependent.
 
Share this answer
 
v2
Comments
AndreFratelli 28-Mar-12 8:19am    
I've read it, and I know what inlining is. I don't get the point you're trying to make..
1. inline keyword is always a request from programmer. making any function inline dos not makes it inline. it is upto the compiler to decide whether to make it inline or not depending on how this function is being refrenced( but that is no reason to give compile error).

2. If I put the definition of the function in the header file at the point of declaration then that function will be treated as inline and chances of compiler making it inline are also more than the other approach.

3. If the compiler decide to make the function inline then all the types being used in the functions should be visible. For this I can either include a header file or do a forward declaration.

I suggest you try to put both function definition in header file at the point of declaration only without inline keyword and see if it works. If it does then you can try using inline keyword(although it will be redundant)
 
Share this answer
 
Comments
AndreFratelli 28-Mar-12 8:25am    
Yes, I know all that. Writing the body with the declaration (in the header file) gives other unresolved externals, oddly enough:

Error 1 error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char="">,class std::allocator<char> > const & __thiscall SGEEmotion::id(void)const " (?id@SGEEmotion@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: bool __thiscall SGEEmotion::Comparator::operator()(class SGEEmotion const &,class SGEEmotion const &)const " (??RComparator@SGEEmotion@@QBE_NABV1@0@Z) SGEMood.obj
Stefan_Lang 28-Mar-12 10:01am    
short form of that error message:

Missing function definition:
std::string& SGEEmotion::id()const

Did you forget to implement that function?
AndreFratelli 28-Mar-12 10:45am    
Sorry, didn't notice that last line. Yes, I did. Please note that not inlining the operator works, so everything else works.
Stefan_Lang 28-Mar-12 11:03am    
Not your fault, I added that last line later, so you may not have seen it when you read the response for the first time ;)

Unfortunately I can't help. I strongly suspect it's a problem with VS, not your code. But I don't have VS08 here to try.
AndreFratelli 28-Mar-12 11:04am    
Maybe a bug report, then?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900