ここでの初めてのポスターですが、今は数か月潜んでいます。現在、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;
}
Grade.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;
}
Grade.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= の問題に関する私の混乱の一部は、学生 3 を学生 1 に割り当てたときは問題がなかったのに、単に学生 2 = "Bill Miller" を割り当てると、オペランドの問題が発生することです。これは本質的に学生2( "ビルミラー"、0)のオブジェクトを作成するだけだと思っていましたが、どこか間違っているに違いありません。これを修正するものは何でも、現在割り当てられている成績に基づいて新しい変数を割り当てるときに、学生1の成績を更新するというオペランドの問題も修正すると想像できます。関数が現時点でどのように構造化されているか、おそらく operator+ のオーバーロードが "Grade Grade:operator+(const学年 &)"、
私がいる状況から抜け出す方法をよりよく理解するためにあなたが私を指し示すことができる方向は、絶対に優れているでしょう。これはクラスの課題であるため、GradeMain はインストラクターによって提供されるため、提案は Grade.cpp と Grade.h にのみ関係する必要があります。
読んでくれてありがとう:)