【问题标题】:Operator overloading "+" & "=" issue -- school asst运算符重载“+”和“=”问题——学校助理
【发布时间】: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,而不是 *thistemp1 也应该从 *this 初始化,以便它获取名称
  • 作为家庭作业问题,这还不错,但您可能想解释更多您的推理。例如,为什么您希望零自动作为Grade(string,int) 构造函数的第二个参数给出?
  • 对于那个特定的例子,我记得在此处阅读了另一个发布的问题,其中有人遇到了类似的错误,并且我记得有人用编译器自动假设值为 0 的情况下做出响应,如果没有的话输入以容纳构造函数。如果我的解释含糊不清,我很抱歉:(

标签: c++ operator-overloading overloading operator-keyword copy-assignment


【解决方案1】:

当你说

student2 = "Bill Miller";

这将涉及两种用户定义的转换:一种从const char *std::string,另一种从std::stringGrade。但是,C++ 只允许进行一次用户定义的自动转换。

此外,您有一个接受std::string 的构造函数,但它也接受一个额外的参数,因此它不能自动用于将std::string 转换为Grade。只有可以使用单个参数调用的构造函数才能用于转换。

其他错误

int adjusted_grade = student1 + 4;

没有采用Gradeintoperator+,并且无法将4 转换为Grade,也无法将Grade 转换为另一种类型可以与添加 int 一起使用。

【讨论】:

  • 第二个用户定义的转换甚至不存在。
  • 感谢您的帮助!我对 C++ 及其所有复杂性还不太了解,所以我什至不知道关于用户定义转换的规则(或者它已经让我忘记了)。所以看起来我可能需要将字符串更改为 12 的 char 数组以用于“Bill Miller”部分,以限制用户定义的转换......如果我理解正确的话。至于第二部分,你引用的那一行是老师期望测试的,所以我可能不得不让他们运行它,看看如果由于转换问题而无法执行,他们期望什么。
  • @Gooeybanana 与其编写转换构造函数,不如考虑提供一个带有std::string 参数的operator= 重载。
  • 所以我将其更改为“string Grade::operator=(string temp)”并设置 name = temp,然后返回 name.(loledits -- 不知道正确的格式 xD) 这最终修复了第一个错误,但在构建时,我收到“1>GradeMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Grade::Grade(void)" (??0Grade@@QAE@XZ) referenced in function _main”当我将添加和比较部分注释掉以查看它是否会正确输出时出现错误=/
  • @Gooeybanana 不,不要替换您的复制分配运算符。这将是一个单独的过载。而您的链接器错误是因为您实际上没有定义 Grade 的默认构造函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2010-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多