2

I've encountered these two error when trying to compile..

anyone knows whats wrong ?

Was thinking maybe I #include the wrong header file ? the sample of the codes and error as per following:

Error:

Square.cpp:8: error: redefinition of ‘Square::Square(bool, Point*, std::string, int)’
Square.h:21: error: ‘Square::Square(bool, Point*, std::string, int)’ previously defined here
Square.cpp: In member function ‘Point Square::getCoord()’:
Square.cpp:22: error: expected primary-expression before ‘]’ token
Square.cpp: In member function ‘void Square::setCoord(Point*)’:
Square.cpp:32: error: expected primary-expression before ‘]’ token
Square.cpp:32: error: expected primary-expression before ‘]’ token

cpp file

#include "Square.h"`
#include <cmath>
using namespace std;

Square::Square(bool containsWarpSpace, Point coord[], string shapeName, int vertPoint):ShapeTwoD(shapeName, containsWarpSpace) {

 vertPoint = vertPoint;
 coord[] = coord[];

}

int Square::getVertPoint()
{
    return vertPoint;
}

Point Square::getCoord()
{
    return coord[];
}

void Square::setVertPoint(int verticleP)
{
    vertPoint = verticleP;
}

void Square::setCoord(Point coord[])
{
    coord[] = coord[];
}

header:

#include "ShapeTwoD.h"

class Square : public ShapeTwoD
{
    private:
        int vertPoint;
        Point coord[];

    public:
        //Accessor
        int getVertPoint();
        Point getCoord();

        //Mutator
        void setVertPoint(int vertP);
        void setCoord(Point coord[]);

        //virtual member
        virtual double computeArea(Point x, Point y);

        Square(bool containsWarpSpace, Point coord[], std::string shapeName = "Square", int vertPoint = 4):ShapeTwoD(shapeName, containsWarpSpace){}

};
4

3 回答 3

4

您定义了两次构造函数,一次在标头中,一次在实现文件中。在标题中,您只需要像这样声明它:

Square(bool containsWarpSpace,
       Point coord[],
       std::string shapeName = "Square",
       int vertPoint = 4);

您还需要修复 的处理coord,也许像更改coord

Point* coord;

并使用

Point* Square::getCoord()
{
    return coord;
}

this->coord = coord;

在构造函数和setCoord().

请注意,您的处理方式coord对我来说似乎很奇怪和危险,但是如果没有关于您实际尝试做什么的进一步信息,很难给出具体建议。通常,考虑使用标准容器而不是手动内存/阵列管理。

于 2013-10-27T15:38:25.897 回答
2

编译器清楚地告诉您问题:
您在头文件和 cpp 文件中定义了两次构造函数。

另外,您到底打算做什么:

coord[] = coord[];

你应该理解你编写的每一个代码语句。想一想,你打算用这个语句做什么?& 然后将其与您所学的语言语法相匹配。

于 2013-10-27T15:38:29.207 回答
0

源文件:

Square::Square(bool containsWarpSpace, Point coord[],
               string shapeName, int vertPoint)
   :ShapeTwoD(shapeName, containsWarpSpace)
{
    vertPoint = vertPoint;
    coord[] = coord[];
}

头文件:

Square(bool containsWarpSpace, Point coord[], 
       std::string shapeName = "Square", int vertPoint = 4)
    :ShapeTwoD(shapeName, containsWarpSpace)
{}

看起来像是同一功能的两个不同版本。
头文件中的那个调用基类构造函数,但在构造函数的主体中没有任何代码。

于 2013-10-27T16:40:49.570 回答