C++ C++;
在C++中使用组合和类,我正处于摸索阶段。我遇到的一个代码片段以以下方式实现了组合:C++ C++;,c++,composition,scoping,C++,Composition,Scoping,在C++中使用组合和类,我正处于摸索阶段。我遇到的一个代码片段以以下方式实现了组合: #include<iostream> using namespace std; class Engine { public: int power; }; class Car { public: Engine e; string company; string color; void show_details() { cout <<
#include<iostream>
using namespace std;
class Engine {
public:
int power;
};
class Car {
public:
Engine e;
string company;
string color;
void show_details() {
cout << "Compnay is: " << company << endl;
cout << "Color is: " << color << endl;
cout << "Engine horse power is: " << e.power << endl;
}
};
int main() {
Car c;
c.e.power = 500;
c.company = "hyundai";
c.color = "black";
c.show_details();
return 0;
}
#包括
使用名称空间std;
类引擎{
公众:
整数幂;
};
班车{
公众:
发动机e;
弦乐公司;
字符串颜色;
void show_details(){
cout在第二个示例中,您注释掉了e
//Engine e;
你注释掉了引擎e;
,傻!你忘了包含
标题,你注释掉了汽车类中的引擎e;
。@user2357112,真傻。我只是掌纹而已。你应该知道我花了多少时间格式化这个问题。
comp3.cpp: In member function ‘void Car::show_details()’:
comp3.cpp:22:44: error: ‘e’ was not declared in this scope
cout << "Engine horse power is: " << e.power << endl;
^
comp3.cpp: In function ‘int main()’:
comp3.cpp:26:9: error: ‘class Car’ has no member named ‘e’
c.e.power = 500;
//Engine e;