【发布时间】:2014-11-07 22:11:15
【问题描述】:
第一次在这里发布海报,但现在已经潜伏了几个月。目前沉迷于 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 &)",但到目前为止,我为找到正确的语法所做的所有努力都是徒劳的。
您可以指出我的任何方向以更好地了解如何摆脱我所处的情况绝对是极好的,只是不要直接给我答案(当然)!由于这是课堂作业,因此任何建议都应仅与 Grade.cpp 和 Grade.h 有关,因为 GradeMain 由讲师提供。
非常感谢您的阅读:)
【问题讨论】:
-
一个
operator+,左边有一个Grade,右边有一个int,返回一个int,看起来像int Grade::operator+(int) const,但最好只实现一个 getter。 -
Grade::operator+应该返回temp1,而不是*this。temp1也应该从*this初始化,以便它获取名称 -
作为家庭作业问题,这还不错,但您可能想解释更多您的推理。例如,为什么您希望零自动作为
Grade(string,int)构造函数的第二个参数给出? -
对于那个特定的例子,我记得在此处阅读了另一个发布的问题,其中有人遇到了类似的错误,并且我记得有人用编译器自动假设值为 0 的情况下做出响应,如果没有的话输入以容纳构造函数。如果我的解释含糊不清,我很抱歉:(
标签: c++ operator-overloading overloading operator-keyword copy-assignment