Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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++_Inheritance - Fatal编程技术网

C++ C++;继承类不显示默认构造函数

C++ C++;继承类不显示默认构造函数,c++,inheritance,C++,Inheritance,我创建了几个类,我决定创建一个基本类,其他类将继承这个基本类 这是我的基本类标题 #pragma once #include "ImageService.h" class State { public: State( ImageService& is ); ~State(); void Update(); }; 不要担心这些方法,它们不是问题所在。 现在我继续创建一个类似于So(头文件)的IntroState 这是cpp文件 #include "Intr

我创建了几个类,我决定创建一个基本类,其他类将继承这个基本类

这是我的基本类标题

#pragma once

#include "ImageService.h"

class State
{
public:
    State( ImageService& is );
    ~State();

    void Update();

};
不要担心这些方法,它们不是问题所在。 现在我继续创建一个类似于So(头文件)的IntroState

这是cpp文件

#include "IntroState.h"


IntroState::IntroState(ImageService& imageService)
{
    //error here
}


IntroState::~IntroState()
{
}

在构造函数中,它声明“没有类“State”的默认构造函数”,现在我认为是这样的,State的默认构造函数需要传递一个imageService引用给它。那么,如何将此构造函数中的imageservice传递给状态构造函数呢?

您的基类没有默认构造函数,这是在当前派生类构造函数中隐式调用的。您需要显式调用基的构造函数:

IntroState::IntroState(ImageService& imageService) : State(imageService)
{

}
通常的方式是:

IntroState::IntroState(ImageService& imageService)
    : State(imageService)
{
}

您也应该调用
State
的构造函数,如下所示:

IntroState::IntroState(ImageService& imageService)
    : State(imageService) {
}
提示:不要使用:

#pragma一次
,使用防护装置

例如:

#ifndef GRANDFATHER_H
#define GRANDFATHER_H

class A {
    int member;
};

#endif /* GRANDFATHER_H */
您可以阅读有关中包含卫士的更多信息


您看到的是不是标准的。在
C++11
()中,两者都没有起作用。

我只是简单地键入了它,它就起作用了:D,为快速回复干杯,虽然+1格式很好,实际上,几乎是自己回答了问题。
#ifndef GRANDFATHER_H
#define GRANDFATHER_H

class A {
    int member;
};

#endif /* GRANDFATHER_H */