运算符重载=,* =。(Operator overloading =, *=. How to make this work?)

主要有这些行的问题:

*标签[1] = TEST1; *标签[4] = TEST2; 它只是增加了颜色,变量(a,b,h)保持不变。 我正在尝试这样的事情

cuboid operator=(const cuboid & base) { return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_) }

但是这似乎并不奏效

下一个是这样的:

*选项卡[4] * = 2;

这个方法有一个重载的运算符,当我运行这个时会发生一些错误。 与操作符* =不匹配。 操作数类型是figure和int。

最后一个是:* tab [2] =“明亮”+ * tab [2]; 我认为我需要一个新的构造函数,但我在哪里创建一个?

感谢您的任何答案!

#include <iostream> #include <cstdio> #include <cstring> #include <math.h> using namespace std; class figure { string * colour_; public: figure(): colour_(new string("Empty")) {} figure(const string & colour): colour_(new string(colour)) {} figure(const figure & base) { colour_=new string(*base.colour_); } virtual ~figure() {delete colour_;} string & colour () const {return *colour_;} virtual double area () =0; virtual void print(ostream& where) const { where << "Colour: " << colour() << " "; } friend ostream & operator <<(ostream &os, const figure & base) { base.print(os); return os; } figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } } }; class circle :public figure { int r_; public: circle() : r_(0) {} circle(const string & colour,const int r) : figure(colour), r_(r) {} double area() { return M_PI*r_*r_; } const int & radius() const {return r_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Radius: " << radius() << " "; } circle & operator=(const circle & base) { r_=base.r_; figure::operator=(base); return *this; } }; class rectangle : public figure { int a_; int b_; public: static int ObjectCount_; rectangle() : a_(0), b_(0) {++ObjectCount_;} rectangle(const string & colour, const int a, const int b) : figure(colour),a_(a), b_(b) {++ObjectCount_;} ~rectangle() {--ObjectCount_;} double area() { return a_*b_; } const int & valueA () const {return a_;} const int & valueB () const {return b_;} int & changeA() {return a_;} int & changeB() {return b_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; } rectangle & operator=(const rectangle & base) { a_=base.a_; b_=base.b_; return *this; } static int & ObjectCount() {return ObjectCount_; } }; class cuboid :public rectangle { int h_; public: cuboid() : h_(0) {} cuboid(const string & colour, const int a, const int b, const int h) : rectangle(colour,a,b), h_(h) {} double area() { return 2*valueA()*valueB()+2*valueB()*h_+2*valueA()*h_; } void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; where << "Height: " << h_ << " "; } cuboid & operator=(const cuboid & base) { figure::operator=(base); rectangle::operator=(base); h_=base.h_; return *this; } cuboid & operator*=(const int number) { h_*=number; changeA()*=number; changeB()*=number; return *this; } }; int rectangle::ObjectCount_=0; int main() { figure * tab[5]; const circle test1("black",100); const cuboid test2("grey", 2,2,2); tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid; for(unsigned i=0; i<5;++i) cout << tab[i]->area() << endl; for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "***********************" << endl; *tab[1]=test1; // it just assigns a colour, rest stays the same *tab[4]=test2; // same here /* *tab[2] = "bright" + *tab[2]; //????? */ //*tab[4]*=2; //some error, no idea for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "$ " << rectangle::ObjectCount() << endl; for(int i=0; i<5; i++) delete tab[i]; cout << "$ " << rectangle::ObjectCount() << endl; }

I have problem with those lines in main:

*tab[1]=test1; *tab[4]=test2; It just adds colour, and variables (a,b,h) stay the same. I was trying something like this

cuboid operator=(const cuboid & base) { return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_) }

but this doesn't seem to be working either

next one is this:

*tab[4] * =2;

There is overloaded operator for this method and when I run this there occures some error. No match for operator*=. Operand types are figure and int.

The last one is: *tab[2] = "bright" + *tab[2]; I think that I need a new constructor for this, but where do I make one?

