关于运算符重载的问题<< [英] problem regarding overloading of operator <<.
问题描述
大家好,
我已经超载了<< operator.as如下所示。
ostream&运算符<<(ostream& out,const student& a)
{
out<< a.idno;
out< ;< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks<<结束;
退出;
}
和
ostream& operator<<(ostream& out,string1& s1)
{
char * p;
p = s1.c_str() ;
out<< p;
退出;
}
这里string1是字符串类i已经创建并且学生是
班级学生
{
public:
string1 idno;
string1 name;
int marks;
};
现在我收到此错误
否匹配`std :: basic_ostream< char,std :: char_traits< char>
hi guys,
i have overloaded the << operator.as shown below.
ostream& operator<<(ostream &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostream &out,string1 &s1)
{
char *p;
p=s1.c_str();
out<<p;
return out;
}
here string1 is the string class i have created and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ostream<char, std::char_traits<char>
>& << const string1&''operator
>& << const string1&'' operator
行
out<< a.idno;
重载运算符<<对于string1类正在为其他
实现。
请向我解释这个错误。
提前感谢你。
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.
kindly explain me this error.
thanking you in advance.
推荐答案
ostream&运算符<<(ostream& out,const student& a)
ostream& operator<<(ostream &out, const student &a)
您接受引用为const。
You are accepting the reference as const .
ostream& operator<(ostream& out,string1& s1)
ostream& operator<<(ostream &out,string1 &s1)
这只是参考。
this is just reference.
out<< a.idno;
out<<a.idno;
As" a"是const,这个调用不适用于重载运算符
接受对string1的非const引用。
使用this = ostream& operator<<(ostream& out,const string1& s1)
它应该可以工作。
问候,
阿米尔
As "a" is const, this call is not working with overloaded operator
accepting non-const reference to string1.
Use this =ostream& operator<<(ostream &out,const string1 &s1)
It should work.
Regards,
Amir
Ashwin写道:
Ashwin wrote:
嗨伙计们,
i重载了<< operator.as如下所示。
ostream&运算符<<(ostream& out,const student& a)
{
out<< a.idno;
out< ;< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks<<结束;
退出;
}
和
ostream&运算符<<(ostream& out,string1& s1)
hi guys,
i have overloaded the << operator.as shown below.
ostream& operator<<(ostream &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostream &out,string1 &s1)
这应该是:
stream& operator<<(ostream& out,string1 const& s1)
This should read:
stream& operator<<(ostream &out,string1 const &s1)
{
char * p;
p = s1.c_str();
out<< p;
退出;
}
{
char *p;
p=s1.c_str();
out<<p;
return out;
}
你的字符串类是否故意在字符串中不允许嵌入0
字符?
Is it intentional that your string class does not allow for embedded 0
characters within strings?
这里string1是我创建的字符串类
here string1 is the string class i have created
为什么要创建自己的字符串类?通常,这是一个BadIdea(tm),
我倾向于打赌你的情况也属于那个类别。
Why did you create your own string class? Usually, this is a BadIdea(tm),
and I am inclined to bet that your case is in that category, too.
和学生是
班学生
{
public:
string1 idno;
string1 name;
int marks;
};
现在我收到此错误
不匹配`std :: basic_ostream< char,std :: char_traits< char>
and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ostream<char, std::char_traits<char>
>& << const string1&''operator
>& << const string1&'' operator
行
out<< a.idno;
重载运算符<< for string1类正在为其他
实现。
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.
其他哪个实现?
ps:只需使用std :: string。
Best
Kai-Uwe Bux
Which other implementation?
ps.: just use std::string.
Best
Kai-Uwe Bux
am ****** @ yahoo.com 写道:
am******@yahoo.com wrote:
Hi,
ostream&运算符<<(ostream& out,const student& a)
ostream& operator<<(ostream &out, const student &a)
您接受引用为const。
You are accepting the reference as const .
ostream& operator<(ostream& out,string1& s1)
ostream& operator<<(ostream &out,string1 &s1)
这只是参考。
this is just reference.
out<< a.idno;
out<<a.idno;
As" a"是const,这个调用不适用于重载运算符
接受对string1的非const引用。
使用this = ostream& operator<<(ostream& out,const string1& s1)
它应该可以工作。
问候,
Amir
As "a" is const, this call is not working with overloaded operator
accepting non-const reference to string1.
Use this =ostream& operator<<(ostream &out,const string1 &s1)
It should work.
Regards,
Amir
感谢Amir。
现在我明白了什么问题。你的解释非常好。
非常感谢
thanks Amir.
now i understand what is the problem . your explanation was very nice.
thanks a lot
这篇关于关于运算符重载的问题<<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!