5

when I compile the following files, I've got the error:

ECArgs.h:36:3: error: ‘string’ does not name a type

ECArgs.h:36: ECString value(char c);

Could somebody give me any hints for the error?

ECArgs.h

#include <list>
#include "ECString.h"

class ECArgs
{
 public:
  ECArgs(int argc, char *argv[]);
  int nargs() { return nargs_; }
  bool isset(char c);
  ECString value(char c);
  ECString arg(int n) { return argList[n]; }
 private:
  int nargs_;
  int nopts_;
  ECString argList[32];
  list<ECString> optList;
};

ECString.h

#define ECS gnu

#if ECS == gnu
#include <cstring>
#define ECString string
using namespace std;
#else
#include <bstring.h>
#define ECString string
#endif
5
  • It's not a "wired" error. It's perfectly reasonable not to recognize a symbol from a non-included header. Also, typedef and using are better solutions for type aliases than #define. Commented Mar 4, 2013 at 4:08
  • @chris What do you mean by non-included header? Commented Mar 4, 2013 at 4:11
  • If you want to get access to std::string, you need to #include <string>, not #include <cstring>. Commented Mar 4, 2013 at 4:13
  • Precisely what the answer says. std::string is in <string> and you never include it. Therefore, the compiler is perfectly reasonable in complaining. Commented Mar 4, 2013 at 4:13
  • Are you sure that #if ECS == gnu does what you think it does? It evaluates ECS and gnu as integer constant expressions. How is gnu defined? Commented Mar 4, 2013 at 6:38

3 Answers 3

9

I ran into a similar error. It was due to the fact that I had left out using namespace std;

Alternatively, one has to use std::string for all occurrences of string.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I guess just typing #include <string> is not enough, need using namespace std;
3

You need to add:

#include <string>

cstring includes function to manipulate C-style string. This version works:

#include <list>
#include <string>

#if ECS == gnu
#include <cstring>
#define ECString string
using namespace std;
#else
#include <bstring.h>
#define ECString string
#endif

class ECArgs
{
 public:
  ECArgs(int argc, char *argv[]);
  int nargs() { return nargs_; }
  bool isset(char c);
  ECString value(char c);
  ECString arg(int n) { return argList[n]; }
 private:
  int nargs_;
  int nopts_;
  ECString argList[32];
  list<ECString> optList;
};

int main()
{

}

2 Comments

Just wondering why cstring doesn't work. I tried to use 'string' in another test.cpp file which just include <cstring>. The test.cpp works well.
@EarthWorm Is possible you includes another header files that included string? I am looking at gcc 4.4.4 and several headers include string, including random and stdexcept.
0

I had the same error. It didn't work with

#include <string.h>

but started to work as I changed it to

#include <string>

Have no idea why. (Under Microsoft Visual Studio Professional 2022)

2 Comments

string.h is a header from the C library with stuff like strlen. string is part of the standard C++ library that defines std::string
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.