gpt4 book ai didi

c++ - 无法识别 C++ 中的字符串数据类型

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:24 24 4
gpt4 key购买 nike

我正在使用 Visual C++ 2008 速成版学习 C++ 编程。我的程序没有识别 std::string数据类型。我已经包含了 <string>图书馆。

#include "stdafx.h"
#include <iostream>
#include "TestClass.h"
#include <string>

using namespace std;

class Rectangle {
private:
int width;
int height;
double area;
string name;

public:
Rectangle()
{
width = 0;
height = 0;
}

int getWidth()
{
return width;
}

int getHeight()
{
return height;
}

void setWidth(int width)
{
this->width = width;
}

void setHeight(int height)
{
this->height= height;
}

void setArea( )
{
area = width * height;
}

double getArea( )
{
return area;
}

Rectangle (const Rectangle & x)
{
this->area = x.area;
}

void friendtestfunction(Rectangle2 s)
{
cout << "" << s.name;
}
};

int Rectangle2::stat_var = 5;

int _tmain(int argc, _TCHAR* argv[])
{
Rectangle rect;
rect.setWidth(10);
rect.setHeight(20);
rect.setArea( );

cout<< "height is equal to :" << rect.getHeight() << endl;
cout<< "width is equal to :" << rect.getWidth() << endl;
cout << "area is equal to :" << rect.getArea() << endl;

getchar();
return 0;
}

当我在 Rectangle 类中声明一个字符串类型的私有(private)变量时,它显示了以下错误:

1>c:\users\subith.p\documents\visual studio 2008\projects\test\test\testclass.h(10) : error C2146: syntax error : missing ';' before identifier 'name'

1>c:\users\subith.p\documents\visual studio 2008\projects\test\test\testclass.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

最佳答案

添加#include <string>到你的头文件 testclass.h .

关于c++ - 无法识别 C++ 中的字符串数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com