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,
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.
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.
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);