c++11compiler-errorsshared-ptrfactory-methodmake-shared

C++ compiler error: “invalid declarator before”


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


Solution

  • 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.