Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I wrote the following code below the line is error :
C++
ostream& String::operator<<(ostream& stream,const String& str){


my code :
C++
#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;
}


error compiler :
[Error] 'std::ostream& String::operator<<(std::ostream&, const String&)' must take exactly one argument
Posted
Comments
Andreas Gieriet 12-Apr-14 19:13pm    
Just a comment to the semantic of the String class: ownership of ch is broken - you will have memory leaks and your system will die sooner or later.
1) why the heck do you create your own String class - use the std::string
2) if you insist on doing your own String class, add all four (C++11 6) normalization functions, take over proper ownership and free that resource in the destructor.
3) Strings should have value semantics (everything else is an undesired surprise)
4) Decide is your string is mutable or only const (e.g. C+ std::string is mutable, C# string is not, etc.)
5) How about support for wide characters, UTF8, UTF16, UTF32?
Writing your own string class opens up the box of pandora - you most likely will not have a complete implementation and risk to fix this and that again and again...
Cheers
Andi

1 solution

You declared the insertion operator as global (friend function of String class) and then defined as String member. Change from
Quote:
ostream& String::operator<<(ostream& stream,const String& str){
to
C++
ostream& operator<<(ostream& stream,const String& str){
 
Share this answer
 
Comments
Andreas Gieriet 12-Apr-14 17:53pm    
Correct, my 5!
Cheers
Andi
zehs_sha 12-Apr-14 18:31pm    
Not work and line so error Please test code in your ide and then answer
Andreas Gieriet 12-Apr-14 19:04pm    
Hello zehs_sha,
What error do you get after that fix?
Andi
zehs_sha 12-Apr-14 23:03pm    
error compiler too:
[Error] 'std::ostream& String::operator<<(std::ostream&, const String&)' must take exactly one argument

Andreas Gieriet 13-Apr-14 14:21pm    
You need to learn reding carefully: the String:: needs to be removed in the operatod implementation - it is not a member of the String class. See also the other comemnts on that!
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900