第一次在这里发布海报,但现在已经潜伏了几个月。目前沉迷于 C++,并从学校以前的 Comp Sci 课程中获得了少量的 Java 知识。如果你们中的一些人看到这个并感到失望,我深表歉意,因为已经存在关于运算符重载的问题,但是我无法将我的程序与我在这里找到的内容完全不同的问题放在一起。
这个问题尤其与 + & = 运算符有关。我已经在课堂上完成了几个涉及这两种重载的程序,而且我似乎没有任何困难使程序正常运行;然而,这些先前的程序和我目前的程序肯定有一些明显的区别,我没有看到这超出了我对每个运算符重载如何工作的理解。
错误消息本身使问题看起来几乎太容易修复,因为它显然只是一个操作数匹配问题,但我仍然无法找到正确的语法来纠正这些错误。它们如下:
"1>...\grademain.cpp(19): error C2679: binary '=' : no operator found which takes a right-hand 1>operand of type 'const char [12]' (or there is no acceptable conversion)
1>...\grade.h(39): could be 'Grade &Grade::operator =(const Grade &)'
1> while trying to match the argument list '(Grade, const char [12])' "
和
"1>...\grademain.cpp(26): error C2679: binary '+' : no operator found which takes a right-hand 1>operand of type 'int' (or there is no acceptable conversion)
1>...\grade.h(40): could be 'Grade Grade::operator +(const Grade &)'
1>\grade.h(16): or 'int operator +(const Grade &)'
1> while trying to match the argument list '(Grade, int)' "
代码如下:
GradeMain.cpp:
#include <iostream>
#include <string>
#include "Grade.h"
using namespace std;
int main(void)
{
Grade student1("Tom Smith", 90); //declare initialized object
cout << "First: " << student1 << endl;
Grade student2;
student2 = "Bill Miller"; //uses conversion constructor // ERROR # 1
cout << "Second: " << student2 << endl;
Grade student3;
student3 = student1;
cout << "Third: " << student3 << endl;
int adjusted_grade = student1 + 4; // ERROR # 2
cout << "adjusted grade of first by 4 points gives " << adjusted_grade << endl;
//test equality operator
if (student1 == student2)
cout << "\nerror - students should not be equal\n";
else
cout << "\nstudent 1 is not equal to student 2\n";
if (student1 == student3)
cout << "\nstudent 1 is equal to student 3\n";
else
cout << "\nerror - student 1 should be equal to student 3\n";
system("pause");
return 0;
}
等级.cpp:
#include <iostream>
#include <string>
#include "Grade.h"
// Copy Assignment
Grade &Grade::operator=(const Grade &temp)
{
name = temp.name;
grade = temp.grade;
return *this;
}
// Addition Operator Overload
Grade Grade::operator+(const Grade &temp)
{
Grade temp1;
temp1.grade = grade + temp.grade;
return *this;
}
// Output Operator Overload
ostream& operator<<(ostream &os, const Grade &p)
{
os << p.name << " " << p.grade;
return os;
}
// Input Operator Overload
istream& operator>>(istream &in, const Grade &p)
{
// No purpose for program -- User does not input anything
return in;
}
// Comparison Operator Overload
bool Grade::operator==(const Grade &temp)
{
if(grade == temp.grade)
return true;
else
return false;
}
等级.h:
using namespace std;
class Grade
{
// Friend Function Prototypes
friend ostream& operator<<(ostream& , const Grade&);
friend istream& operator>>(istream& , const Grade&); // Not necessary -- deleting
friend bool operator==(const Grade&, const Grade&);
friend int operator+(const Grade&);
private:
string name;
int grade;
public:
// Default Constructor
Grade();
// Constructor
Grade(string studentName, int studentGrade)
{
name = studentName;
grade = studentGrade;
}
// Copy Constructor
Grade(const Grade &obj)
{
name = obj.name;
grade = obj.grade;
}
// Operator Overloads
Grade& operator=(const Grade &); // Copy Assignment
Grade operator+(const Grade &);
Grade& operator<<(const Grade &);
Grade& operator>>(const Grade &);
bool operator==(const Grade &);
};
我对 operator= 问题的部分困惑是,当我将 student3 分配给 student1 时,没有问题,但是当我简单地分配 student2 = "Bill Miller" 时,它遇到了操作数问题。我原以为这只会创建 student2("Bill Miller", 0) 的对象,但我一定是在某个地方错了。我只能想象,在根据当前分配的成绩分配新变量时,无论什么纠正这一点,都会纠正更新student1的成绩的操作数问题。有些东西告诉我,这可能只是函数目前的结构方式,可能 operator+ 重载需要类似于“int Grade::operator+(const Grade &)”而不是“Grade Grade:operator+(const年级 &)”,
您可以指出我的任何方向以更好地了解如何摆脱我所处的情况,这绝对是极好的,只是不要直接给我答案(当然)!鉴于这是课堂作业,任何建议都应仅与 Grade.cpp 和 Grade.h 有关,因为 GradeMain 由讲师提供。
非常感谢阅读:)