c++11 - C++ 编译器错误 : “invalid declarator before”

标签 c++11 compiler-errors shared-ptr factory-method make-shared

这是我的代码。编译时出现错误

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/

相关文章:

c++ - weak_ptr C++ 中的比较运算符

Java:找不到符号(Array 类)

c++ - htmlcxx 编译错误

xml - 构建SLN : CS0008 - CS0115的奇怪错误

c++ - 如何避免每次构造或重置时都需要为 std::shared_ptr 指定删除器?

c++ - 将 mem_fun_ref 与 boost::shared_ptr 一起使用

c++ - 从左和右对数组求和时平衡数组索引

c++ - 枚举成员类型是否仍然依赖于实现?

c++ - 为什么是 "move semantics"而不是简单的 memcpy?

c++ - shared_ptr 交换线程安全吗?