<分区>
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
我最近开始研究 C++ 的解释器,但我很生气,无论我尝试什么, vector 或数组都不能传递给外部类方法,所以我删除了我所做的一切。事实证明,我什至无法将 int 传递给另一个类。我决定在求助于 C 或 Java 之前再给 C++ 一次机会,但编译器仍然不能像我期望的那样工作。也许我忘记了一些关于 C++ 的简单内容,因为我已经有一段时间没有使用它了,但这看起来很简单。我的问题是:当参数未在同一文件中定义时,我无法将参数传递给其他类中的方法。这是我正在尝试做的事情:
主程序:main.cpp
#include "myclass.h"
int main() {
MyClass test;
int n = test.add(25, 30);
return n;
}
header :myclass.h
class MyClass {
public:
int add(int a, int b);
};
类实现:myclass.cpp
#include "myclass.h"
int MyClass::add(int a, int b) {
return a + b;
}
用 g++ main.cpp
编译它会产生
/tmp/ccAZr6EY.o: In function
main': main.cpp:(.text+0x1a): undefined reference to
MyClass::add(int, int)' collect2: error: ld returned 1 exit status
我到底做错了什么?另外,即使我的函数没有参数化,编译器也会对我大喊大叫,所以这一定是头文件有问题。
非常感谢任何帮助 - 谢谢!