【问题标题】:C++ Constructor Code is working fine on Windows but not compiling on MacOSC++ 构造函数代码在 Windows 上运行良好,但在 MacOS 上无法编译
【发布时间】:2022-01-20 09:54:37
【问题描述】:

这是代码:

#include<iostream>
#include<string>
#include<vector>

enum class OrderBookType{bid, ask};

class OrderBookEntry{
    public:

        OrderBookEntry( double _price,
                        double _amount,
                        std::string _timeStamp,
                        std::string _product,
                        OrderBookType _orderType)
        {
            price = _price;
            amount= _amount;
            timeStamp= _timeStamp;
            product= _product;
            orderType= _orderType;
        }

        double price;
        double amount;
        std::string timeStamp;
        std::string product;
        OrderBookType orderType;

};

int main(){

    OrderBookEntry order1{102000.01, 0.0001, "2020/03/17 17:01:24.884492", "ETH/BTC", OrderBookType::bid};

    std::cout << "price: " << std::fixed << order1.price << std::endl;

    return 0;
}

我遇到的错误:

main.cpp:5:6: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class OrderBookType{bid, ask};
     ^
main.cpp:33:20: error: no matching constructor for initialization of 'OrderBookEntry'
    OrderBookEntry order1{102000.01, 0.0001, "2020/03/17 17:01:24.884492", "ETH/BTC", OrderBookType::bid};
                   ^
main.cpp:7:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class OrderBookEntry{
      ^
main.cpp:10:9: note: candidate constructor not viable: requires 5 arguments, but 0 were provided
        OrderBookEntry( double _price,
        ^
main.cpp:33:26: error: expected ';' at end of declaration
    OrderBookEntry order1{102000.01, 0.0001, "2020/03/17 17:01:24.884492", "ETH/BTC", OrderBookType::bid};
                         ^
                         ;
1 warning and 2 errors generated.

【问题讨论】:

  • 请添加编译行(clang ...gcc ...
  • 无法重现 godbolt.org/z/oPe4G7oo7 您的编译器默认使用旧的 C++ 标准。添加开关-std=c++11(或更好)。
  • 你应该能够只使用-std=c++11 标志进行编译。
  • 您需要将xcode中的目标标准设置为C++11或更高版本。
  • 仔细检查您正在使用的编译器的版本。它必须至少支持c++11。当 C++11 首次出现时,编译器的默认设置是在 C++03 模式下构建,直到您设置编译器命令行标志 -std=c++11。新编译器提高了默认语言级别,因此您不需要这样做。如果可以的话,我强烈建议您使用更新的编译器,因为从那时起该语言(及其对它的支持)已经有了很大的改进。

标签: c++ macos constructor


【解决方案1】:

正如编译器错误所指出的,枚举类仅从 C++ 11 开始受支持,因此在编译时,必须将标志/开关或参数传递给 C++ 编译器以使用该版本的语言语法,然后只有它会编译你的 OrderBookType::bid 构造函数中传递的最后一个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多