Консультация № 163402
27.03.2009, 18:17
0.00 руб.
0 2 1
Здравствуйте. Помогите разобраться пажалуста. я создал класс где перегрузил оператор = и+
и при выражении k=p+v; происходит ошибка как зделать ддопустимым это выражение

Приложение:
class Table
{ public:
Table(){};
Table(string a1,string b1,int c,double d1);

friend ostream& operator <<(ostream& vivod,Table& f);
friend istream& operator >>(istream& vvod, Table& f);
Table operator +(Table& t);
Table operator =(Table& t);
bool operator ==(Table& t);
private:
string a;
string b;
int c;
double d;
};


#pragma argsused
int main(int argc, char* argv[])
{ Table p("a","b",1,2),k,v("c","d",3,4);
k=p+v;
cout<<k;

getch();
return 0;
}
//---------------------------------------------------------------------------
Table:: Table(string a1,string b1,int c1,double d1):a(a1),b(b1),c(c1),d(d1)
{}

ostream& operator <<(ostream& vivod,Table& f)
{cout<<"Famili9 "<<f.a<<" "
<<"Inicial "<<f.b<<" "
<<"God "<<f.c<<" "
<<"Oklad "<<f.d<<"\n";
return vivod;
}

istream& operator >>(istream& vvod, Table& f)
{cout<<"Vvedite Famili9,Inicial,God,Okald ";
cin>>f.a>>f.b>>f.c>>f.d;
return vvod;
}

Table Table:: operator +(Table& t)
{ string h,l;int f;double s;
h=a+t.a;
l=b+t.b;
f=c+t.c;
s=d+t.d;
return Table(h,l,f,s);
}

bool Table:: operator ==(Table& t)
{return (a==t.a && b==t.b && c==t.c && d==t.d);
}

Table Table::operator =(Table& t)
{ a=t.a;
b=t.b;
c=t.c;
d=t.d;
return *this;
}

Обсуждение

давно
Академик
20764
1861
27.03.2009, 18:30
общий
Table operator +(Table const & t);
Table &operator =(Table const & t);
bool operator ==(Table const & t);
Неизвестный
27.03.2009, 18:33
общий
это ответ
Здравствуйте, Lordfoks!
Результат выполнения операции сложения не может быть lvalue поэтому необходимо добавить const в Table operator =(const Table& t); тогда такое выражение будет допустимым.
Кстати, MS VC++ не требует такого соответствия и программа будет компилироваться нормально.
Код:

#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

class Table
{ public:
Table(){};
Table(string a1,string b1,int c,double d1);

friend ostream& operator <<(ostream& vivod,Table& f);
friend istream& operator >>(istream& vvod, Table& f);
Table operator +(Table& t);
Table operator =(const Table& t);
bool operator ==(Table& t);
private:
string a;
string b;
int c;
double d;
};


int main(int argc, char* argv[])
{
Table p("a","b",1,2),k,v("c","d",3,4);
k=p+v;
cout<<k;

getch();
return 0;
}
//---------------------------------------------------------------------------
Table:: Table(string a1,string b1,int c1,double d1):a(a1),b(b1),c(c1),d(d1)
{}

ostream& operator <<(ostream& vivod,Table& f)
{cout<<"Famili9 "<<f.a<<" "
<<"Inicial "<<f.b<<" "
<<"God "<<f.c<<" "
<<"Oklad "<<f.d<<"\n";
return vivod;
}

istream& operator >>(istream& vvod, Table& f)
{cout<<"Vvedite Famili9,Inicial,God,Okald ";
cin>>f.a>>f.b>>f.c>>f.d;
return vvod;
}

Table Table:: operator +(Table& t)
{ string h,l;int f;double s;
h=a+t.a;
l=b+t.b;
f=c+t.c;
s=d+t.d;
return Table(h,l,f,s);
}

bool Table:: operator ==(Table& t)
{return (a==t.a && b==t.b && c==t.c && d==t.d);
}

Table Table::operator =(const Table& t)
{ a=t.a;
b=t.b;
c=t.c;
d=t.d;
return *this;
}
Форма ответа