我在 g++ 4.9 中遇到以下错误:
basis.cpp:16: undefined reference to `Basis::foo(int, int)'
这是头文件:
#ifndef BASIS_H
#define BASIS_H
#include "common.h"
#include <math.h>
#include "xdouble.h"
using namespace std;
class Basis {
private:
int rank;
int dim;
public:
Basis(); //Empty constructor
Basis(int r, int d); //Default constructor
void foo(int a, int b);
void bar(int a, int b);
};
#endif
basis.cpp 文件如下:
#include "basis.h"
Basis::Basis()
{
rank = 0;
dim = 0;
}
Basis::Basis(int r, int d) // Default constructor
{
rank = r;
dim = d;
}
void Basis::bar(int a, int b)
{
void foo(int a, int b);
}
void Basis::foo(int a, int b)
{
}
即使我包含了 basis.h 文件,我仍然收到 undefined reference 错误,我不明白为什么会这样。我做错了什么?
谢谢
最佳答案
看起来像是复制粘贴错误。试试这个:
void Basis::bar(int a, int b)
{
foo(a, b);
}
你犯了一个错误,因为你复制并粘贴了函数 foo 的定义到你想调用这个函数的地方。
关于c++ - 链接错误 : undefined reference within the same class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30254032/