Hi guys. I'm aware of the requirement to specify std:: as the namespace for string in some way ( whether it be using namespace std etc) - but the following error has stumped me as to its cause.

$ g++ Conf.cpp
Conf.cpp:33: error: ‘string’ in namespace ‘std’ does not name a type

This is the only error - and here is a (truncated) Conf.cpp.

#ifndef Conf_H
#include "Conf.h"
#endif
#include <string.h>
#include "iniparser/iniparser.h"

std::string Conf::getString(const char* name) {/* This is the line causing the error */
    std::string rtn;
    rtn = string(iniparser_getstring(this->file, name));
    return rtn;
}

Line 33 as shown in the code sample is the definition of the function returning a string.

I've tried combinations of using std::string, but each time it states something similar "string does not name a type", "std::string has not been declared" etc..

Any suggestions would be most helpful.

Thanks,
PC_Nerd

Recommended Answers

All 3 Replies

> #include <string.h>
This is the old C-style header

Try
#include <string>


#ifndef Conf_H
#include "Conf.h"
#endif
Normally, the header include guards go inside the header file itself.

Ahh ok

so I should go back and make all me #include <string.h>'s into #include <string> ?

Thanks

Using gcc, #include <string.h> , provides a back-compatability system for stuff like using std::memcpy; etc...

You include that when you are using code that does not use the std::strcpy form but you want to just write strcpy(...) . Since you almost never want using namespace std; it is a nice half-way house, for importing c like code.

So if you have not included #include <string> , you have only a forward declaration for std::string. Hence the error. [Note that gcc use to include <string> in a lot of other includes, but they have been moving towards a independent #include system, so as you go up the version number you need to be more exact on you #include's. This means that although your code gets slightly longer your compile time goes down.

So in answer to the question you pose, yes you do need to add #include <string> BUT you may ALSO need #include <string.h>

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.