我的问题是关于硬件分配。我试过谷歌,联系我的老师(从未回复),没有解决方案。
基本上我需要重载一个在头文件中有两个参数的函数。
它与此非常相似 - > Operator(s) too many parameters for this function?
但由于某种原因,当我添加一个朋友时,它不会编译。
这是我需要重载的一个功能。
Rational.cpp
bool reduceFractions(numbers &fractions) {
bool answer;
for (int i = fractions.numerator; i >= 2; i--) { //This will keep running until i = the GFC for both numerator & denominator
if ((((fractions.numerator % i) == 0) && (fractions.denominator % i) == 0)) {
fractions.numerator /= i;
fractions.denominator /= i;
answer = true;
}
}
answer = false;
return answer;
}
//Adding fractions
numbers operator+(numbers firstFraction, numbers secondFraction) {
numbers holdFraction1, holdFraction2, someAnswers;
//Multiply to get same denominator
holdFraction1.denominator = firstFraction.denominator * secondFraction.denominator;
holdFraction2.denominator = firstFraction.denominator * secondFraction.denominator;
//Numerator multiplication to match new denominator
holdFraction1.numerator = firstFraction.numerator * secondFraction.denominator;
holdFraction2.numerator = secondFraction.numerator * firstFraction.denominator;
//Adding the fractions
someAnswers.numerator = holdFraction1.numerator + holdFraction2.numerator;
someAnswers.denominator = holdFraction2.denominator;
//Reduce fractions
while (reduceFractions(someAnswers));
return someAnswers;
}
istream& operator >> (istream &in, numbers &fraction) {
char slash;
return in >> fraction.numerator >> slash >> fraction.denominator;
}
ostream& operator << (ostream& out, numbers fraction) {
return out << fraction.numerator << "/" << fraction.denominator;
}
Rational.h
##ifndef Rational
#define Rational
struct numbers {
int numerator;
int denominator;
};
class Math
{
private:
numbers first, second;
public:
Math();
friend numbers operator+(numbers firstFraction, numbers secondFraction);
friend istream& operator >>(istream& in, numbers& fraction);
friend ostream& operator<< (ostream& out, numbers fraction);
};
#endif
Driver.cpp 主要在这里
#include <iostream>
using namespace std;
#include "Rational.h"
int main() {
numbers firstFraction, secondFraction, answerFraction;
cout << "Enter first fraction (format a/b)";
cin >> firstFraction;
cout << "Enter second fraction (format a/b)";
cin >> secondFraction;
answerFraction = firstFraction + secondFraction;
cout << answerFraction;
system("Pause");
return 0;
}
^^^ 似乎没有重载该功能。给我一个编译错误。
我得到了这个和其他几个错误
Error 1 error LNK2005: "struct numbers __cdecl operator+(struct numbers &,struct numbers &)" (??H@YA?AUnumbers@@AAU0@0@Z) already defined in Driver.obj C:\Users\JeanLuis\Documents\Visual Studio 2013\Projects\Project1\Project1\Rational.obj Project1
如果我拿出朋友 它告诉我参数太多了......
究竟我做错了什么?
希望我能说清楚一点。
答案 0 :(得分:0)
所以,我猜测它是因为你的签名不匹配。
你有:
friend numbers operator+(numbers firstFraction, numbers secondFraction);
friend istream& operator >>(istream& in, numbers& fraction);
friend ostream& operator<< (ostream& out, numbers fraction);
但你的实际功能是:
numbers operator+(numbers &firstFraction, numbers &secondFraction) // BAD
istream& operator >> (istream &in, numbers &fraction) // GOOD
ostream& operator << (ostream& out, numbers &fraction) // BAD
注意参考运营商如何匹配?修复它,看看是否可以编译。像显示的那样奇怪的错综复杂的错误通常是由签名不匹配引起的。
与评论相关的修改
.h添加
#include <iostream>
using namespace std;
.cpp添加
#include "test2.h"
#include <iostream>
using namespace std;