gpt4 book ai didi

c++ - 链接器 undefined symbol 错误

转载 作者:行者123 更新时间:2023-11-30 00:40:06 26 4
gpt4 key购买 nike

好像是我的两个文件,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

和用户界面.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 "./";
}
}

关于c++ - 链接器 undefined symbol 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com