C++ – error: ‘string’ in namespace ‘std’ does not name a type

cclass

I have this 2 separated class and i cant fix this compiler problem:

In file included from classA.cpp:2:0:
classB.h:6:10: error: 'string' in namespace 'std' does not name a type
std::string str;
^

In file included from classA.cpp:3:0:
classA.h:6:25: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
classB *ptr_b = new classB;

There is the classA.h:

#ifndef CLASSA_H
#define CLASSA_H

class classA {
private:
    classB *ptr_b = new classB;
public:
    classA();
};

#endif /* CLASSA_H */

classA.cpp:

#include "classB.h"
#include "classA.h"

classA::classA() {
}

classB.h:

#ifndef CLASSB_H
#define CLASSB_H

class classB {
private:
    std::string str;
public:
    classB();

};

#endif /* CLASSB_H */

classB.cpp:

#include <string>
#include "classB.h"

classB::classB() {
}

I apreciate all the help you can give. I donĀ“t know how to fix this and I'm going crazy.

Thanks for reading.

Best Answer

You need #include <string> in classB.h. Right now classA.cpp includes classB.h with no prior #include <string> anywhere, so the included reference to std::string in classB.h causes an error.

In general, if a name is used in a header foo.h, you should include the header bar.h that declares the name in foo.h, or forward-declare the name in foo.h. Otherwise everyone else who includes foo.h will need to remember to make sure bar.h gets included first.

Related Topic