我無法弄清楚這裏有什麼問題。我有一個ConsoleIO
類,它包含兩個方法:用靜態方法鏈接錯誤
static void OutputMessage(const std::string &message);
static void OutputMessageNoNewLine(const std::string &message);
他們在頭內嵌定義:
inline void ConsoleIO::OutputMessage(const std::string &message)
{
std::cout << message << std::endl;
}
inline void OutputMessageNoNewLine(const std::string &message)
{
std::cout << message << " ";
std::flush(std::cout);
}
而且另一個類,ContentParser
,與方法:
static const bool StringEquals(const char *a, const char *b);
template <typename T>
static const std::string NumToString(const T num);
template <typename T>
static const T StringToNum(const std::string &s);
它們在單獨的文件ContentParser.cpp
中定義。
template <typename T>
const std::string ContentParser::NumToString(const T num)
{
std::ostringstream ss;
ss << num;
return ss.str();
}
我在AdventureGame
類以下代碼:
ConsoleIO::OutputMessageNoNewLine(ContentParser::NumToString(num+1));
ConsoleIO::OutputMessage(" - " + decision.choices.at(num).text);
奇怪的是,從上面的,底線工作正常,但聯(線65當頂部產生一個錯誤錯誤)。 StringEquals
方法也適用於任何地方。
這裏是生成日誌:
14:03:02 **** Incremental Build of configuration Debug for project AdventureGame ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o AdventureGame.o "..\\AdventureGame.cpp"
g++ "-LD:\\Program Files\\tinyxml2-master" -o AdventureGame.exe tinyxml2.o Main.o ContentParser.o AdventureGame.o -ltinyxml
AdventureGame.o: In function `AdventureGame::ProcessDecision()':
D:\adat\eclipse\AdventureGame\Debug/../AdventureGame.cpp:65: undefined reference to `std::string const ContentParser::NumToString<unsigned int>(unsigned int)'
D:\adat\eclipse\AdventureGame\Debug/../AdventureGame.cpp:65: undefined reference to `ConsoleIO::OutputMessageNoNewLine(std::string const&)'
D:\adat\eclipse\AdventureGame\Debug/../AdventureGame.cpp:68: undefined reference to `ConsoleIO::OutputMessageNoNewLine(std::string const&)'
collect2.exe: error: ld returned 1 exit status
我缺少什麼?
我想你沒有正確定義它們'在標題中內聯'。但是由於您沒有顯示該代碼,這只是一個猜測。請用相關代碼更新您的問題。 – john
如果您將首行更改爲'ConsoleIO :: OutputMessage(ContentParser :: NumToString(num + 1));',僅供測試 – dzada
@john編輯該帖子。 – Innkeeper