c ++类 - 冲突的声明错误

时间:2011-07-30 16:54:12

标签: c++ class static

我刚开始用C ++学习类,我在下面的代码中得到了这个错误:conflicting declaration 'std::string PizzaOrder::toppings_offered'

有人可以在这里解释我的代码有什么问题吗?

class PizzaOrder
{
public:
    //all the toppings that are offered, constant array of strings
    static string toppings_offered[5];
    static double topping_base_cost;
};

string PizzaOrder::toppings_offered = {"onions", "bell peppers", "olives", "spinach", "tomatoes"};
double PizzaOrder::topping_base_cost = 0.50;

4 个答案:

答案 0 :(得分:1)

您忘记了toppings_offered是一个字符串数组,而不是字符串:

string PizzaOrder::toppings_offered[5] = {"onions", ... };
//                                  ^
//                                  |

(顺便说一句,我希望披萨订单订购配料,而不是提供它们。难道你的设计仍然有点混乱?)

答案 1 :(得分:0)

string PizzaOrder::toppings_offered[5]

而不是

string PizzaOrder::toppings_offered

答案 2 :(得分:0)

您必须在定义中使用与声明中相同的类型:

string PizzaOrder::toppings_offered[5] = { ... };

类型为string[5],而不是string

答案 3 :(得分:-1)

你不能这样做。您需要通过调用

来创建类的实例
PizzaOrder myPizzaOrder = new PizzaOrder();
myPizzaOrder.topping_offered {"","",""};
相关问题