1

我已经搜索(并找到了)这个错误的主题,但还不能将它们应用于我的具体情况。所以这里是:

理性.h

#include <iostream>
class Rational{
public:
    Rational(int a = 0, int b = 1);
    Rational(const Rational &number);
    ~Rational();

    static Rational add(const Rational &a, const Rational &b);
    static Rational sub(const Rational &a, const Rational &b);
    static Rational mult(const Rational &a, const Rational &b);
    static Rational div(const Rational &a, const Rational &b);

    void reduce(Rational a);

    int get_nom() const;
    int get_denom() const;
    void set_nom(int a);
    void set_denom(int b);

    void printOut();

private:
    int nom;
    int denom;

    int greatCommonDiv(int a, int b);
};

理性.cpp

#include <iostream>

class Rational{
public:
    Rational(int a = 0, int b = 1):
        nom(a), denom(b){
    }
    Rational(const Rational &number):
        nom(number.get_nom()), denom(number.get_denom()){
    }
    ~Rational(){
    }

    static Rational add(const Rational &a, const Rational &b){
        Rational sum( ((a.get_nom() * b.get_denom()) + (a.get_denom() * b.get_denom())), (a.get_denom() * b.get_denom()) );
        sum.reduce();
        return sum;
    }
    static Rational sub(const Rational &a, const Rational &b){
        Rational diff( ((a.get_nom() * b.get_denom()) - (a.get_denom() * b.get_denom())), (a.get_denom() * b.get_denom()) );
        diff.reduce();
        return diff;
    }
    static Rational mult(const Rational &a, const Rational &b){
        Rational product(a.get_nom() * b.get_nom(), a.get_denom() * b.get_denom());
        product.reduce();
        return product;
    }
    static Rational div(const Rational &a, const Rational &b){
        Rational quotient(a.get_nom() * b.get_denom(), a.get_denom() * b.get_nom());
        quotient.reduce();
        return quotient;
    }
    void reduce(){
        int ggT = greatCommonDiv(nom, denom);
        nom = nom / ggT;
        denom = denom / ggT;
    }

    int get_nom() const { return nom; }
    int get_denom() const { return denom; }
    void set_nom(int a){ nom = a; }
    void set_denom(int b){ denom = b; }

    void printOut(){
        std::cout << nom << "/" << denom << std::endl;
        return;
    }

private:
    int nom;
    int denom;

    int greatCommonDiv(int a, int b){           
        if(b == 0)
            return a;
        else return greatCommonDiv(b, a % b);
    }
};

源.cpp

#include <iostream>
#include <Rational.h>

int main(){
Rational a(5,3);
a.printOut();
}

MSVS 给了我 3 个错误:

1>Source.obj:错误LNK2019:函数_main中引用的未解析外部符号“public:__thiscall Rational::Rational(int,int)”(??0Rational@@QAE@HH@Z)

1>Source.obj:错误LNK2019:函数_main中引用的未解析外部符号“public:__thiscall Rational::~Rational(void)”(??1Rational@@QAE@XZ)

1>Source.obj : 错误 LNK2019: 函数 _main 中引用的无法解析的外部符号“public: static void __cdecl Rational::printOut(void)” (?printOut@Rational@@SAXXZ)

我不知道为什么会发生这种情况,因为我很确定他可以在正确的位置找到 .h 和 .cpp 文件。

4

1 回答 1

2

所以你自己解决了你的链接器问题。关于“类类型重新定义”:

编译器是对的。您可以/应该/必须只定义一次类,就像您在头文件中所做的那样。cpp 文件应如下所示:

#include "Rational.h"

Rational Rational::add(const Rational &a, const Rational &b){
    Rational sum( ((a.get_nom() * b.get_denom()) + (a.get_denom() * b.get_denom())), (a.get_denom() * b.get_denom()) );
    sum.reduce();
    return sum;
}

...

你得到了基本的想法:在头文件中没有定义(只声明)的东西,你在 cpp.xml 中定义。总是以Classname::. 您在标题中定义的内容(例如您的构造函数)不必再次定义。

于 2013-08-06T09:21:30.257 回答