Ask your own question, for FREE!
Computer Science 57 Online
OpenStudy (pradius):

another C++ question :) learning to use functions and objects. I separate a class with functions in a different file functions.cpp one of the functions string GetName(); appears as a invalid member of the class in the header file functions.h.... whats wrong?

OpenStudy (pradius):

functions.cpp file #include "functions.h" #include <iostream> #include <string> using namespace std; class functions { public: void SetName(string x){ name = x; } string GetName(){ return name; } private: string name; };

OpenStudy (pradius):

functions.h file #ifndef FUNCTIONS_H #define FUNCTIONS_H class functions { public: void SetName(); string GetName(); }; #endif // FUNCTIONS_H

OpenStudy (konradzuse):

I believe you need to include the function's class into this second class, and by doing that you will be able to call it's methods... I am not sure tho, this is how it's done in java. You're creating a class with functions of setting and getting a name. To access these you need to call the class that contains the set and get, and then use them. You also should actually set a string, and use a printf statement to see what the name is when returned.

OpenStudy (rsmith6559):

AFAIK, in the cpp file the class isn't redeclared, it's just a bunch of function definitions. The function definitions need to have the full name of the function: #include "functions.h" #include <iostream> #include <string> void functions::SetName(string x) { name = x; } string functions::GetName() { return name; } name doesn't need any declaration in the cpp file. Classes are their own namespace, so your using statement is (AFAIK) useless. This reply should work with you header file.

OpenStudy (anonymous):

Well first of all, in order to help you , please define what the problem is.{.learning to use functions and objects. I separate a class with functions in a different file functions.cpp one of the functions string GetName(); appears as a invalid member of the class in the header file functions.h.... whats wrong?} 2. Just by looking at it your code is missing input , at the moment your code is all out put., which makes your code hard to read, and understand .

OpenStudy (pradius):

ok I see u guys are a lil bit confused with my question I will include the main function so it might get a lot more cleared. Just think that each reply is a different file.. the file name is always in the top of the reply. thanks

OpenStudy (pradius):

main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; int main() { string name; functions fobject; cout << "What is your name\n"; getline(cin, name); fobject.SetName(name); cout << "Your name is " << fobject.GetName() << endl; return 0; }

OpenStudy (anonymous):

ok.. cool..lol .. in C++ it goes header file, class file ,{ class files, } then class main.. that way it's easy to troubshoot..

OpenStudy (pradius):

the error is that the compiler says that the class function have no member named GetName

OpenStudy (anonymous):

not sure but i think it should be functions fobject ( parm 1, parm2) cout << "What is your name\n"; getline(cin, name); fobject.SetName(name); cout << "Your name is " << fobject.GetName() << endl; return 0;

OpenStudy (rsmith6559):

Yeh, that's the absolute path: functions::GetName

OpenStudy (pradius):

Well nothing is working for me... I just gonna keep reading to see what I can find. Thanks for your help guys

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!