我在这里收到链接错误,为什么会这样?

时间:2014-10-04 02:27:32

标签: c++ debugging overloading

我相信我的所有代码都是正确的,但如果您发现任何错误,请告诉我。我收到了这个链接错误。

该程序操纵分数并测试重载方法。 感谢任何有助于提前帮助的人。

1>------ Build started: Project: hw05, Configuration: Debug Win32 ------
1>  main.cpp
1>main.obj : error LNK2005: "public: __thiscall Fraction::Fraction(int,int)" (??0Fraction@@QAE@HH@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Fraction &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVFraction@@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Fraction const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVFraction@@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator==(class Fraction const &)const " (??8Fraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator!=(class Fraction const &)const " (??9Fraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: class Fraction __thiscall Fraction::operator*(class Fraction const &)const " (??DFraction@@QBE?AV0@ABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: class Fraction __thiscall Fraction::operator-(class Fraction const &)const " (??GFraction@@QBE?AV0@ABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: class Fraction __thiscall Fraction::operator+(class Fraction const &)const " (??HFraction@@QBE?AV0@ABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: class Fraction __thiscall Fraction::operator/(class Fraction const &)const " (??KFraction@@QBE?AV0@ABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator<(class Fraction const &)const " (??MFraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator<=(class Fraction const &)const " (??NFraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator>(class Fraction const &)const " (??OFraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: bool __thiscall Fraction::operator>=(class Fraction const &)const " (??PFraction@@QBE_NABV0@@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: int __thiscall Fraction::getDenominator(void)const " (?getDenominator@Fraction@@QBEHXZ) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: int __thiscall Fraction::getNumerator(void)const " (?getNumerator@Fraction@@QBEHXZ) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: void __thiscall Fraction::reduce(void)" (?reduce@Fraction@@QAEXXZ) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: double __thiscall Fraction::returnDecimal(void)const " (?returnDecimal@Fraction@@QBENXZ) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: void __thiscall Fraction::setDenominator(int)" (?setDenominator@Fraction@@QAEXH@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: void __thiscall Fraction::setNumerator(int)" (?setNumerator@Fraction@@QAEXH@Z) already defined in Fraction.obj
1>main.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Fraction::toString(void)const " (?toString@Fraction@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Fraction.obj
1>Fraction.obj : error LNK2019: unresolved external symbol "private: int __thiscall Fraction::gcd(void)const " (?gcd@Fraction@@ABEHXZ) referenced in function "public: void __thiscall Fraction::reduce(void)" (?reduce@Fraction@@QAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "private: int __thiscall Fraction::gcd(void)const " (?gcd@Fraction@@ABEHXZ)
1>C:\Users\Matt\Documents\SIUE COMPUTER SCIENCE\CS 240 Projects\ConsoleApplication2\Debug\hw05.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我的课程。

的main.cpp

#include <iostream>
#include "Fraction.h"
#include "string"
 using namespace std;

int main()
{
    Fraction temp;

Fraction tempTwo;

Fraction tempThree;

cout << "Please enter the first fraction. [numerator denominator]: ";

cin >> temp;

cout << "You entered: " << temp << endl << endl;

cout << "Please enter the second fraction. [numerator denominator]: ";

cin >> tempTwo;

cout << "You entered: " << tempTwo << endl << endl;

cout << "f1 = " << temp << ", f2 = " << tempTwo << endl;

tempThree = (temp + tempTwo);

cout << "f1 + f2 = " << tempThree << " = ";

tempThree.reduce();

cout << tempThree << " = " << tempThree.returnDecimal() << endl;

tempThree = (temp - tempTwo);

cout << "f1 - f2 = " << tempThree << " = ";

tempThree.reduce();

cout << tempThree << " = " << tempThree.returnDecimal() << endl;

tempThree = (temp * tempTwo);

cout << "f1 * f2 = " << tempThree << " = ";

tempThree.reduce();

cout << tempThree << " = " << tempThree.returnDecimal() << endl;

tempThree = (temp / tempTwo);

cout << "f1 / f2 = " << tempThree << " = ";

tempThree.reduce();

cout << tempThree << " = " << tempThree.returnDecimal() << endl << endl;



return 0;


}

Fraction.cpp

#include "Fraction.h"

    #include <iostream>

    using namespace std;

    Fraction::Fraction(const int numerator, const int denominator) {  }

    int Fraction::getNumerator() const {
        return numerator;
    }

    int Fraction::getDenominator() const {
        return denominator;
    }



    void Fraction::setNumerator(const int numerator) {
        this->numerator = numerator;
    }

    void Fraction::setDenominator(const int denominator) {
        this->denominator = denominator;
    }

    string Fraction::toString() const {
        return "";
    }

    int Fraction::gcd() const { 
     int n = numerator; 
     int d = denominator; 
    int tmp; 

     while (d != 0) { 
     tmp = n % d; 
     n = d; 
     d = tmp; 
     } 
     return n; 
    }// end gcd()

    double Fraction::returnDecimal() const {
        double a = (double) getNumerator();
        double b = (double) getDenominator();

        return (a / b);
    }

    void Fraction::reduce()  {

        int a = gcd();
        int b = numerator / a;
        int c = denominator / a;

        setNumerator(b);
        setDenominator(c);

    }

    Fraction Fraction::operator +(const Fraction& f) const {

        Fraction temp;

        int a = getNumerator();
        int b = getDenominator();
        int c = f.getNumerator();
        int d = f.getDenominator();

        int tempOne = b;
        int tempTwo = d;

        a = a * tempTwo;
        b = b * tempTwo;

        c = c * tempOne;
        d = d * tempOne;

        temp.setNumerator(a+c);
        temp.setDenominator(d);

    return temp;

    }

    Fraction Fraction::operator -(const Fraction& f) const {

    Fraction temp;

    int a = getNumerator();
    int b = getDenominator();
    int c = f.getNumerator();
    int d = f.getDenominator();

    int tempOne = b;
    int tempTwo = d;

    a = a * tempTwo;
    b = b * tempTwo;

    c = c * tempOne;
    d = d * tempOne;

    temp.setNumerator(a-c);
    temp.setDenominator(d);

    return temp;

    }

    Fraction Fraction::operator *(const Fraction& f) const {

    Fraction temp;

    temp.setNumerator(getNumerator() * f.getNumerator());
    temp.setDenominator(getDenominator() * f.getDenominator());

    return temp;

    }

    Fraction Fraction::operator /(const Fraction& f) const {

    Fraction temp;

    temp.setNumerator(getNumerator() * f.getDenominator());
    temp.setDenominator(getDenominator() * f.getNumerator());

    return temp;

    }

    bool Fraction::operator ==(const Fraction& f) const {

    return ( (getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator()) );

    }

    bool Fraction::operator !=(const Fraction& f) const {

    return !( (getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator()) );

    }

    bool Fraction::operator <(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) < (c/d));

    }

    bool Fraction::operator <=(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) <= (c/d));

    }

    bool Fraction::operator >(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) > (c/d));

    }

    bool Fraction::operator >=(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) >= (c/d));

    }

    ostream& operator <<(ostream& out, const Fraction& f) {
    out << f.getNumerator() << "/" << f.getDenominator();

    return out;
    }

    istream& operator >>(istream& in, Fraction& f) {    

    int a;
    int b;

    in >> a >> b;

    f.setNumerator(a);
    f.setDenominator(b);

    return in;

    }

