no matching for function call

Write your question here.
this is from a c++ example in a c++ text book
in h file
class Employee
{
public:
Employee( std::string, std::string, double );
...
in implement file-constructor
Employee::Employee(string name1, string name2, double salary)
in main file
Employee employeeA{ "Joe", "smith", 3456.56};

and the program works, I read another text book
there is a similar class example but it using & for string
so I tried that method and add & after string
in h file
class Employee
{
public:
Employee( std::string &, std::string &, double );
...
in implement file-constructor
Employee::Employee(string & name1, string & name2, double salary)
in main file
Employee employee( "Joe", "smith", 3456.56);
it did not work, with error message of "no matching function for call to
Employee::Employee...."
can someone explain it what is wrong.
I think I have problem to use & and pointer properly,

I am using codeBlock 16.01 with window 7

thank you

























7name2, double salary)




you should declare a reference on the header as well. Otherwise it is nor the same signature and that's why it sends an error.
Those error should be spotted as c++ has a lot of vulnerabilities that you should consider.
If you are having problems find those errors you can always use a software to help you do that.
I know one called checkmarx that might help you.
Good luck!
Ben.
Hello oakmill,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can us the preview button at the bottom to see how it looks.

Maybe this will help clear things up:

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
class Employee
{
public:
	Employee(std::string, std::string, double);
	...

//  Not sure what your class looks like, but I added this for demonstration
private:
	std::string m_fName;
	std::string m_lName;
	double m_salary;
};

//  I have found in my us of Visual Studio this part  works better when it is in its own
//  .cpp file and included as part of the compile. This part should also work if included
//  in the file that contains main.

Employee::Employee(string name1, string name2, double salary)
{
	m_fName = name1;
	m_lName = name2;
	m_salary = salary;
}

//in main file
//  Notice I changed the{} to () because that is what is needed here. You are calling
//  the constructor not creating a block of code.

Employee employeeA("Joe", "smith", 3456.56);


Hope that helps,

Andy
Last edited on
@Handy Andy
Your posts are always well done and informative, but perhaps there's a typo on line 11:
double m_salry;
@Enoizat,

I try hard, but my keyboard sometimes does not work well and I miss things sometimes.

Andy
Topic archived. No new replies allowed.