C++继承类没有显示默认构造函数
C++ inherit class shows no default constructor
我正在创建几个类,我决定创建一个基本类,其他类将继承该基本类
这是我的基本类标题
#pragma once
#include "ImageService.h"
class State
{
public:
State( ImageService& is );
~State();
void Update();
};
不要担心方法,它们不是问题所在。所以现在我继续创建一个IntroState(头文件(
#pragma once
#include "State.h"
class IntroState : public State
{
public:
IntroState(ImageService& imageService);
~IntroState();
GameState objectState;
};
这是cpp文件
#include "IntroState.h"
IntroState::IntroState(ImageService& imageService)
{
//error here
}
IntroState::~IntroState()
{
}
在构造函数中,它声明"没有类"State"的默认构造函数",现在我认为正在发生的事情是,State的默认构造函数需要一个传递给它的imageService引用。那么,我该如何将该构造函数中的imageService传递给State构造函数呢?
基类没有默认构造函数,这是在当前派生类构造函数中隐式调用的构造函数。您需要显式调用基的构造函数:
IntroState::IntroState(ImageService& imageService) : State(imageService)
{
}
常用方法:
IntroState::IntroState(ImageService& imageService)
: State(imageService)
{
}
您也应该调用State
的构造函数,如下所示:
IntroState::IntroState(ImageService& imageService)
: State(imageService) {
}
提示:不要使用:
#pragma once
,使用防护装置!
示例:
#ifndef GRANDFATHER_H
#define GRANDFATHER_H
class A {
int member;
};
#endif /* GRANDFATHER_H */
你可以在wikipedia中阅读更多关于include guards的信息。
您可以看到#pragma
不是标准的。两者均未出现在C++11
(链接(中。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- Switch语句不显示除默认语句之外的任何类型的输出
- CMFCToolBar 和 CMFCMenuBar 默认不显示动态项目
- 如何使用C Win32 API在Combobox中默认显示域值
- 显示状态栏默认消息
- 如何使用C Winapi 32在默认值中显示Combobox第二值
- 聚合类正在显示默认构造函数
- 如何显示默认 c++ 对象值
- 显示错误的 QT 默认项目
- C++构造函数并使其显示默认答案
- 访问Ximea相机并使用OpenCV设置预定义的分辨率时,由于相机默认分辨率的大小为Mat,因此显示混乱的输出
- 在变量上显示默认文本,用户可以在C++命令行程序上编辑这些变量
- 默认项目显示在不可编辑的组合框中
- 为什么当窗口在win32上失去焦点时,被覆盖的非客户端区域显示默认值
- 自动完成不显示在xcode中有默认值的函数参数
- 增强构建显示默认工具集和版本
- 显示默认值
- C++继承类没有显示默认构造函数
- 需要一个示例来显示默认构造函数不是继承的
- 如何在QTableView的默认视图中显示超文本?