Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;给属性赋值_C++ - Fatal编程技术网

C++ C++;给属性赋值

C++ C++;给属性赋值,c++,C++,这似乎是一个显而易见的答案……但我正在尝试为我的属性赋值。 即: 我正在尝试为主菜等制作相同的_starter=“starter”…但是我得到一个错误,说“make _starterstatic” 我还尝试过分类(_starter=“starter”);这也不管用 您不能在类声明中定义成员的值,您需要将其声明为静态并在定义(即.cpp)文件中定义,或者在构造函数中定义它。您可以将初始值作为参数传递给构造函数 // ... // constructor: category ( con

这似乎是一个显而易见的答案……但我正在尝试为我的属性赋值。 即:

我正在尝试为主菜等制作相同的_starter=“starter”…但是我得到一个错误,说“make _starterstatic”


我还尝试过分类(_starter=“starter”);这也不管用

您不能在类声明中定义成员的值,您需要将其声明为静态并在定义(即.cpp)文件中定义,或者在构造函数中定义它。

您可以将初始值作为参数传递给构造函数

// ...
// constructor:
category
       ( const char * starter
       , const char * maincourse
       , const char * pudding
       , const char * drink
       , const char * itemDescription
       , const char * price
       )
       : _starter(starter)
       , _maincourse(maincourse)
       , _pudding(pudding)
       , _drink(drink)
       , _itemDescription(itemDescription)
       , _price(price)
       {}
此外,您还可以在以下表单中为数据项创建访问器函数:

 public:
     string& Starter() { return _starter; } // non-const accessor
     const string& Starter() const { return _starter; } // const accessor.
     // ...

把它放在class
category
的构造函数中。。category::category(){string _starter=“starter”}?是的,但没有类型声明(即,没有
字符串
)。。。或者如我在下面给出的答案所示(
category():_starter(“starter”){…}
)。或者,如果您希望在所有
类别
对象中共享该属性的相同实例,则只需将其声明为
静态
 public:
     string& Starter() { return _starter; } // non-const accessor
     const string& Starter() const { return _starter; } // const accessor.
     // ...