0

I've the following class called as Point which also contains an inner class called as PointFactory. The Point class has a static instance of the inner class PointFactory as Factory present inside it.

    class Point{

    private:
        float x, y;
        Point(float a, float b){
            x = a;
            y = b;
        }

    public:
        struct PointFactory{

                PointFactory(){}

                Point newCartesian(float x, float y){
                    return {x, y};
                }

                Point newPolar(float rho, float theta){
                    return {rho*cos(theta), rho*sin(theta)};
                }
            };
        static PointFactory Factory();
    };

I'm trying to access the methods - newCartesian and newPolar from the the static instance Factory as follows:

Point point = Point::Factory.newCartesian(10, 20);
Point point2 = Point::Factory.newPolar(100, 200);

But, I get 2 errors saying that the methods newCartesian and newPolar couldn't be resolved. I understand the static variables need to be initialized and I believe I've done that.

I would like to know the reason for the above mentioned errors.

  • You use `Point::Factory`, but what *is* `Point::Factory`? Is it an object? – Some programmer dude Jan 14 '19 at 10:52
  • Yes, it's a static object, which is defined as: `static PointFactory Factory();` in the Point class. – Shubham Urkade Jan 14 '19 at 10:54
  • 3
    No it's not a static object. It's a static ***function***. And as all functions it needs to be *called*. – Some programmer dude Jan 14 '19 at 10:54
  • You might want to read [this](https://en.wikipedia.org/wiki/Most_vexing_parse) – paler123 Jan 14 '19 at 10:57
  • 2
    Not directly related but that factory idea is bad. check that: http://coliru.stacked-crooked.com/a/2aa2de8e4a219c57 Variations possible. – YSC Jan 14 '19 at 10:57
  • @Someprogrammerdude I've also tried to change it as: `static PointFactory Factory;`. But, now I get the following error during run-time: `undefined reference to `Point::Factory'` – Shubham Urkade Jan 14 '19 at 11:01
  • For that error, dup: https://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member – YSC Jan 14 '19 at 11:04
  • I see what you're trying to do. Here is an alternate solution that kindi keep your syntax: https://pastebin.com/rGDJzNeu – Tezirg Jan 14 '19 at 11:07

0 Answers0