Error overloading operator

I am getting this error "<< operator function should take two parameters"
I wanted to overload << operator so as to work like "cout<<s;" where s is a string object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
	char *name;
	int length;
	public:
	string()
	{
	}
	string(char *s)
	{   	
		length=strlen(s);
		name=new char[length+1];
		strcpy(name,s);
	}
	friend void operator<<(string m);
	};
void operator<<(string m)
{
	cout<<m.name;
}
int main()
{
	string s1("hello world");
	<<s1;
	return 0;
}
Last edited on
You say you want to overload an operator, but you're doing it as if you want to define a function. There's a big difference in both syntax and meaning.

Here's a tutorial on overloading the inserter operator:
http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/Useafriendtooverload.htm
You are putting in the wrong way . you cannot create string class as string is the data type.
Use <iostream> and not <iostream.h>
Don't use <conio.h>
Use <string> and not <string.h>, or if you want to call your class lowercase "string" then don't include the string header at all.

As for your smaller problem, it has already been answered above.
Topic archived. No new replies allowed.