错误:在 Code::Blocks 中使用 const std::string& 和 const std::vector<int>& 参数对函数声明的未定义引用

Error: undefined reference to function declaration with const std::string& and const std::vector<int>& parameters in Code::Blocks

提问人:Canavan Neeson 提问时间:5/6/2018 最后编辑:Canavan Neeson 更新时间:5/6/2018 访问量:425

问:

尝试在 Code::Blocks 17.12(Windows 10,默认 MinGW GCC 编译器)上编译 C++ 项目时出现错误。函数

std::vector<int> crib_drag(const std::string&, const std::vector<int>&);

在 crib_drag.h 中声明,这是我通过 C::B 对话框创建的(crib_drag.h #includes 向量和字符串)。该函数在 crib_drag.cpp 中定义,也是通过 C::B 对话框创建的:

#include "crib_drag.h"
std::vector<int> crib_drag(const std::string& term, const std::vector<int>& xored)
{
    std::vector<int> ret;
    //Do some stuff
    return ret;
}

main.cpp包括 crib_drag.h 和 #include “crib_drag.h” 在 main 中,crib_drag 被调用。 编译会给 crib_drag 函数一个未定义的引用错误。我的理解是,我不需要更改链接器设置,因为所有文件都是使用 C::B 项目中的默认设置创建的,并且我没有更改任何文件的文件路径。

谁能解释一下这里出了什么问题以及如何解决它?提前致谢。

生成日志如下所示:

-------------- Build: Debug in CribDrag (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe  -o bin\Debug\CribDrag.exe obj\Debug\crib_drag.o obj\Debug\main.o   
obj\Debug\main.o: In function `main':
C:/[Long file path omitted]/CribDrag/main.cpp:13: undefined reference to `crib_drag(std::string const&, std::vector<int, std::allocator<int> > const&)'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

主要:

#include <iostream>
#include "crib_drag.h"
#include <vector>

using namespace std;
int main() {
    vector<int> xored1{14, 6, 31, 30, [omitted for brevity]};
    string word("overnight");
    vector<int> found_at = crib_drag(word, xored1);//Problem line
    //.....
    return 0;
}
C++ 编译器错误 STL 代码块 undefined-reference

评论

0赞 Vamsidhar Reddy Gaddam 5/6/2018
你如何在 main 中调用这个函数?
0赞 Canavan Neeson 5/6/2018
int main(){ vector<int> xored1{14, 6, 31, 30, 28, 12, 20, 3, 30, 8, 8, 7, 31, 23, 18, 18, 1, 4, 23, 31, 28, 8, 0, 0, [为简洁起见,省略了更多整数]}; string word(“overnight”); vector<int> found_at = crib_drag(word, xored1); /*更多内容*/ return 0; }
0赞 Canavan Neeson 5/6/2018
很抱歉评论中没有换行符
0赞 Vamsidhar Reddy Gaddam 5/6/2018
可能会更新问题吗?
0赞 Canavan Neeson 5/6/2018
更新。谢谢,不知道我能做到这一点(我是 Stack Overflow 的新手)

答: 暂无答案