Hey everyone,

I'm trying to create a string inside a class, but for some reason, my compiler is not recognizing:

class ob {
    string name("Name");
public:
      //code
}

error C2059: syntax error : 'string'

This class is being written in a header file, which has #include <string>.

Now, when I use the same code in my main() file, it works fine.

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Well can't use just do #include <string> in main() then?

Initialization of class members (except static integral types):

class Ob // avoid all small letters names
{
public: // declare public then protected then private
  Ob():name("Name") {} // member initializators list
  ...
private:
  std::string name; // initialize in constructor(s)!
};

Initialization of class members (except static integral types):

class Ob // avoid all small letters names
{
public: // declare public then protected then private
  Ob():name("Name") {} // member initializators list
  ...
private:
  std::string name; // initialize in constructor(s)!
};

Got it to work, it makes sense. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.