String Value

Sep 25, 2011 at 12:11pm
Ok so I have been looking for a solution in the internet for nearly 2 hours and this has been bugging me seriously...

class Menu(){
private:
std::string();
};

works, but when I tried this however...

std::string("dkfjdkf");

it errors... now I dont know why or what Im doing wrong... but I tried lots of things it compiles sometimes but then most of the time it errors... please help me out Im stuck :(
Sep 25, 2011 at 12:46pm
Firstly, what are the errors?

You class is bad. It should be
1
2
3
4
class Menu{
  private:
   std::string some_string;
};
Sep 25, 2011 at 12:59pm
my bad its actually like that std::string some_string();

however when I say std::string some_string("dfdf");

it gives off an error expected ; before ) token... i dont even understand it..

again, im sorry i was wrong at the post thank god you replied.
Sep 25, 2011 at 1:54pm
diorson, what do you want to do?
Sep 25, 2011 at 2:33pm
i want to have a string as a class member... I can declare it, but I cant put a value on it... I have declared strings on main and on the class methods and I can assign a value to them just fine...
Sep 25, 2011 at 2:34pm
If you write std::string some_string();, you're declaring a function. If you write std::string some_string("dfdf"); you're declaring a string variable and assigning "dfdf" to it. You don't give enough information to know for sure, but I bet that your problem comes from that.

What you want is
1
2
3
4
5
6
7
8
class Menu{
   private:
      std::string my_string;
   public:
      void set(){
         my_string = "dfdf";
      }
};
Last edited on Sep 25, 2011 at 2:35pm
Topic archived. No new replies allowed.