这里有一个链接错误,为什么呢?

时间:2022-09-22 22:53:55

I believe I have all my code correct, but if you see any errors let me know. I'm getting this linking error.

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

This program manipulates fractions and tests overloading methods. Thank you, to anyone that will help in advance.

这个程序控制分数和测试重载方法。谢谢你,任何能提前帮助你的人。

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 ==========

Here are my classes.

这是我的类。

main.cpp

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

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

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 个解决方案

#1


2  

Fraction.cpp compiles separately and gets linked later. To use members from there in main.cpp you need Fraction.h. So just remove the line:

#include Fraction.cpp
and you should be fine.

分数。cpp将单独编译并在稍后进行链接。主要使用成员。你需要cpp Fraction.h。所以只要去掉一行:#include分数。你应该没事的。

#2


2  

In main.cpp:

在main.cpp:

#include "Fraction.cpp"

Don't include source files. Include header files (e.g. Fraction.h).

不包括源文件。包括头文件(例如,Fraction.h)。

EDIT:

编辑:

You seem to have written a lot of code without testing any of it. That's why you're forced to fix bugs one at a time and hope you didn't spend all that time on an approach that turns out to be unworkable.

您似乎编写了很多代码,却没有测试任何代码。这就是为什么你不得不一次修正一个错误,希望你没有把所有的时间都花在一个行不通的方法上。

Start small and simple, add complexity a little at a time, test at every step, develop new functionality in isolation, and never add to code that doesn't work.

开始小而简单,每次增加一点复杂性,每一步都进行测试,孤立地开发新功能,永远不要添加不工作的代码。

#3


0  

Your main.cpp is including the fraction.cpp. Therefore the code of fraction.cpp appears twice in the resulting binary which explain the "already defined" messages.

你的主。cpp包括fraction.cpp。因此分数的代码。cpp在生成的二进制文件中出现两次,它解释了“已定义的”消息。

#4


0  

Your function int Fraction::gcd() const; is declared in Fraction.h, but not defined.

你的函数int分数::gcd() const;中声明的一部分。h,但没有定义。

#1


2  

Fraction.cpp compiles separately and gets linked later. To use members from there in main.cpp you need Fraction.h. So just remove the line:

#include Fraction.cpp
and you should be fine.

分数。cpp将单独编译并在稍后进行链接。主要使用成员。你需要cpp Fraction.h。所以只要去掉一行:#include分数。你应该没事的。

#2


2  

In main.cpp:

在main.cpp:

#include "Fraction.cpp"

Don't include source files. Include header files (e.g. Fraction.h).

不包括源文件。包括头文件(例如,Fraction.h)。

EDIT:

编辑:

You seem to have written a lot of code without testing any of it. That's why you're forced to fix bugs one at a time and hope you didn't spend all that time on an approach that turns out to be unworkable.

您似乎编写了很多代码,却没有测试任何代码。这就是为什么你不得不一次修正一个错误,希望你没有把所有的时间都花在一个行不通的方法上。

Start small and simple, add complexity a little at a time, test at every step, develop new functionality in isolation, and never add to code that doesn't work.

开始小而简单,每次增加一点复杂性,每一步都进行测试,孤立地开发新功能,永远不要添加不工作的代码。

#3


0  

Your main.cpp is including the fraction.cpp. Therefore the code of fraction.cpp appears twice in the resulting binary which explain the "already defined" messages.

你的主。cpp包括fraction.cpp。因此分数的代码。cpp在生成的二进制文件中出现两次,它解释了“已定义的”消息。

#4


0  

Your function int Fraction::gcd() const; is declared in Fraction.h, but not defined.

你的函数int分数::gcd() const;中声明的一部分。h,但没有定义。