CAN YOU HAVE A MEMBER FUNCTION RETURNING A STRING IN A CLASS

Jul 10, 2012 at 4:16pm
Hi everyone, Is it possible to have a member function return a string in a class. for example:

1
2
3
4
5
6
7
class myClass{
public:
[...]
string returnStr();
private:
int blar_blar;
}
Jul 10, 2012 at 4:17pm
Yes. Not just a string. It can return any kind of object that you can define.
Jul 10, 2012 at 6:41pm
I have a counter question: Is there anythin you CAN'T have a member function return???
Jul 10, 2012 at 7:20pm
I'm getting the error "string does not name a type" but when i comment the member function out. my program runs perfectly fine. Do you guys have any idea what this error means and what could be causing it?
Jul 10, 2012 at 7:23pm
Do you include string header file in header part of the file where you declare and implement your class, myClass?
Jul 10, 2012 at 7:29pm
#include <string> is included both in the header file and the implementation file.
Jul 10, 2012 at 8:02pm
And did you remember to specify the namespace?
Jul 10, 2012 at 8:35pm
Try the following

std::string returnStr();
Jul 11, 2012 at 7:42am
@viliml

static arrays?
Jul 11, 2012 at 7:56am
@ToniAz: Can ANY function return an array? Any kind of array? No, just pointers! I meant, is there something you CAN return from a global function, but mnot from a member function!
Last edited on Jul 11, 2012 at 7:57am
Topic archived. No new replies allowed.