本文介绍了问题覆盖运算符<<输出cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面写了以下代码,错误是:
Hi, I wrote the following code below the line is error :
ostream& String::operator<<(ostream& stream,const String& str){
我的代码:
my code :
#include <iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class String{
char *ch;
public:
String();
String operator=(char* ob);
String operator+(String str);
char * toString();
friend ostream& operator<<(ostream& stream,const String& str);
};
String::String(){
}
String String::operator=(char* ob){
ch=ob;
return *this;
}
String String::operator+(String str){
String temp;
temp.ch=strcpy(ch,str.ch);
return temp;
}
ostream& String::operator<<(ostream& stream,const String& str){
stream<<str.ch;
return stream;
}
char * String::toString(){
return ch;
}
int main(int argc, char *argv[]) {
String str1,str2,str3;
str1="ehsan";
str2="shahbakhty";
cout<<str2;
return 0;
}
错误编译器:
[错误]'std :: ostream& String :: operator<<(std :: ostream&,const String&)'必须只接受一个参数
error compiler :
[Error] 'std::ostream& String::operator<<(std::ostream&, const String&)' must take exactly one argument
推荐答案
Quote:
ostream& String :: operator<<(ostream& stream,const String& str){
ostream& String::operator<<(ostream& stream,const String& str){
to
to
ostream& operator<<(ostream& stream,const String& str){
这篇关于问题覆盖运算符<<输出cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!