简体   繁体   中英

error: 'string' in namespace 'std' does not name a type

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM