4

I know this is quite a ridiculous question but this is quite confusing and irritating, as something that should work simply is not. I'm using Code Blocks with the GCC compiler and I am trying to simply create a string variable in my class

#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE

#include <string>

class Language
{
    public:

    private:
        string str;
};

#endif

Strange enough, my compiler halts me with an error saying this:

C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|

For some reason, it is unable to find the class "string" which for some reason, my main.cpp is able to detect "#include " while my language class is not able for some reason.

This is the main I wrote quickly just to see it main itself is able to see the string file:

//main.cpp

#include <iostream>
#include <string>
#include "alien_language.h"

using namespace std;

int main()
{
    string str;

    return 0;
}

Does anyone know what's going on?

6 Answers6

12

using namespace std;

That's what's going on.

You don't have std:: prefixing the string in your class. Everything in the standard library is in the namespace std.

It is generally regarded as bad practice to use using namespace std;, by the way. For more information on why and what to do instead, check out this question: Using std Namespace.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Thank you, I've completely forgot that string the std:: prefixing, I'm used to not putting using namespace std; in my class files, but doing so in my main.cpp –  Sep 03 '09 at 07:00
7

The string class is defined in the std namespace. You should chenge the class to this:

class Language
{
    public:

    private:
        std::string str;
};

It is also possible, but not recommended to add this to the top of the header file:

using namespace std;
Ropez
  • 3,485
  • 3
  • 28
  • 30
6

string is in namespace std, and you need to qualify it fully inside your header file:

#include <string>

class Language
{
    public:

    private:
        std::string str;
};

Do not use using namespace std; or similar in header files.

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
  • why not to `using namespace std;` in header files? – Mogi May 16 '20 at 19:12
  • 1
    nevermind, find the appropriate answer after googling: http://www.cplusplus.com/forum/beginner/25538/ leaving these comments for future confusions – Mogi May 16 '20 at 19:14
2

You should refer to it as std::string;

Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
1

It looks to me like you're missing the all-important (with a hint of sarcasm) using namespace std; line. Either add that in before your class, or explicitely use std::string str. I'd recommend against adding the using namespace std; line in a header file, as it would pollute the mainspace for any file that includes it.

Twisol
  • 2,762
  • 1
  • 17
  • 17
0

The string class in standard C++ is in std namespace. Write something like using std::string; in your header or fully qualify it as std::string in your header.

Beware that using namespace std; in header is a bad practice (read here).

Community
  • 1
  • 1
Abhay
  • 7,092
  • 3
  • 36
  • 50