我无法让这段简单的代码在不显式地将TestClass.cpp文件包含到main.cpp文件中的情况下编译通过。我做错了什么?谢谢!
以下是代码:
TestClass.h
#ifndef TESTCLASS_H_
#define TESTCLASS_H_
class TestClass
{
public:
static int foo();
};
#endif
测试类.cpp
#include "TestClass.h"
int TestClass::foo() { return 42; }
主函数文件:main.cpp
#include <iostream>
#include "TestClass.h"
using namespace std;
int main()
{
cout << TestClass::foo() << endl;
return 0;
}
这里是错误信息:
g++ main.cpp -o main.app
/tmp/ccCjOhpy.o: In function `main':
main.cpp:(.text+0x18e): undefined reference to `TestClass::foo()'
collect2: ld returned 1 exit status