C++ 类无法识别字符串数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2878401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ class is not recognizing string data type
提问by reallythecrash
I'm working on a program from my C++ textbook, and this this the first time I've really run into trouble. I just can't seem to see what is wrong here. Visual Studio is telling me Error: identifier "string" is undefined.
我正在处理我的 C++ 教科书中的一个程序,这是我第一次真正遇到麻烦。我似乎看不出这里有什么问题。Visual Studio 告诉我错误:标识符“字符串”未定义。
I separated the program into three files. A header file for the class specification, a .cpp file for the class implementation and the main program file. These are the instructions from my book:
我将程序分成三个文件。类规范的头文件、类实现的 .cpp 文件和主程序文件。这些是我书中的说明:
Write a class named Car that has the following member variables:
编写一个名为 Car 的类,它具有以下成员变量:
year. An int
that holds the car's model year.
年。一个int
是拥有汽车的车型年。
make. A string
that holds the make of the car.
制作。string
保存汽车品牌的A。
speed. An int
that holds the car's current speed.
速度。一个int
是拥有汽车的当前速度。
In addition, the class should have the following member functions.
此外,该类应具有以下成员函数。
Constructor. The constructor should accept the car's year
and make
as arguments and assign these values to the object's year
and make
member variables. The constructor should initialize the speed
member variable to 0
.
构造函数。构造函数应该接受汽车的year
和make
作为参数并将这些值分配给对象year
和make
成员变量。构造函数应将speed
成员变量初始化为0
.
Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object's year
, make
and speed
member variables.
存取器。适当的存取器函数应创建以允许从一个对象的检索到的值year
,make
和speed
成员变量。
There are more instructions, but they are not necessary to get this part to work.
有更多说明,但它们不是使这部分工作所必需的。
Here is my source code:
这是我的源代码:
// File Car.h -- Car class specification file
#ifndef CAR_H
#define CAR_H
class Car
{
private:
int year;
string make;
int speed;
public:
Car(int, string);
int getYear();
string getMake();
int getSpeed();
};
#endif
// File Car.cpp -- Car class function implementation file
#include "Car.h"
// Default Constructor
Car::Car(int inputYear, string inputMake)
{
year = inputYear;
make = inputMake;
speed = 0;
}
// Accessors
int Car::getYear()
{
return year;
}
string Car::getMake()
{
return make;
}
int Car::getSpeed()
{
return speed;
}
// Main program
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
int main()
{
}
I haven't written anything in the main program yet, because I can't get the class to compile. I've only linked the header file to the main program. Thanks in advance to all who take the time to investigate this problem for me.
我还没有在主程序中写任何东西,因为我无法编译这个类。我只将头文件链接到主程序。提前感谢所有花时间为我调查这个问题的人。
采纳答案by radman
You have forgotten to #include the string header and you need to fully qualify your usage of string
to std::string
, the amended code should be.
您忘记了 #include 字符串标题,您需要完全限定您对string
to的使用std::string
,修改后的代码应该是。
// File Car.h -- Car class specification file
#ifndef CAR_H
#define CAR_H
#include <string>
class Car
{
private:
int year;
std::string make;
int speed;
public:
Car(int, string);
int getYear();
std::string getMake();
int getSpeed();
};
#endif
// File Car.cpp -- Car class function implementation file
#include "Car.h"
// Default Constructor
Car::Car(int inputYear, std::string inputMake)
{
year = inputYear;
make = inputMake;
speed = 0;
}
// Accessors
int Car::getYear()
{
return year;
}
You could put using namespace std;
at the top of Car.cpp and that would let you use string without the std:: qualifier in that file. However DON'T put one of these in the header because it is very bad mojo.
您可以放在using namespace std;
Car.cpp 的顶部,这样您就可以在该文件中使用没有 std:: 限定符的字符串。但是,不要将其中之一放在标题中,因为它非常糟糕。
As a note you should always include everything that the class declaration needs in the header before the class body, you should never rely on a client source file including a file (like <string>
) before it includes your header.
请注意,您应该始终在类主体之前的头文件中包含类声明所需的所有内容,在包含您的头文件之前,您不应该依赖包含文件(如<string>
)的客户端源文件。
With regard to this part of your task:
关于您任务的这一部分:
Constructor. The constructor should accept the car's year and make as arguments and assign these values to the object's year and make member variables. The constructor should initialize the speed member variable to 0.
构造函数。构造函数应该接受汽车的年份和 make 作为参数,并将这些值分配给对象的年份和 make 成员变量。构造函数应将 speed 成员变量初始化为 0。
The best practice is to use an initializer list in the constructor, like so:
最佳实践是在构造函数中使用初始化列表,如下所示:
// Default Constructor
Car::Car(int inputYear, string inputMake)
: year(inputYear),
make(inputMake),
speed(0)
{
}
回答by sth
You should use the fully qualified name std::string
, or you forgot to include the <string>
header. Or both.
您应该使用完全限定的 name std::string
,或者您忘记包含<string>
标题。或两者。
回答by Dan Puzey
I suspect you need your #include <string>
at the topof the file, above where you use the string
type.
我怀疑你需要#include <string>
在文件的顶部,在你使用string
类型的上方。
回答by Sabri Mevi?
#include <string>
does NOT work. You should put using namespace std ;
above the code.
#include <string>
不起作用。你应该把using namespace std ;
代码放在上面。
回答by fsoria
you need to add these 2 lines:
您需要添加以下两行:
#include < string >;
#include <字符串>;
using std::string;
使用 std::string;
回答by mike
Doing std::string
will work but is rather cumbersome to write each time. To make it work for all string without doing std::each time, simply put these two lines of code at the top of your header file:
这样做std::string
会奏效,但每次都写起来相当麻烦。要使其适用于所有字符串而无需每次都执行std::,只需将这两行代码放在头文件的顶部:
#include < string >;
using namespace std;
And you're good to go!
一切顺利!