[英]Access to a string member in C++
当我设置&color
不起作用时,没有&
说需要转换并且不编译
我想在控制台中显示颜色属性
using namespace std;
int main()
{
class Car {
private:
int speed;
string color;
public:
Car::Car(int Speed, string Color)
{
speed = Speed;
color = Color;
}
void Car::MyMethod()
{
cout << color << endl;
}
};
Car Ferrari = Car(200, “Blue”);
Ferrari.MyMethod();
return 0;
}
添加正确的包含后,删除类内的范围解析( ::
)并修复代码编译的引号
#include <iostream>
#include <string>
using namespace std;
int main()
{
class Car {
private:
int speed;
string color;
public:
Car(int Speed, string Color)
{
speed = Speed;
color = Color;
}
void MyMethod()
{
cout << color << endl;
}
};
Car Ferrari = Car(200, "Blue");
Ferrari.MyMethod();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.