Operator overloading

Hey guys i m realy in need of help
i was learning about copy constructor and now i m stuck here
it says
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) e:\computer science\visual studio projects\copycons\copycons\copycons.cpp 34 1 CopyCons


what can i do here ??

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  // CopyCons.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class Person{
	string* name;
	int age;

public:
	Person( string s, int a){
		this->name = new string (s);
		this->age = a;
	}

	Person(const Person& p){

		name = new string(*p.name);
		age = p.age;
		cout << "Copy Constructor called \n";
	}

	void changePerson(string s,int a){

		*(this->name) = s;
		this->age = a;
	}

	friend ostream& operator<< (ostream &out,const Person& p){

		out << "Hey I am " << *p.name << "and I am " << p.age << "years old \n";
		return out;
	}

};

int main(int argc, char* argv[])
{
	Person p("Salman",18);
	cout << p;

	Person p2 = p;
	cout << p2;
	p.changePerson("Salman Majid",17);
	cout << p;
	cout << p2;

	system("pause");
	return 0;
}

you need to #include <string>

By the way: it's completely unnecessary to make name a pointer. You can simply assign a string at any time.
also, making it a pointer makes it necessary to deallocate its memory in the destructor.

Aceix.
bro Actually i was testing copy constructor so i used string as pointer and thats it .... it is not working
Thanks bro its working now as i included

<string>
thanks for your support

so much happy
Topic archived. No new replies allowed.