c++ - C++抽象基类的重新定义
问题描述
使用派生类可以构建的构造函数实现抽象基类。我在编译代码时遇到问题,我提前为使用命名空间 std 表示歉意;这是我的任务所必需的。
我尝试了 headerguards 并检查了我的包含代码。我将这些文件分成一个主文件(Assn2)、抽象基类(S2D).h 和 .cpp 文件。
在主文件 Assn2 中
#include <iostream>
#include <string>
#include <fstream>
#include "S2D.h"
在 S2D.h 内
#ifndef _S2D_H_
#define _S2D_H_
#include <iostream>
#include <string>
using namespace std;
class ShapeTwoD {
private:
string name;
bool containsWarpSpace;
public:
ShapeTwoD();
ShapeTwoD(string, bool);
在 S2D.cpp 内
#include <iostream>
#include <string>
#include "S2D.h"
using namespace std;
class ShapeTwoD {
ShapeTwoD::ShapeTwoD() {
name = " ";
containsWarpSpace = false;
}
ShapeTwoD::ShapeTwoD(string Name, bool ContainsWarpSpace) {
name = Name;
containsWarpSpace = containsWarpSpace;
}
};
这是我收到的错误。
S2D.cpp:7:7: error: redefinition of ‘class ShapeTwoD’
class ShapeTwoD {
^~~~~~~~~
In file included from S2D.cpp:3:
S2D.h:7:7: note: previous definition of ‘class ShapeTwoD’
class ShapeTwoD {
^~~~~~~~~
make: *** [S2D.o] Error 1
只是一个后续问题,我正在尝试基于这个抽象基类实现派生类。我正在尝试基于这些抽象构造函数创建具有更多参数的派生类构造函数。
例如。
Rectangle::Rectangle(string Name, bool ContainsWarpSpace, int YSize, int XSize, int(*ArrY), int (*ArrX) )
我想知道我在 Java 中学到的这个概念是否适用于 C++?
解决方案
在 S2D.cpp 内
#include <iostream>
#include <string>
#include "S2D.h"
using namespace std;
class ShapeTwoD {// 删除这一行
ShapeTwoD::ShapeTwoD() {
name = " ";
containsWarpSpace = false;
}
ShapeTwoD::ShapeTwoD(string Name, bool ContainsWarpSpace) {
name = Name;
containsWarpSpace = containsWarpSpace;
}
};// 删除这一行
推荐阅读
- reactjs - Joi,验证子模式范围之外的引用
- reactjs - 单击链接后如何仅显示视图而不显示(视图+链接)?
- node.js - 为什么我一直看到警告“基于 GCLOUD_PROJECT 估计配置”
- angular - 如何通过 FormControl 之一按 FormArray 分组
- c++ - 如何使用 g3log 为每个函数或每个模块创建日志文件
- r - 如何从 stargazer 输出中删除模型类型?
- android - 缺少带有 ID 的必需视图,导航图 android
- jenkins - 从 jenkins 管道中拉取、编辑和推送到 git
- javascript - 在javascript中将变量传递给引号
- google-apps-script - 为什么当我将 GAS 链接到标准 GCP 时无法使用 DriveApp 方法?