質問

I have seen other people asking this question before, but the answers they received were unique to their programs and unfortunately do not help me.

Firstly, I have a shape class - split into .h and .cpp files

//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;
}

I keep getting an error in my Shape.h class that says 'Shape' : 'class' type redefinition. Any idea why I might be getting this error?

役に立ちましたか?

解決

add include guard to your header file

#ifndef SHAPE_H
#define SHAPE_H

// put your class declaration here

#endif

And the way you initialize member mColor is incorrect. You can't assign NULL to string type

Shape::Shape(const string& color) : mColor(color) {
}

Add virtual destructor to Shape class as it serves as a base with virtual functions.

Also, do NOT use using directive in header file.

他のヒント

It seems like you want to write an abstract base class here, but is there any other files you compiled but not showed here?


You must include “shape.h” twice more. Just use macros to prevent this case.

PS:I guess Rectangle is base class of Square,and also inherited Shape.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top