gpt4 book ai didi

c++ - "No default constructor exists "尽管我不需要它

转载 作者:行者123 更新时间:2023-11-28 04:08:02 24 4
gpt4 key购买 nike

<分区>

我想问为什么我的编译器说“类对象不存在默认构造函数”,即使我在该类中的任何地方都不需要它。

我不应该使用默认构造函数(它明确表示我只需要一个带有参数 id,full assignment can be found on this link 的参数化构造函数)

我试图思考它需要默认构造函数的原因,但我就是看不到它。就像,即使在 header 中我也需要对象的构造函数。

我也一直在搜索谷歌,但它也没有回答我的问题,或者即使它回答了我也无法理解它并将其应用于我的示例。

这是我的 Object.h

#pragma once
#ifndef OBJECT_H
#define OBJECT_H
class Object
{
public:
Object(int aId);
virtual ~Object() {};
int getId() const;
double getX() const;
double getY() const;
void setX(double aX);
void setY(double aY);
private:
int id;
double x;
double y;
};

#endif //!OBJECT_H

这是我的 Object.cpp

#include "Object.h"

Object::Object(int aId)
{
this->id = aId;
}

int Object::getId() const
{
return this->id;
}

double Object::getX() const
{
return this->x;
}

double Object::getY() const
{
return this->y;
}

void Object::setX(double aX)
{
this->x = aX;
}

void Object::setY(double aY)
{
this->y = aY;
}

这是我遇到错误的类的头文件

#pragma once
#ifndef STATIC_OBJECT_H
#define STATIC_OBJECT_H

#include "Object.h"

enum class ObstacleType { Rock, SmallFlower, BigFlower };
class StaticObject : public Object {

public:
StaticObject(int aId, ObstacleType aObstacleType);
ObstacleType& getObstacleType();
private:
ObstacleType obstacleType;
};


#endif // !STATIC_OBJECT_H

在以括号开头的第 4 行,我收到错误消息“类 Object 中不存在默认构造函数”,即使我在那里不需要它,即使我没有在它保留的 block 中放置任何东西这么说。

#include "StaticObject.h"

StaticObject::StaticObject(int aId, ObstacleType aObstacleType)
{ // <-- compilator error shows here
Object* obj = new Object(aId);
this->obstacleType = aObstacleType;
}

ObstacleType& StaticObject::getObstacleType() {
return this->obstacleType;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com