6

Here's my code. When compiling I get the error

invalid declarator before ‘geometry’

at line 16 and line 48, I am not sure what I am doing wrong. Please advise.

#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class FactGeometry {   //Factory class
public:
    static std::shared_ptr<FactGeometry>geometry( int choice );
    virtual void calcArea() = 0;
};

class CalcRectangle :public FactGeometry {
    void calcArea() {
        double ll, bb, Area;
        std::cout << "\nEnter the length = ";
        std::cin >> ll;
        std::cout << "\nEnter the breadth = ";
        std::cin >> bb;
        Area = ll * bb;
        std::cout << "\nArea = " << Area;
    }
}; //end class

class CalcTraingle :public FactGeometry {
    void calcArea() {
        double bb, hh, Area;
        std::cout << "\nEnter the base = ";
        std::cin >> bb;
        std::cout << "\nEnter the height = ";
        std::cin >> hh;
        Area = 0.5 * bb * hh;
        std::cout << "\nArea = " << Area;
    }
};

FactGeometry std::shared_ptr<FactGeometry>geometry( int choice ) {
    switch ( choice ) {
    case 1: return shared_ptr<FactGeometry>( new CalcRectangle );
        break;
    case 2: return shared_ptr<FactGeometry>( new CalcTraingle );
        break;
    default: std::cout << "EXIT";
        break;
    }
} //end class

int main() {
    cout << "Hello World";
    int choice;
    std::vector<std::shared_ptr<FactGeometry>> table;
    while ( 1 ) {
        std::cout << "1. Rectangle 2. Triangle";
        std::cout << "Enter Choice :";
        std::cin >> choice;
        if ( ( choice != 1 ) || ( choice != 2 ) )
            break;
        else
            table.push_back( FactGeometry::make_shared<FactGeometry>geometry( choice ) );
    }
    for ( int i = 0; i < table.size(); i++ ) {
        table[i];
    }
    return 0;
}

I am writing code for Factory Method class but I am getting this error as invalid declarator before ‘geometry’.I am not sure what I am doing wrong

ChrisMM
  • 8,448
  • 13
  • 29
  • 48

4 Answers4

26

For people who are facing similar issue of “invalid declarator before” in C++ even if the syntax you wrote looks good, please check for semicolon in previous line.

1

There are several things wrong with your code.

The line the compiler complains about is the definition of the static geometry method of FactGeometry. The structure of a definition is:

<Return Type> <Class>::<Method Name>(<Parameters>) { ... }

geometry returns a shared_ptr<FactGeometry> and belongs to class FactGeometry. So it should be defined as

std::shared_ptr<FactGeometry> FactGeometry::geometry(int choice){
    // code
}

If you fix this, the compiler will complain about this line

table.push_back(FactGeometry::make_shared<FactGeometry>geometry(choice));

Here I think you do not understand what make_shared is supposed to accomplish. It is a helper function you can call to create a new shared_ptr of the desired type and takes the same arguments as that type's constructor. Since your FactGeometry is an abstract class make_shared<FactGeometry> won't work.

I suppose you want to call FactGeometry::geometry(choice).

Inside of geometry you can call e.g.

return make_shared<CalcRectangle>();

instead of

return shared_ptr<FactGeometry>(new CalcRectangle());
Devon Cornwall
  • 957
  • 1
  • 7
  • 17
  • Thanks a lot for the solution. Yes I was writing wrong signature prototype. This solution had worked for me. In the calling function argument passed is just as **FactGeometry::geometry(choice)** – Debdutta Halder Dec 12 '19 at 07:31
0

Check the signature of your static method geometry where you have defined it. Change it to

std::shared_ptr<FactGeometry> FactGeometry::geometry(int choice)

This should fix your compiler error.

Sisir
  • 4,584
  • 4
  • 26
  • 37
0

Please check if you have missed to add a semicolon (;) in any of the lines before

  • 2
    This is the same solution as in [this other answer](https://stackoverflow.com/a/68234073/2227743). – Eric Aya Feb 28 '22 at 10:39