Thanks for any answer !!

#include <iostream> #include <cstdio> #include <cstring> #include <math.h> using namespace std; class figure { string * colour_; public: figure(): colour_(new string("Empty")) {} figure(const string & colour): colour_(new string(colour)) {} figure(const figure & base) { colour_=new string(*base.colour_); } virtual ~figure() {delete colour_;} string & colour () const {return *colour_;} virtual double area () =0; virtual void print(ostream& where) const { where << "Colour: " << colour() << " "; } friend ostream & operator <<(ostream &os, const figure & base) { base.print(os); return os; } figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } } }; class circle :public figure { int r_; public: circle() : r_(0) {} circle(const string & colour,const int r) : figure(colour), r_(r) {} double area() { return M_PI*r_*r_; } const int & radius() const {return r_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Radius: " << radius() << " "; } circle & operator=(const circle & base) { r_=base.r_; figure::operator=(base); return *this; } }; class rectangle : public figure { int a_; int b_; public: static int ObjectCount_; rectangle() : a_(0), b_(0) {++ObjectCount_;} rectangle(const string & colour, const int a, const int b) : figure(colour),a_(a), b_(b) {++ObjectCount_;} ~rectangle() {--ObjectCount_;} double area() { return a_*b_; } const int & valueA () const {return a_;} const int & valueB () const {return b_;} int & changeA() {return a_;} int & changeB() {return b_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; } rectangle & operator=(const rectangle & base) { a_=base.a_; b_=base.b_; return *this; } static int & ObjectCount() {return ObjectCount_; } }; class cuboid :public rectangle { int h_; public: cuboid() : h_(0) {} cuboid(const string & colour, const int a, const int b, const int h) : rectangle(colour,a,b), h_(h) {} double area() { return 2*valueA()*valueB()+2*valueB()*h_+2*valueA()*h_; } void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; where << "Height: " << h_ << " "; } cuboid & operator=(const cuboid & base) { figure::operator=(base); rectangle::operator=(base); h_=base.h_; return *this; } cuboid & operator*=(const int number) { h_*=number; changeA()*=number; changeB()*=number; return *this; } }; int rectangle::ObjectCount_=0; int main() { figure * tab[5]; const circle test1("black",100); const cuboid test2("grey", 2,2,2); tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid; for(unsigned i=0; i<5;++i) cout << tab[i]->area() << endl; for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "***********************" << endl; *tab[1]=test1; // it just assigns a colour, rest stays the same *tab[4]=test2; // same here /* *tab[2] = "bright" + *tab[2]; //????? */ //*tab[4]*=2; //some error, no idea for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "$ " << rectangle::ObjectCount() << endl; for(int i=0; i<5; i++) delete tab[i]; cout << "$ " << rectangle::ObjectCount() << endl; }

最满意答案

你正在定义figure* tab[5]组图figure* tab[5] ,所以你做了一个assigments,你正在将所有指向你的对象的指针变成figure* :

tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid;

上figure只有这个分配运算符:

figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } }

那么,你在做什么:

*tab[1]=test1; *tab[4]=test2;

你正在从班级人物中调用该分配操作员。

与operator *=相同。 上层figure就是没有。 这就是为什么你会得到错误。

You are defining array figure* tab[5], so then you make an assigments you are casting all pointers to your objects to figure*:

tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid;

Class figure have only this assigment operator:

figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } }

So, then you are doing:

*tab[1]=test1; *tab[4]=test2;

you are calling that assigment operator from class figure.

Same with operator *=. class figure just doesn't have it. Thats why you are getting error.

运算符重载=,* =。(Operator overloading =, *=. How to make this work?)

主要有这些行的问题:

*标签[1] = TEST1; *标签[4] = TEST2; 它只是增加了颜色,变量(a,b,h)保持不变。 我正在尝试这样的事情

cuboid operator=(const cuboid & base) { return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_) }

