C++ errors: ‘string’ does not name a type
Asked Answered
P

3

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
Palsy answered 4/3, 2013 at 4:6 Comment(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.Meshed
@Meshed What do you mean by non-included header?Palsy
If you want to get access to std::string, you need to #include <string>, not #include <cstring>.Graviton
Precisely what the answer says. std::string is in <string> and you never include it. Therefore, the compiler is perfectly reasonable in complaining.Meshed
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?Catty
G
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()
{

}
Gratulant answered 4/3, 2013 at 4:11 Comment(2)
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.Palsy
@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.Gratulant
S
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.

Swiftlet answered 1/3, 2016 at 19:26 Comment(1)
Yes I guess just typing #include <string> is not enough, need using namespace std;Nitrite
G
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()
{

}
Gratulant answered 4/3, 2013 at 4:11 Comment(2)
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.Palsy
@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.Gratulant
C
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)

Contributor answered 4/6, 2024 at 13:14 Comment(2)
string.h is a header from the C library with stuff like strlen. string is part of the standard C++ library that defines std::stringFantinlatour
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 ReviewPoodle

© 2022 - 2025 — McMap. All rights reserved.