+ 1

Problem creating classes in cpp

I was following the c++ tutorial step by step, and I created, in separated files, the MyClass that takes two integer arguments. And I had the idea to make a similar one, but one taking strings for arguments. Here is the header: #ifndef PENDORCHO_H #define PENDORCHO_H class Pendorcho { public: Pendorcho (string x); void setName(string x); string getName(); protected: private: const string name; }; #endif // PENDORCHO_H And here is the source: #include "Pendorcho.h" #include <iostream> using namespace std; Pendorcho::Pendorcho (string x) : name(x) { cout << "Merenguete" << "\n"; } Pendorcho::setName(string x) { name = x; } Pendorcho::getName() { return name; } This is my main: int main() { Pendorcho obj1("Mingo"); obj1.getName(); obj1.setName("Mariela"); obj1.getName(); return 0; } When I try to run it I get this error in the header file: Line 8: error: expected ')' before 'x'. I don't know why I should close de parenthesis before the x, but I tried it and has no better result. What am I doing wrong?

17th Sep 2018, 12:31 AM
Federico
Federico - avatar
14 ответов
+ 3
You need to: #include <string>
17th Sep 2018, 12:57 AM
John Wells
John Wells - avatar
+ 3
the member data (name) is declared constantly and is initielized 2 times by the member functions ,
17th Sep 2018, 12:58 AM
Michael
Michael - avatar
+ 3
You don't have a: using namespace std; in your header so must use std::string to reference it.
17th Sep 2018, 2:07 AM
John Wells
John Wells - avatar
+ 3
Your Pendorcho.cpp has a few issues: - It should include <string> (convention states you always include things you require even when they are included by your other includes), - Pendorcho::setName is missing the function type of void, and - Pendorcho::getName is missing the function type of string.
17th Sep 2018, 2:33 AM
John Wells
John Wells - avatar
+ 3
One last point, it is not a good idea putting using namespace in a header. You should either just use the symbols you must have or tag them with their namespace at each usage. using string; OR Pendorcho (std::string x);
17th Sep 2018, 5:13 AM
John Wells
John Wells - avatar
+ 2
I wouldn't. Put three files into playground so I can see them. Post a link to all three.
17th Sep 2018, 1:29 AM
John Wells
John Wells - avatar
+ 2
You are very welcome. Enjoy your experimenting. Feel free to ask more here or as a comment on my code, if you need more help.
17th Sep 2018, 2:38 AM
John Wells
John Wells - avatar
+ 1
It should be in the h file immediately after the #define.
17th Sep 2018, 1:19 AM
John Wells
John Wells - avatar
+ 1
Thanks! Now it works nice Thanks for being there!!
17th Sep 2018, 2:36 AM
Federico
Federico - avatar
0
I'm sorry, it's not really clear where the error pop up. Is the line in the header, the "Pendorcho (string x);" one
17th Sep 2018, 12:34 AM
Federico
Federico - avatar
0
I've just tried with the #include <string> and I keep receiving the same error message. I reduced my main function to this one: int main() { Pendorcho man("Mingo"); man.getName(); return 0; } To avoid problems, and the name data is not a constant variable anymore.
17th Sep 2018, 1:17 AM
Federico
Federico - avatar
0
There it is. I tryed removing it and puting it again with no better results. Could be something wrong in my project configuration or something like that? Maybe I should try starting it over from zero.
17th Sep 2018, 1:26 AM
Federico
Federico - avatar
0
First of all: thanks for the patience! It did work and the compiler doesn't got stucked at that point. But now I have an error in the class source. Is in the line: "Pendorcho::setName(string x)" And says: error: prototype for 'std::__cxx11::string Pendorcho::setName(std::__cxx11::string)' does not match any in class 'Pendorcho'
17th Sep 2018, 2:23 AM
Federico
Federico - avatar