链接器未定义符号错误
Linker undefined symbols error
我的两个文件userinterface.h
#ifndef USERINTERFACE_H
#define USERINTERFACE_H
#include <string>
#include "vocabcollection.h"
namespace user_interface
{
//Finds a file
//
//Returns when user selects a file
std::string findFile();
//more comments followed by functions
}
#endif
和userinterface.cpp,
#include "userinterface.h"
using namespace std;
using namespace user_interface;
string findFile()
{
return "./";
}
//more placeholder implementations of such functions; void functions have nothing within
//the brackets
从链接器中给了我大量的错误:
Undefined symbols for architecture x86_64:
make: Leaving directory `longdirectorypath'
"user_interface::showTestResults(int, int)", referenced from:
vocabCollection::test() in vocabcollection.o
"user_interface::get(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
addNewCollection() in mainlogic.o
loadNewCollection() in mainlogic.o
"user_interface::findFile()", referenced from:
loadNewCollection() in mainlogic.o
"user_interface::displayMainMenu(std::vector<vocabCollection, std::allocator<vocabCollection> >)", referenced from:
mainlogic() in mainlogic.o
"user_interface::getUserAction()", referenced from:
mainlogic() in mainlogic.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [cheapassVocab.app/Contents/MacOS/cheapassVocab] Error 1
The process "/usr/bin/make" exited with code 2.
Error while building project cheapassVocab (target: Desktop)
When executing build step 'Make'
这是怎么回事?
在头文件中,在命名空间user_interface
中声明函数findFile
。在cpp文件中定义了free函数 findFile
。是的,你是using namespace user_interface
,但是编译器不知道那里定义的findFile
属于namespace user_interface
。所有这些的结果是您已经声明了user_interface::findFile
并定义了::findFile
。当您调用user_interface::findFile
时,链接器找不到它,因为只有自由函数findFile
。
轻松解决- cpp文件:
#include "userinterface.h"
using namespace std;
namespace user_interface
{
string findFile()
{
return "./";
}
}
你不能这样实现findFile
;它必须放在命名空间中:
namespace user_interface
{
string findFile()
{
return "./";
}
}
or:
string user_interface::findFile()
{
return "./";
}
using
指令仅用于查找,而不是用于定义-想象using namespace std;
将对您的所有函数定义做什么!
您在错误的命名空间中定义了findFile。
std::string user_interface::findFile()
{
return "./";
}
或
namespace user_interface
{
std::string findFile()
{
return "./";
}
}
using
不影响名称的定义位置,它只影响名称的查找方式。
相关文章:
- 在C++中使用内联方法时出现未定义的符号错误
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 错误:隐式转换更改符号:'int'到'unsigned long'
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 添加符号 libgtest.so 错误:命令行中缺少 DSO
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 体系结构x86_64的未定义符号:链接器错误
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 与Qt交叉编译到Raspberry Pi 3B+通讯录(协议缓冲区)-错误符号查找错误
- 链接错误:符号重复
- 架构X86_64静态类成员的错误符号X86_64错误
- 缓冲区中使用 GetWindowText 的错误符号
- OSX:ld:警告:错误符号操作:适用于核心库
- 为什么 Folly 的共享库引用了 Boost 中的错误符号?(OS X 上的 GCC)
- 嵌入lua运行时错误:符号未找到:_luaL_newstate
- C++ 找不到"::"架构的苹果马赫-O链接器错误/符号