Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identifier "string" undefined?

Tags:

c++

string

I am receiving the error: identifier "string" undefined.

However, I am including string.h and in my main file, everything is working fine.

CODE:

#pragma once
#include <iostream>
#include <time.h>
#include <string.h>

class difficulty
{
private:
    int lives;
    string level;
public:
    difficulty(void);
    ~difficulty(void);

    void setLives(int newLives);
    int getLives();

    void setLevel(string newLevel);
    string getLevel();
};

Can someone please explain to me why this is occurring?

like image 273
Rhexis Avatar asked Aug 22 '11 11:08

Rhexis


People also ask

What is a string identifier?

String Identifiers (StringIDs) are a special type of output data used to represent some of the strings that the z/OS XML parser encounters during a parse.

How do you include a string?

In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string , and it's in the std namespace, so its full name is std::string .

How do you find the length of a string in C++?

The C++ String class has length() and size() function. These can be used to get the length of a string type object. To get the length of the traditional C like strings, we can use the strlen() function. That is present under the cstring header file.


7 Answers

<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.

like image 155
Puppy Avatar answered Oct 11 '22 01:10

Puppy


You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.

like image 37
Flexo Avatar answered Oct 11 '22 02:10

Flexo


You forgot the namespace you're referring to. Add

using namespace std;

to avoid std::string all the time.

like image 22
m0skit0 Avatar answered Oct 11 '22 02:10

m0skit0


Because string is defined in the namespace std. Replace string with std::string, or add

using std::string;

below your include lines.

It probably works in main.cpp because some other header has this using line in it (or something similar).

like image 23
Ernest Friedman-Hill Avatar answered Oct 11 '22 02:10

Ernest Friedman-Hill


Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.

like image 30
Nicol Bolas Avatar answered Oct 11 '22 03:10

Nicol Bolas


You must use std namespace. If this code in main.cpp you should write

using namespace std;

If this declaration is in header, then you shouldn't include namespace and just write

std::string level;
like image 21
Camelot Avatar answered Oct 11 '22 01:10

Camelot


#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;

like image 34
Pierre Lacave Avatar answered Oct 11 '22 03:10

Pierre Lacave