c++ - 链接错误 : undefined reference within the same class

标签 c++ class include

我在 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/

相关文章:

c++ - 是一个捕获其中所有异常的 C++ 函数 "noexcept"吗?

c++ - 用户定义文字的范围是什么?

c++ - 如何在这种情况下应用 C++ std 算法?

class - 为什么 Typescript 类会自动实现接口(interface)?

c++ - wchar* 和打印它的问题 C

python - 从类初始化器内部调用方法是Pythonic吗?

c++ - 为方阵创建构造函数的问题

c# - ASP.Net - 如何包含另一个项目中的嵌入式 JavaScript 文件?

C++ #include - 只有部分#include

c++ - 为什么 `include <iostream>` 最终包含这么多*文件?