【发布时间】:2015-08-04 17:02:04
【问题描述】:
考虑以下两个程序。
p1.cpp
#include <iostream>
struct test
{
void fun();
};
int main()
{
test t;
t.fun();
}
p2.cpp
#include <iostream>
void test::fun()
{
std::cout<<"fun() is called\n";
}
我正在编译如下。
g++ -c -o p1.o p1.cpp
g++ -c -o p2.o p2.cpp <--------- This gives me compiler error.
我该如何解决这个错误?我做错了什么?
【问题讨论】:
-
你必须在p2.cpp中声明类
test(包括它的方法)(通常,我们在头文件中声明它,并在两个源文件中都包含头文件)
标签: c++ function definition compilation