但是这似乎并不奏效

下一个是这样的:

*选项卡[4] * = 2;

这个方法有一个重载的运算符,当我运行这个时会发生一些错误。 与操作符* =不匹配。 操作数类型是figure和int。

最后一个是:* tab [2] =“明亮”+ * tab [2]; 我认为我需要一个新的构造函数,但我在哪里创建一个?

感谢您的任何答案!

#include <iostream> #include <cstdio> #include <cstring> #include <math.h> using namespace std; class figure { string * colour_; public: figure(): colour_(new string("Empty")) {} figure(const string & colour): colour_(new string(colour)) {} figure(const figure & base) { colour_=new string(*base.colour_); } virtual ~figure() {delete colour_;} string & colour () const {return *colour_;} virtual double area () =0; virtual void print(ostream& where) const { where << "Colour: " << colour() << " "; } friend ostream & operator <<(ostream &os, const figure & base) { base.print(os); return os; } figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } } }; class circle :public figure { int r_; public: circle() : r_(0) {} circle(const string & colour,const int r) : figure(colour), r_(r) {} double area() { return M_PI*r_*r_; } const int & radius() const {return r_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Radius: " << radius() << " "; } circle & operator=(const circle & base) { r_=base.r_; figure::operator=(base); return *this; } }; class rectangle : public figure { int a_; int b_; public: static int ObjectCount_; rectangle() : a_(0), b_(0) {++ObjectCount_;} rectangle(const string & colour, const int a, const int b) : figure(colour),a_(a), b_(b) {++ObjectCount_;} ~rectangle() {--ObjectCount_;} double area() { return a_*b_; } const int & valueA () const {return a_;} const int & valueB () const {return b_;} int & changeA() {return a_;} int & changeB() {return b_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; } rectangle & operator=(const rectangle & base) { a_=base.a_; b_=base.b_; return *this; } static int & ObjectCount() {return ObjectCount_; } }; class cuboid :public rectangle { int h_; public: cuboid() : h_(0) {} cuboid(const string & colour, const int a, const int b, const int h) : rectangle(colour,a,b), h_(h) {} double area() { return 2*valueA()*valueB()+2*valueB()*h_+2*valueA()*h_; } void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; where << "Height: " << h_ << " "; } cuboid & operator=(const cuboid & base) { figure::operator=(base); rectangle::operator=(base); h_=base.h_; return *this; } cuboid & operator*=(const int number) { h_*=number; changeA()*=number; changeB()*=number; return *this; } }; int rectangle::ObjectCount_=0; int main() { figure * tab[5]; const circle test1("black",100); const cuboid test2("grey", 2,2,2); tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid; for(unsigned i=0; i<5;++i) cout << tab[i]->area() << endl; for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "***********************" << endl; *tab[1]=test1; // it just assigns a colour, rest stays the same *tab[4]=test2; // same here /* *tab[2] = "bright" + *tab[2]; //????? */ //*tab[4]*=2; //some error, no idea for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "$ " << rectangle::ObjectCount() << endl; for(int i=0; i<5; i++) delete tab[i]; cout << "$ " << rectangle::ObjectCount() << endl; }

I have problem with those lines in main:

*tab[1]=test1; *tab[4]=test2; It just adds colour, and variables (a,b,h) stay the same. I was trying something like this

cuboid operator=(const cuboid & base) { return cuboid(base.colour(), base.valueA(), base.valueB(),base.h_) }

but this doesn't seem to be working either

next one is this:

*tab[4] * =2;

There is overloaded operator for this method and when I run this there occures some error. No match for operator*=. Operand types are figure and int.

The last one is: *tab[2] = "bright" + *tab[2]; I think that I need a new constructor for this, but where do I make one?

Thanks for any answer !!

