我在CodeBlocks中有2个项目:
图灵机
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一切正常)。路径也是正确的(TuringMachineTest的目录在TuringMachine的目录中)。
但是编译器说:
||=== 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的引用)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句