Search code examples
c++structconstructorredefinition

Error C2011, Tried everything already asked on here


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


Solution

  • 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) { ... }
    };