0

I have a class

class Cartesian
{
    float x,y,z;
 };

and another class

class Spherical
{
    float rho, phi, r;
};

How would I convert between them? I have tried adding Spherical Cartesian::toSpherical and Cartesian Spherical::toCartesian inside of the class declarations but no matter the order I put them in, the first one complains that the other one is undefined. I have tagged VS and Ubuntu because I want it to work for both.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 3
    related/dupe: http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies – NathanOliver Apr 18 '17 at 16:24
  • Are these in the same file? If so, think about moving them to separate files and including the headers. If they are in different files, you are most likely missing an include or the link info. – mascoj Apr 18 '17 at 16:26
  • @NathanOliver but if I do `class Cartesian;`, then `class Spherical{}` in Spherical I can't assess the members of Cartesian to convert. – William Thompson Apr 18 '17 at 16:29
  • @WilliamThompson That is why you use header and cpp files. You put the declaration in the header files and the put the actual code that needs the hard instances in cpp files that include those header files. Sometimes that requires a rework of the interface but that is just how circular references work in C++. – NathanOliver Apr 18 '17 at 16:32
  • @WilliamThompson Please provide [mcve]. Your example is minimal, but not complete, and because of that - not verifiable. – Algirdas Preidžius Apr 18 '17 at 16:33
  • @NathanOliver thank you I feel dumb... – William Thompson Apr 18 '17 at 16:34

1 Answers1

1

Do the forward declaration.

    class Spherical;
    class Cartesian 
    { 
       float x,y,z;
       Spherical toSpherical();
    };

    class Spherical
    { 
       float rho, phi, r; 
       Cartesian toCartesian();
    };
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38