Error in Class example

Hi everyone, I'm trying to display the sentence "my name is james" in main() using class. But I could not figure out what's giving me error in this code. Can somebody tell me what is wrong with this code please? Thank you so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

class myClass
{
	public:
		myClass(string x){		
			name = x;			
			getName(name);		
	}
		string getName(string z){	
			return z;	
		}
	private:
		string name;
};

int main()
{
	myClass myObject("my name is james");		
	cout << myObject.getName() << endl;
	return 0;
}

p.s. the compiler says "no matching function for call to 'myClass::getName()'.
You don't need getName() function inside your constructor, because it already sets name to the string from the parameter. getName() doesn't need a parameter, since you want it to return the private member name. So it should be something like : string getName(){ return name}
Last edited on
Ohhhh i see! Thank you so much for the help arbwok! :D
Topic archived. No new replies allowed.