跨多个文件的命名空间"undefined reference"错误
"undefined reference" error with namespace across multiple files
我看了几个相关的帖子,但这个错误没有运气。当我的命名空间存在于多个文件中时,我在下面收到此未定义的引用错误消息。如果我只编译 ConsoleTest.cpp使用控制台的内容.cpp将源代码编译到其中。
我将不胜感激有关此问题的任何反馈,提前感谢您。
g++ Console.cpp ConsoleTest.cpp -o ConsoleTest.o -Wall
/tmp/cc8KfSLh.o: In function `getValueTest()':
ConsoleTest.cpp:(.text+0x132): undefined reference to `void Console::getValue<unsigned int>(unsigned int&)'
collect2: ld returned 1 exit status
控制台.h
#include <iostream>
#include <sstream>
#include <string>
namespace Console
{
std::string getLine();
template <typename T>
void getValue(T &value);
}
控制台.cpp
#include "Console.h"
using namespace std;
namespace Console
{
string getLine()
{
string str;
while (true)
{
cin.clear();
if (cin.eof()) {
break; // handle eof (Ctrl-D) gracefully
}
if (cin.good()) {
char next = cin.get();
if (next == 'n')
break;
str += next; // add character to string
} else {
cin.clear(); // clear error state
string badToken;
cin >> badToken;
cerr << "Bad input encountered: " << badToken << endl;
}
}
return str;
}
template <typename T>
void getValue(T &value)
{
string inputStr = Console::getLine();
istringstream strStream(inputStr);
strStream >> value;
}
}
控制台测试.cpp
#include "Console.h"
void getLineTest()
{
std::string str;
std::cout << "getLinetest" << std::endl;
while (str != "next")
{
str = Console::getLine();
std::cout << "<string>" << str << "</string>"<< std::endl;
}
}
void getValueTest()
{
std::cout << "getValueTest" << std::endl;
unsigned x = 0;
while (x != 12345)
{
Console::getValue(x);
std::cout << "x: " << x << std::endl;
}
}
int main()
{
getLineTest();
getValueTest();
return 0;
}
模板函数需要在标头中定义,而不仅仅是声明。编译器需要查看模板实现和模板参数,以构建主服务器所需的专用化。所以直接把定义放在标题中:
namespace Console
{
std::string getLine();
template <typename T>
inline void getValue(T &value) {
string inputStr = Console::getLine();
istringstream strStream(inputStr);
strStream >> value;
}
}
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"