C++继承类没有显示默认构造函数

Can*_*vas 2 c++ inheritance

我正在创建一些类,我决定创建一个基本类,其他类将继承该基本类

所以这是我的基本类头

#pragma once

#include "ImageService.h"

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

    void Update();

};
Run Code Online (Sandbox Code Playgroud)

不要担心方法,它们不是问题.所以现在我继续创建像这样的IntroState(头文件)

#pragma once

#include "State.h"

class IntroState : public State
{
public:
    IntroState(ImageService& imageService);
    ~IntroState();

    GameState objectState;
};
Run Code Online (Sandbox Code Playgroud)

这是cpp文件

#include "IntroState.h"


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


IntroState::~IntroState()
{
}
Run Code Online (Sandbox Code Playgroud)

在构造函数中它声明"没有类"State""的默认构造函数,现在我认为正在进行的是,State的默认构造函数需要传递给它的imageService引用.那么我如何将此构造函数中的imageservice传递给状态构造函数?

jua*_*nza 7

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

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

}
Run Code Online (Sandbox Code Playgroud)