我之前见过其他人问过这个问题,但他们收到的答案对他们的程序来说是独一无二的,遗憾的是我没有帮助.
首先,我有一个形状类 - 分为.h和.cpp文件
//Shape.h
#include <string>
using namespace std;
class Shape
{
private:
string mColor;
public:
Shape(const string& color); // constructor that sets the color instance value
string getColor() const; // a const member function that returns the obj's color val
virtual double area() const = 0;
virtual string toString() const = 0;
};
Run Code Online (Sandbox Code Playgroud)
//Shape.cpp
#include "Shape.h"
using namespace std;
Shape::Shape(const string& color) : mColor(NULL) {
mColor = color;
}
string Shape::getColor() const
{
return mColor;
}
Run Code Online (Sandbox Code Playgroud)
我的Shape.h类中一直出现错误,上面写着'Shape':'class'类型重定义.知道为什么我会收到这个错误吗?
添加include guard到您的头文件
#ifndef SHAPE_H
#define SHAPE_H
// put your class declaration here
#endif
Run Code Online (Sandbox Code Playgroud)
初始化成员mColor的方式不正确.您不能将NULL分配给字符串类型
Shape::Shape(const string& color) : mColor(color) {
}
Run Code Online (Sandbox Code Playgroud)
将虚拟析构函数添加到Shape类,因为它充当具有虚函数的基础.
另外,请勿在头文件中使用using指令.
| 归档时间: |
|
| 查看次数: |
3661 次 |
| 最近记录: |