PDamasceno
asked on
LNK2019: unresolved external symbol error on function call
Hey!
I am developing an application using JetBrains CLion 2018.2.4 with cmake 3.12.2 and Visual Studio 2017 compiler.
Let me set up things first.
Here is the base class:
Here is the derived class:
Here is how I'm acessing the function GetMessage();
When I try to execute GetMessage() like up above, I get the error below:
It has to do with the function GetMessage() but I'm failing to see what is wrong. If I remove that call, everything compile fine.
Also, I do the exact same call in other class and it is working fine.
Help is needed!!!
I am developing an application using JetBrains CLion 2018.2.4 with cmake 3.12.2 and Visual Studio 2017 compiler.
Let me set up things first.
Here is the base class:
class Header
{
public:
enum MessageType
{
Message = 100,
Command = 101,
InvalidMessage = 102
};
Header();
Header(Header::MessageType messageType);
virtual ~Header() = default;
Header::MessageType GetMessageType() const;
private:
Header::MessageType m_messageType;
};
Header::Header() :
m_messageType(Header::MessageType::InvalidMessage)
{
}
Header::Header(Header::MessageType messageType) :
m_messageType(messageType)
{
}
Header::MessageType Header::GetMessageType() const
{
return m_messageType;
}
Here is the derived class:
class Message : public Header
{
public:
Message();
~Message() = default;
void SetMessage(std::string message);
std::string GetMessage() const&;
private:
std::string m_message;
};
Message::Message() :
Header(Header::MessageType::Message),
m_message("")
{
}
void Message::SetMessage(std::string message)
{
m_message = message;
}
std::string Message::GetMessage() const&
{
return m_message;
}
Here is how I'm acessing the function GetMessage();
int main()
{
Reader reader;
auto basePtr = reader.ReadAll(message);
auto derivedPtr = std::dynamic_pointer_cast<communication::Message>(basePtr);
std::cout << "read: " << derivedPtr->GetMessageType() << ";" << derivedPtr->GetMessage();
}
When I try to execute GetMessage() like up above, I get the error below:
Server.cpp.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall communication::Message::GetMessageA(void)const & " (?GetMessageA@Message@communication@@QGBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
Server.exe : fatal error LNK1120: 1 unresolved externals
It has to do with the function GetMessage() but I'm failing to see what is wrong. If I remove that call, everything compile fine.
Also, I do the exact same call in other class and it is working fine.
Help is needed!!!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks, Sara, your right, I misinterpreted the result I had with a test code I wrote ...
Zoppo, it was a minor correction which doesn't lessen the good catch you did with your solution. ;-)
@PDamasceno
a similar mess you could get by calling a function 'GetObject' or 'GetItemName'. actually those macros switching function names and types between wide and single characters in WINAPI and MFC are a great nuisance.
Sara
@PDamasceno
a similar mess you could get by calling a function 'GetObject' or 'GetItemName'. actually those macros switching function names and types between wide and single characters in WINAPI and MFC are a great nuisance.
Sara
ASKER
Hey people!
Thanks a million for explaining everything.
I ended up changing the function name. Wasn't aware there was these kind of words it was not good to use in the name of functions when programming on Windows.
Everyone was very helpful. Thanks again!
Thanks a million for explaining everything.
I ended up changing the function name. Wasn't aware there was these kind of words it was not good to use in the name of functions when programming on Windows.
Everyone was very helpful. Thanks again!
the problem comes the GetMessage you have in your class conflicts with a macro GetMessage defined in <winuser.h>:
Open in new window
IMO this can only happen if you include your classes header files before <winuser.h> is included (at least I found no other way to reproduce the problem), so you should check the inclusion-order.
Hope this helps,
ZOPPO