【发布时间】: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