C++ compiler error: “invalid declarator before”
Asked Answered
B

4

10

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

Baer answered 11/12, 2019 at 12:7 Comment(1)
Please align your code for quicker readingCaddish
M
37

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.

Mood answered 3/7, 2021 at 7:22 Comment(1)
Spot on! It can even be the declaration of a struct ;), keep note of that.Dropline
E
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());
Eury answered 11/12, 2019 at 12:40 Comment(1)
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)Baer
C
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.

Caddish answered 11/12, 2019 at 12:38 Comment(0)
L
0

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

Leticia answered 28/2, 2022 at 7:4 Comment(1)
This is the same solution as in this other answer.Rasp

© 2022 - 2024 — McMap. All rights reserved.