Fraction.h

#ifndef __hw05__Fraction__
#define __hw05__Fraction__


#include <iostream>
    #include <string>

    using namespace std;
     class Fraction {

    private:

    int numerator;

    int denominator;

    int gcd() const;

    public:

    Fraction(const int numerator = 0, const int denominator = 0);

    int getNumerator() const;

    int getDenominator() const;

    void setNumerator(const int numerator);

    void setDenominator(const int denominator);

    string toString() const;

    double returnDecimal() const;

    void reduce();

    bool operator ==(const Fraction& f) const;

    bool operator !=(const Fraction& f) const;

    bool operator <(const Fraction& f) const;

    bool operator <=(const Fraction& f) const;

    bool operator >(const Fraction& f) const;

    bool operator >=(const Fraction& f) const;

    Fraction operator +(const Fraction& f) const;

    Fraction operator -(const Fraction& f) const;

    Fraction operator *(const Fraction& f) const;

    Fraction operator /(const Fraction& f) const;

    friend ostream& operator <<(ostream&, const Fraction&);

    friend istream& operator >>(istream&, Fraction&);

    };

    #endif /* d

4 个答案:

答案 0 :(得分:2)

main.cpp

#include "Fraction.cpp"

不要include源文件。 Include个头文件(例如Fraction.h)。

修改

您似乎编写了大量代码而未对其进行任何测试。这就是为什么你被迫一次修复一个错误的原因,并希望你没有把所有的时间花在一个结果是不可行的方法上。

从小而简单开始,一次添加一点复杂性,在每一步测试,单独开发新功能,永远不会添加到无法正常工作的代码。

答案 1 :(得分:2)

Fraction.cpp单独编译并稍后链接。要使用main.cpp中的成员,您需要Fraction.h。 所以只需删除该行:

#include Fraction.cpp   你应该没事。

答案 2 :(得分:0)

你的main.cpp包含了fraction.cpp。因此,fraction.cpp的代码在生成的二进制文件中出现两次,这解释了&#34;已定义的&#34;消息。

答案 3 :(得分:0)

你的函数int Fraction :: gcd()const;在Fraction.h中声明,但未定义。