-1

i am new to Cpp and am having this error:

Error   C2011   'point2d': 'struct' type redefinition

it is the first time i use modules, and i am having an error with the headers. Here is my code:

squarecell.cc:

#include <vector>
#include "squarecell.h"

using namespace std;

struct point2d {

    point2d(int x, int y) {
        X = x;
        Y = y;
    }
    point2d() {
        X = 0;
        Y = 0;
    }
    int X;
    int Y;
};

squarecell.h:

#pragma once

#include <iostream>
#include <vector>

using namespace std;

struct point2d {
    point2d(int x, int y);
    point2d();
    int X;
    int Y;
};

I tried this in the header:

#pragma once

#include <iostream>
#include <vector>

using namespace std;

#ifndef point2d_HEADER
#define point2d_HEADER

struct point2d {
    point2d(int x, int y);
    point2d();
    int X;
    int Y;
};
#endif

Also didnt work, I searched everywhere, i know im doing something wrong but i cant figure it out.

Any help would be much appreciated,

Karl

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

1

The issue is in the source file, not the header.

Implementations are done like this:

point2d::point2d(int x, int y) { ... }

Not like this:

struct point2d {
    point2d(int x, int y) { ... }
};
POBIX
  • 96
  • 1
  • 3
0

You defined the structure struct point2d several times.

At first it is defined in the header "squarecell.h" and then it is redefined at least in the module squarecell.cc.

Leave the structure definition only in the header and remove the structure definition in each module where it is redefined. Instead include the header in each module where the structure definition is required.

The constructors can be defined either within the structure definition in the header (in this case they will be inline functions) or place their definitions only in one module like

#include "squarecell.h"

point2d::point2d(int x, int y) : X( x ), Y( y ) 
{
}
point2d::point2d() : X(), Y()
{
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335