0

I want to get the value of a private string name in sampleclass.

#include <iostream>
#include <string>

using namespace std;

class sampleclass {
public:
    int getname(){ //this is my attempted getter
    string x = name;
    }
private:
    string name= "lance"; //this is the private I want returned by value

};

int main(){    
    sampleclass object;
    cout << object.getname();
}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
user3490590
  • 17
  • 2
  • 9

2 Answers2

4

You need to return a string in your getname() function, since your name variable is a string

string getname() {
    return name;
}

By doing so, you get a new instance of std::string as an rvalue result, which is then outputted to the screen in your main function.

As another thought, not related to your problem though: there is no problem in using a namespace globally for small programs like this one, but you should try to not get used to it because it can lead to name conflicts within different namespaces in bigger projects.

Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
0
#include <iostream>
#include <string>

using namespace std;

class sampleclass{
public:
    sampleclass() : name("lance") { }
    string getname(){ // return a string (not an int)
       return name;
    }
private:
    string name;

};
int main(){

    sampleclass object;
    cout << object.getname();

}

g++ test.cpp && ./a.out lance

Thomas
  • 3,074
  • 1
  • 25
  • 39
  • 1
    `name` was already correctly initialized (it's semantically equivalent to this). – chris Apr 02 '14 at 17:47
  • Is that valid C++ code? Haven't ever seen that in real world code. – Thomas Apr 02 '14 at 17:49
  • 1
    It's valid C++11 code, which I can only assume the OP is using if writing code like that. – chris Apr 02 '14 at 17:49
  • Ok, learned something new then, looks weird though. Anyway, I don't think he is writing C++11, since this question isn't tagged C++11. – Thomas Apr 02 '14 at 17:51
  • alright so I couldn't declare a string that are on private? – user3490590 Apr 02 '14 at 17:51
  • As far as I know, the C++ tag implies C++11 now, not to say that everyone uses it as such. – chris Apr 02 '14 at 17:52
  • @chris, that's an unusually bold assumption in this case when OP is continuing to struggle to understand matching of return types :-) but quite interesting – UpAndAdam Apr 02 '14 at 17:56
  • @UpAndAdam, I do realize that, although the OP didn't mention any errors from it. I doubt that syntax would come from the OP being a C# or Java programmer either for your mentioned reason. – chris Apr 02 '14 at 17:57
  • @user3490590: It seems to be correct as long you are writing C++11 code. If you are writing C++03/C++98 code, then you must/should initialize them the way I did. – Thomas Apr 02 '14 at 17:57
  • @chris: Visual C++ defaults to C++11 afaik. – Thomas Apr 02 '14 at 17:58
  • @thomas ok sir, thanks! sorry im a newbie. – user3490590 Apr 02 '14 at 17:59
  • @Thomas, That's correct, but only the most recent version(s) support this feature. – chris Apr 02 '14 at 18:02
  • @chris likewise, hence it was a very worthwhile and interesting read for me, and it also kept the focus on the 'more problematic' part of what op had done wrong. (I figured it was OP just trying intersting things out, I've seen people try writing that before C++0X and C++11X even started when I was a TA during college) – UpAndAdam Apr 02 '14 at 18:53