这是我的代码。编译时出现错误
invalid declarator before ‘geometry’
在第 16 行和第 48 行,我不确定自己做错了什么。请指教。
#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;
}
我正在为工厂方法类编写代码,但我在“几何”之前收到此错误作为无效声明符。我不确定我做错了什么
最佳答案
对于在 C++ 中遇到类似“invalid declarator before”问题的人,即使您编写的语法看起来不错,请检查上一行中的分号。
关于c++11 - C++ 编译器错误 : “invalid declarator before” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59285497/