3

I have two different structs which I want to convert to each other like this:

PointI a = PointI(3,5);
PointF b = a;

I assume that I will need to do something like the code below:

struct PointF
{
    PointF operator=(PointI point){
        x = point.x;
        y = point.y;
        return *this;
    }
    float x, y;
};

struct PointI
{
    PointI operator=(PointF point)
    {
        x = point.x;
        y = point.y;
        return *this;
    }
    int x, y;
};

But the problem is that PointF uses PointI before it is declared. From what I've read in other questions, I understand that I can declare PointI before defining both structs, and then use a pointer. Though it seems that I won't be able to access the variables x and y from that pointer, as these are not defined yet.

Is there a way I can add these variables to the struct declaration before defining them? Or is there a better way to solve this problem?

Michiel
  • 180
  • 2
  • 14
  • 3
    possible duplicate of [What is forward declaration in c++?](http://stackoverflow.com/questions/4926105/what-is-forward-declaration-in-c) – NathanOliver Jun 08 '15 at 16:10
  • 1
    You need to declare both structs first, and define them later (i.e. operation = function definitions need to be done later) – OMGtechy Jun 08 '15 at 16:12
  • @NathanOliver this requires a little more than just a forward declaration, but I agree there is some crossover between the two – OMGtechy Jun 08 '15 at 16:22
  • Just as a side note, the idiomatic `operator=` overload should return a *reference* to `*this`, not a copy of it. – Emil Laine Jun 08 '15 at 18:44

1 Answers1

10

First, forward declare one of the structs and fully declare the other. You'll need to use either a reference or pointer for the forward declared type, since the compiler doesn't have its definition yet:

struct PointI;
struct PointF
{
    PointF operator=(const PointI& point);
    float x, y;
};

Next, you need to fully declare the struct you forward declared:

struct PointI
{
    PointI operator=(const PointF& point);
    int x, y;
};

Now you can go ahead and define the operator= functions for each:

PointF PointF::operator=(const PointI& point)
{
    x = point.x;
    y = point.y;
    return *this;
}

PointI PointI::operator=(const PointF& point)
{
    x = point.x;
    y = point.y;
    return *this;
}

Note that you should change your operator= functions to return references rather than copies, but that's outside the scope of this question/answer.

OMGtechy
  • 7,935
  • 8
  • 48
  • 83
  • Hm, when I insert this in my project, I get some weird errors like "Error 68 error LNK1169: one or more multiply defined symbols found" and "Error 58 error LNK2005: "public: struct PointF __thiscall PointF::operator=(struct PointI const &)" (??4PointF@@QAE?AU0@ABUPointI@@@Z) already defined in Camera.obj" But it seems to work in a new project, so I guess I'll have to figure the problem here out myself. Thanks! – Michiel Jun 08 '15 at 17:37
  • @Michiel sounds like you're breaking the one definition rule, make sure you're not compiling the file with the implementation in more than once! – OMGtechy Jun 08 '15 at 17:58
  • Well, as far as I know, I don't have PointI and PointF defined anywhere else in the project, and I have the header file that contains them include guarded. I don't think there is any other way I can break the one definition rule, or am I wrong there? – Michiel Jun 08 '15 at 18:26
  • @Michiel I'd offer to make a chat room to help you out, but you don't have enough rep to get into one. Make sure you're not doing anything like compiling the file twice or including a cpp file somewhere. – OMGtechy Jun 08 '15 at 18:30
  • As far as I know, I'm not compiling the file twice. I tried copying the piece of code to a new header, calling the structs PointF1 and PointI1. It seems that the error appears again if I include the new header file in Main.h, but doesn't seem to appear if I include the header file in Main.cpp. I think the problem might have something to do with include guarding, but both Main.h and Point.h are include guarded... – Michiel Jun 08 '15 at 19:25
  • @Michiel you should post a separate question with the code in question, I'll do what I can :) – OMGtechy Jun 08 '15 at 19:29
  • @OMGtechy You might want to mention that the declarations should be in header files and implementations should be in .cpp files – NathanOliver Jun 08 '15 at 19:37
  • After looking a bit at what header guards actually do, I realized that they do not solve multiple definition errors and only ensure that a header file won't be included twice in the same cpp file... I feel a bit stupid for having been stuck here for this long, but I got everything working now. Thanks for your help! I'm glad I can continue on my project now. @NathanOliver Well, I just discovered that the painful way... I should've kept an eye on the question... Thanks! – Michiel Jun 08 '15 at 19:56