I have the following classes:
MessageConstants.h:
class MessageConstants
{
public:
...
static const int ErrorDescriptionLength = 256;
...
};
SystemMessage.h:
class EvtError
{
private:
struct MsgData
{
int errorCode;
char errorDescription[MessageConstants::ErrorDescriptionLength];
}__attribute__((packed)) msgData;
public:
EvtError(int errorCode, string errorDescription);
inline void setErrorDescription(string desc){memcpy(msgData.errorDescription, desc.c_str(),
min(MessageConstants::ErrorDescriptionLength, (int)desc.length()));}
};
SystemMessage.cpp:
EvtError::EvtError(int errorCode, string errorDesc)
{
memset(&msgData, '\0', sizeof(msgData));
msgData.errorCode = errorCode;
memcpy(msgData.errorDescription, errorDesc.c_str(), min(MessageConstants::ErrorDescriptionLength, (int)errorDesc.length()));
}
I got the following link error on SystemMessage.cpp statement memcpy(msgData.errorDescription, errorDesc.c_str(), min(MessageConstants::ErrorDescriptionLength, (int)errorDesc.length()));
:
In function EvtError::EvtError(int, std::string): undefined reference to MessageConstants::ErrorDescriptionLength collect2: error: ld returned 1 exit status make: [link] Error 1
If I replace the MessageConstants::ErrorDescriptionLength
with sizeof(msgData.errorDescription)
, the link error disappear.
My questions:
Why it doesn't complain the
MessageConstants::ErrorDescriptionLength
in the SystemMessage.h file, where there are two places with it?How to avoid above link error?