对代码::块中另一个项目的函数的未定义引用
Undefined reference to func from another project in Code::Blocks
我在CodeBlocks中有两个项目:
图灵机
MyExp.h
class MyExp
{
public:
MyExp() = default;
double myExpFunc(double);
};
MyExp.cpp
#include "MyExp.h"
double MyExp::myExpFunc(double x) //fixed the lack of MyExp::, but still doesn't work
{
return x*x;
}
第二个项目:TuringMachineTests
main.cpp
#include "../include/MyExp.h"
#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE( my_test )
{
MyExp me;
int val = me.myExpFunc(5.0);
BOOST_CHECK_EQUAL(val, 24);
}
Boost的东西工作正常(不调用myExpFunc,一切正常)。路径也是正确的(图灵机测试的目录在图灵机的目录中)。
但编译器说:
||=== Build: Debug in TuringMachine (compiler: GNU GCC Compiler) ===|
||=== Build: Debug in TuringMachineTests (compiler: GNU GCC Compiler) ===|
obj/Debug/main.o||In function `my_test::test_method()':|
.../TuringMachine/TuringMachineTests/main.cpp|8|undefined reference to `MyExp::myExpFunc(double)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
听起来Tests项目找不到MyExp.cpp(请注意,默认的ctor是正常调用的)。有什么想法吗?
在实现方法时忘记了类名:
double MyExp::myExpFunc(double x)
// ^^^^^^^
{
...
}
此外,您必须将文件"MyExp.cpp"添加到项目中。必须编译并链接该文件。否则,链接过程中将出现错误(未定义对"MyExp::myExpFunc"的引用)。
相关文章:
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 无法将右值引用函数与 GCC 匹配
- C ++引用函数参数似乎包含原始对象的副本,而不是充当"real reference"
- 通过引用函数传递指针参数是什么意思?
- 常量引用函数参数的地址何时唯一?
- 我想了解为什么在这个例子中使用引用函数?或者引用在c++函数中的重要性
- C++ 通过引用函数传递数组,但内容保持不变
- 用数组或指针引用函数?
- 引用函数如何"int &foo();"工作?
- 常量引用函数参数:是否可以禁止临时对象?
- 计算在代码中引用函数的次数
- <initializer_list> 引用函数
- 字符串引用(函数)的差异
- 在类中引用函数时的"No Matching Function for Call"
- 无法通过引用函数"calcValues"中"firstNum"和"secondNum"变量来更改值
- 尝试引用已删除的函数(不引用函数)
- 引用函数指针
- 引用函数按值和自动返回
- 为什么我们可以取消引用函数指针
- 通过引用函数传递值的目的是什么