Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;_C++_Composition_Scoping - Fatal编程技术网

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 <<

在C++中使用组合和类,我正处于摸索阶段。我遇到的一个代码片段以以下方式实现了组合:

#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;