头文件中的类给出未定义的引用错误

时间:2020-05-06 13:17:12

标签: c++ oop compiler-errors dev-c++

我是C ++的新手,有一个我无法解决的问题。我编写了一个非常简单的类,并将其放在头文件中。以下是文件:

point.h:

// point.h
#include <iostream>

#ifndef _POINT_H_
#define _POINT_H_
using namespace std;

class Point{
    private:
        double x;
        double y;
    public:
        void generate(double a, double b);
        void print();
};
#endif 

point.cpp:

// point.cpp
#include <iostream>
#include "point.h"
using namespace std;

void Point::generate(double a, double b) {
    x = a;
    y = b;
} 

void Point::print() {
    cout << x << endl;
    cout << y << endl;
}

main.cpp:

// main.cpp
#include <iostream>
#include "point.h"
using namespace std;

int main() {
    Point A;
    A.generate(1,2);
    A.print();
    return 0; 
}

每次我编译main.cpp时都会收到错误消息:

main.cpp:(.text+0x30): undefined reference to `Point::generate(double, double)'
main.cpp:(.text+0x3c): undefined reference to `Point::print()'
[Error] ld returned 1 exit status

我做错了什么?希望得到答案;)。

问候, 杰拉尔德

0 个答案:

没有答案