Error in C++ : redefinition of class constructor

c++
10,883

Solution 1

You are defining the constructor twice, once in the header and once in the implementation file. In the header, you just need to declare it like this:

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

You also need to fix the handling of coord, maybe something like changing coord to

Point* coord;

and use

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

and

this->coord = coord;

in the constructor and setCoord().

Please note that your way of handling coord seems strange and dangerous to me, but without further information about what you are actually trying to do it's hard to give specific advise. Generally, consider using the standard containers over manual memory/array management.

Solution 2

The compiler clearly tells you the problem:
You defined the constructor twice once in header file and once in cpp file.

Also, What exactly do you intend to do with:

coord[] = coord[];

You should understand each and every statement of code that you write. Think about, What do you intend this statement to do? & then match it to the language grammar that you learnt.

Share:
10,883
Author by

Alien

Updated on July 22, 2022

Comments

  • Alien over 1 year

    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){}
    };
    
  • srsyogesh about 10 years
    also looks like coord[] = coord[]; will cause issue, Shouldnt this be coord = coord; ? I mean where ever coord is used , looks like its used in a wrong way
  • mah
    mah about 10 years
    @srsyogesh perhaps this->coord = coord;? Unless C++ compiler technology is smart enough to know when you mean the local variable and when you mean the class variable these days? (I'm not saying it isn't but this would be the first time I've heard of that.)
  • Daniel Frey
    Daniel Frey about 10 years
    @Alien Missed the second question/error, sorry. Updated the answer.
  • srsyogesh about 10 years
    ah well , even i am not sure how C++ compiler works or the syntax of c++ . But looking at it i am sure what is defined in the post is not the correct way .
  • Alien about 10 years
    i've try removing the [] in coord but theres another error i nv seen before Square.cpp: In member function ‘Point Square::getCoord()’: Square.cpp:17: error: conversion from ‘Point*’ to non-scalar type ‘Point’ requested Square.cpp: In member function ‘void Square::setCoord(Point*)’: Square.cpp:27: error: incompatible types in assignment of ‘Point*
  • srsyogesh about 10 years
    @Alien It would be better if you tell us what do you want to return either a point array or a particular point object itself. If you want to return array then removing [] is not an option.
  • Alien about 10 years
    @srsyogesh i would like to return an array
  • Alien about 10 years
    @DanielFrey thanks for helping. i've tried using the suggested answer for 2nd error but it doesnt seems to be working Square.cpp:15: error: expected unqualified-id before ‘[’ token Square.cpp: In member function ‘void Square::setCoord(Point*)’: Square.cpp:27: error: incompatible types in assignment of ‘Point*’ to
  • Daniel Frey
    Daniel Frey about 10 years
    @Alien Yeah, I missed another bit. See the updated answer to make it compile but please also consider the last paragraph.