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
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With