【发布时间】:2014-04-10 00:46:45
【问题描述】:
我以前见过其他人问过这个问题,但他们收到的答案是他们的程序所独有的,很遗憾对我没有帮助。
首先,我有一个形状类 - 分为 .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;
};
//Shape.cpp
#include "Shape.h"
using namespace std;
Shape::Shape(const string& color) : mColor(NULL) {
mColor = color;
}
string Shape::getColor() const
{
return mColor;
}
我的 Shape.h 类中不断出现错误,显示“Shape”:“class”类型重新定义。 知道为什么我可能会收到此错误吗?
【问题讨论】:
-
它是否也指定了行?如果是这样,会发生什么?
-
Shape.h有包含保护吗? -
它指定第 6 行恰好是 Shape 类之后的左括号。而且我没有包含保护 - 我不确定那是什么?
-
题外话:修改你的getColor方法为: const string& getColor() const { return mColor;返回字符串的副本不是最优的。
-
不要使用
#pragma once。这是非标准的。使用#ifndef SHAPE_H...
标签: c++ redefinition