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, 2022 in C++ by Nicholas
• 7,760 points
1,215 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, 2022 by Damon
• 4,960 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, 2022 in C++ by Damon
• 4,960 points
5,602 views
0 votes
0 answers

Reverse a number without converting it to string in C++

I'm attempting to write a programme in C++ to reverse a number. It works well with numbers like 1234, but when I try to enter a number like 5430, it displays 345, and the same with numbers that begin with zero, such as 0234, which displays 432. Could someone please explain how to deal with zeros at the beginning and end of a sentence? I simply need to save the number without converting it to a string. #include<iostream> using namespace std; int main(){ ...READ MORE

Jun 30, 2022 in C++ by Nicholas
• 7,760 points
601 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
408 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
774 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, 2022 in Others by Rahul
• 9,680 points
527 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, 2022 in Others by Aditya
• 7,680 points
81,894 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, 2022 in C++ by Damon
• 4,960 points
689 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, 2022 in C++ by Damon
• 4,960 points
539 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, 2022 in C++ by Damon
• 4,960 points
1,387 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, 2022 in C++ by Damon
• 4,960 points
1,089 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP