C string in classes

0 votes

I realise this is a silly question, but it's perplexing and annoying since something that should function isn't. 

I'm using the GCC compiler with Code Blocks, and I'm trying to construct 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 locate the class "string," despite the fact that my main.cpp is able to identify "#include," but my language class is not.

This is the essential point. 

I wrote it hastily to check if the main programme could 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?

Jun 15 in C++ by Nicholas
• 2,460 points
9 views

1 answer to this question.

0 votes

The std namespace contains the string class. You need change the class to something like this:

class Language
{
    public:

    private:
        std::string str;
};

This can also be included at the top of the header file, although it is not recommended:

using namespace std;
answered Jun 15 by Damon
• 3,580 points

Related Questions In C++

0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 3,580 points
6 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31 in C++ by Damon
• 3,580 points
20 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31 in C++ by Damon
• 3,580 points
19 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
15 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10 in Others by Rahul
• 8,980 points
28 views
0 votes
1 answer

What does "collect2: error: ld returned 1 exit status" mean?

 The ld returned 1 exit status error ...READ MORE

answered Feb 22 in Others by Aditya
• 7,280 points
1,519 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
21 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

answered Jun 15 in C++ by Damon
• 3,580 points
13 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP