Hi, everyone!
I'm having trouble with a linker error (I'm using Visual Studio 2008) and I would like to have some tips on solving this problem:
This is my code:
Bill.cpp
#include "Bill.h"
#include "../Exceptions/InvalidArgumentException.h"
Bill::Bill()
{
}
Bill::Bill(string shopName)
{
if (shopName == "")
{
throw(InvalidArgumentException("The shop name cannot be empty."));
}
m_shopName = shopName;
}
InvalidArgumentException.h
#ifndef INVALIDARGUMENTEXCEPTION_H
#define INVALIDARGUMENTEXCEPTION_H
#include <exception>
using namespace std;
class InvalidArgumentException : public exception
{
public:
InvalidArgumentException(const char* invalidArgument);
const char* what() const throw();
static size_t MaximumMessageLength()
{
return 100;
}
private:
InvalidArgumentException();
InvalidArgumentException(const InvalidArgumentException&);
void InitializeMessage();
void ValidateArgument(const char* invalidArgument);
const char* m_invalidArgument;
char m_message[100]; //TODO: Change the way of returning a const char*. That's ugly to use an automatic-allocated array.
};
#endif
InvalidArgumentException.cpp
#include <string>
#include "InvalidArgumentException.h"
//Public Methods
InvalidArgumentException::InvalidArgumentException(const char* invalidArgument)
{
ValidateArgument(invalidArgument);
m_invalidArgument = invalidArgument;
InitializeMessage();
}
const char* InvalidArgumentException::what() const throw()
{
return m_message;
}
//Private Methods
void InvalidArgumentException::InitializeMessage()
{
strcpy_s(m_message, m_invalidArgument);
}
void InvalidArgumentException::ValidateArgument(const char* invalidArgument)
{
if (invalidArgument == 0)
{
throw(std::exception("The message pointer is null."));
}
else if (strlen(invalidArgument) == 0)
{
throw(std::exception("The message is empty."));
}
else if (strlen(invalidArgument) > MaximumMessageLength())
{
throw(std::exception("The message is too long."));
}
}
The linker output is:
1>Linking...
1>LINK : .\Output\Debug\Billzilla.exe not found or not built by the last incremental link; performing full link
1>Bill.obj : error LNK2001: unresolved external symbol "private: __thiscall InvalidArgumentException::InvalidArgumentException(class InvalidArgumentException const &)" (??0InvalidArgumentException@@AAE@ABV0@@Z)
1>.\Output\Debug\Billzilla.exe : fatal error LNK1120: 1 unresolved externals
Looks like Bill.obj can't find the constructor symbol in InvalidArgumentException.obj. It's the constructor call in the throw() of Bill.cpp that causes the error. It's been a long time using C++, and I'm unable to solve this problem. Can you help me?