#include <iostream> #include <cstdio> #include <cstring> #include <math.h> using namespace std; class figure { string * colour_; public: figure(): colour_(new string("Empty")) {} figure(const string & colour): colour_(new string(colour)) {} figure(const figure & base) { colour_=new string(*base.colour_); } virtual ~figure() {delete colour_;} string & colour () const {return *colour_;} virtual double area () =0; virtual void print(ostream& where) const { where << "Colour: " << colour() << " "; } friend ostream & operator <<(ostream &os, const figure & base) { base.print(os); return os; } figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } } }; class circle :public figure { int r_; public: circle() : r_(0) {} circle(const string & colour,const int r) : figure(colour), r_(r) {} double area() { return M_PI*r_*r_; } const int & radius() const {return r_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Radius: " << radius() << " "; } circle & operator=(const circle & base) { r_=base.r_; figure::operator=(base); return *this; } }; class rectangle : public figure { int a_; int b_; public: static int ObjectCount_; rectangle() : a_(0), b_(0) {++ObjectCount_;} rectangle(const string & colour, const int a, const int b) : figure(colour),a_(a), b_(b) {++ObjectCount_;} ~rectangle() {--ObjectCount_;} double area() { return a_*b_; } const int & valueA () const {return a_;} const int & valueB () const {return b_;} int & changeA() {return a_;} int & changeB() {return b_;} void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; } rectangle & operator=(const rectangle & base) { a_=base.a_; b_=base.b_; return *this; } static int & ObjectCount() {return ObjectCount_; } }; class cuboid :public rectangle { int h_; public: cuboid() : h_(0) {} cuboid(const string & colour, const int a, const int b, const int h) : rectangle(colour,a,b), h_(h) {} double area() { return 2*valueA()*valueB()+2*valueB()*h_+2*valueA()*h_; } void print(ostream& where) const { where << "Colour: " << colour() << " "; where << "Value A: " << valueA() << " "; where << "Value B: " << valueB() << " "; where << "Height: " << h_ << " "; } cuboid & operator=(const cuboid & base) { figure::operator=(base); rectangle::operator=(base); h_=base.h_; return *this; } cuboid & operator*=(const int number) { h_*=number; changeA()*=number; changeB()*=number; return *this; } }; int rectangle::ObjectCount_=0; int main() { figure * tab[5]; const circle test1("black",100); const cuboid test2("grey", 2,2,2); tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid; for(unsigned i=0; i<5;++i) cout << tab[i]->area() << endl; for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "***********************" << endl; *tab[1]=test1; // it just assigns a colour, rest stays the same *tab[4]=test2; // same here /* *tab[2] = "bright" + *tab[2]; //????? */ //*tab[4]*=2; //some error, no idea for(int i=0; i<5; ++i) cout<<*tab[i]<<tab[i]->area()<<"\n"; cout << "$ " << rectangle::ObjectCount() << endl; for(int i=0; i<5; i++) delete tab[i]; cout << "$ " << rectangle::ObjectCount() << endl; }

最满意答案

你正在定义figure* tab[5]组图figure* tab[5] ,所以你做了一个assigments,你正在将所有指向你的对象的指针变成figure* :

tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid;

上figure只有这个分配运算符:

figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } }

那么,你在做什么:

*tab[1]=test1; *tab[4]=test2;

你正在从班级人物中调用该分配操作员。

与operator *=相同。 上层figure就是没有。 这就是为什么你会得到错误。

You are defining array figure* tab[5], so then you make an assigments you are casting all pointers to your objects to figure*:

tab[0]=new circle("red",1); tab[1]=new circle; tab[2]=new rectangle("blue",1,1); tab[3]=new cuboid("green",1,1,1); tab[4]=new cuboid;

Class figure have only this assigment operator:

figure & operator=(const figure & base) { if(this==&base) return *this; else { colour_=new string(*base.colour_); return *this; } }

So, then you are doing:

*tab[1]=test1; *tab[4]=test2;

you are calling that assigment operator from class figure.

Same with operator *=. class figure just doesn't have it. Thats why you are getting error.