在这里寻求集体智慧的意见 [英] seeking an opinion from the collective wisdom here
问题描述
我只是想知道这是不是形成错误的类标题:
#ifndef _items_h_
#define _items_h_
#include< iostream>
#include< string>
#include< vector>
使用命名空间std;
class Item
{
public:
Item(float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false ,\\ /
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false);
//以下是
的设置和获取函数//此类中的所有变量
void set_weight(float weight)
{item_weight = weight; } $ / $
float get_weight()
{return item_weight; }
void set_can_equip(bool set_can_equip)
{can_equip = set_can_equip; }
bool get_can_equip()
{return can_equip; }
void set_can_attack(bool set_can_attack)
{can_attack = set_can_attack; }
bool get_can_attack(){return can_attack; }
void set_can_defend(bool set_can_defend)
{can_defend = set_can_defend; }
bool get_can_defend(){return can_defend; }
void set_can_drink(bool set_can_drink)
{can_drink = set_can_drink; }
bool get_can_drink(){return can_drink; }
void set_can_eat(bool set_can_eat)
{can_eat = set_can_eat; }
bool get_can_eat(){return can_eat; }
void set_can_use(bool set_can_use)
{can_use = set_can_use; }
bool get_can_use(){return can_use; }
void set_is_consumable(bool set_is_consumable)
{is_consumable = set_is_consumable; }
bool get_is_consumable(){return is_consumable; }
void set_is_magical(bool set_is_magical)
{is_magical = set_is_magical; }
bool get_is_magical(){return is_magical; }
void describe(); //简单地返回描述(见下文)
void clear_describe(); //清除描述
void add_describe(string line_to_add);
/ *上面的行检查描述是否设置为
need_set或desc_cleared,如果是这样description.clear()
然后push_back(line_to_add),否则只需
push_back(line_to_add)* /
private:
//这些是为每个创建的项目设置的统计数据
string need_set; //用这个初始化描述
string desc_cleared; //描述将被设置为此
//清除后
vector< stringdescription;
//这是允许多个线描述
float item_weight; //这些都是非常自我解释我认为
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};
#endif
但是当我读到的时候,我对某些事感到困惑。上面的工作只是
罚款,但我也想知道构造函数应该是这样的:
项目(浮动权重= 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\\
bool set_is_magical = false);
在cpp文件中有这样的构造函数体:
Item :: Item(float weight,\
bool set_can_equip,\
bool set_can_attack,\
bool set_can_defend,\
bool set_can_drink,\
bool set_can_eat,\
bool set_can_use)
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set ="你需要在这里设置描述!\ n" ;;
desc_cleared ="描述已被描述已清除。\ n" ;;
description.clear();
description.push_back(need_set);
}
或者这个:
Item():浮动权重(0),\
bool can_equip(false),\
bool can_attack(false),\
bool can_defend(false),\
bool can_drink(false),\
bool can_eat(false),\
bool can_use(false),\
bool is_consumable(false),\
bool is_magical(false);
并丢失cpp文件中构造函数的整个主体?当我实例化
对象时,我将需要
来传递参数以覆盖默认值。
问题1:这是一个格式错误的类标题?
问题2:我应该将哪个用于初始化列表?
提前致谢。
DN
-
[我的电子邮件中没有x']
我有权留下沉默
(并且应该尽可能多地使用它)
我打字的任何东西都可以用来对付我
在法庭上白痴
我有权利出错
(可能是)
如果我不能提出自己的错误
我相信有人会为我提供。
[edited to try and eliminate word wrapping in code]
I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item
{
public:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink= false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false );
//Below are the set and get functions for
//all variables in this class
void set_weight( float weight )
{ item_weight = weight; }
float get_weight()
{ return item_weight; }
void set_can_equip( bool set_can_equip )
{ can_equip = set_can_equip; }
bool get_can_equip()
{ return can_equip; }
void set_can_attack( bool set_can_attack )
{ can_attack = set_can_attack; }
bool get_can_attack() { return can_attack; }
void set_can_defend( bool set_can_defend )
{ can_defend = set_can_defend; }
bool get_can_defend() { return can_defend; }
void set_can_drink( bool set_can_drink )
{ can_drink = set_can_drink; }
bool get_can_drink() { return can_drink; }
void set_can_eat( bool set_can_eat )
{ can_eat = set_can_eat; }
bool get_can_eat() { return can_eat; }
void set_can_use( bool set_can_use )
{ can_use = set_can_use; }
bool get_can_use() { return can_use; }
void set_is_consumable( bool set_is_consumable )
{ is_consumable = set_is_consumable; }
bool get_is_consumable() { return is_consumable; }
void set_is_magical( bool set_is_magical )
{ is_magical = set_is_magical; }
bool get_is_magical() { return is_magical; }
void describe(); // simply returns description (see below)
void clear_describe(); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.clear()
then push_back( line_to_add ), else just
push_back( line_to_add)*/
private:
// These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this
//after it has been cleared
vector<stringdescription;
// This is to allow multi-line descriptions
float item_weight; // These are pretty self explanatory I think
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};
#endif
but as I read along I was confused over something. The above works just
fine, but I was also wondering should the constructor be this:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false );
with a constructor body like this in the cpp file:
Item::Item( float weight,\
bool set_can_equip,\
bool set_can_attack,\
bool set_can_defend,\
bool set_can_drink,\
bool set_can_eat,\
bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}
or this:
Item() : float weight( 0 ),\
bool can_equip( false ),\
bool can_attack( false ),\
bool can_defend( false ),\
bool can_drink( false ),\
bool can_eat( false ),\
bool can_use( false ),\
bool is_consumable( false ),\
bool is_magical( false );
and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.
Question 1: Is this a mal-formed class header?
Question 2: Which should I use for the initialization list?
Thanks in advance.
DN
--
[there are no x''s in my email]
I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I''m sure someone will provide it for me.
推荐答案
Devon Null写道:
Devon Null wrote:
我只是想知道这是否是一个格式错误的类标题:
< br $>
#ifndef _items_h_
#define _items_h_
#include< iostream>
#include< ; string>
#include< vector>
using namespace std;
[edited to try and eliminate word wrapping in code]
I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
Never ,将此行放在标题中。
Never, ever put this line in a header.
class Item
{
public:
项目(浮动权重= 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false ,\
bool set_is_consumable = false,\
bool set_is_magical = false);
class Item
{
public:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink= false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false );
为什么那里有所有延续字符?
-
Ian Collins。
Why are all those continuation characters there?
--
Ian Collins.
" Devon Null" < th ************* @ xgmailx.com写在留言中
新闻:u6 ***************** *************@comcast.com。 ..
"Devon Null" <th*************@xgmailx.comwrote in message
news:u6******************************@comcast.com. ..
我只是想知道这是不是形成的标题:
#ifndef _items_h_
#define _items_h_
#include< iostream>
#include< string>
#include< vector>
using namespace std;
class物品
{
公共:
物品(浮球重量= 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false ,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false);
//以下是设置和获取函数
//此类中的所有变量
void set_weight(浮动权重)
{item_weight =重量; } $ / $
float get_weight()
{return item_weight; }
void set_can_equip(bool set_can_equip)
{can_equip = set_can_equip; }
bool get_can_equip()
{return can_equip; }
void set_can_attack(bool set_can_attack)
{can_attack = set_can_attack; }
bool get_can_attack(){return can_attack; }
void set_can_defend(bool set_can_defend)
{can_defend = set_can_defend; }
bool get_can_defend(){return can_defend; }
void set_can_drink(bool set_can_drink)
{can_drink = set_can_drink; }
bool get_can_drink(){return can_drink; }
void set_can_eat(bool set_can_eat)
{can_eat = set_can_eat; }
bool get_can_eat(){return can_eat; }
void set_can_use(bool set_can_use)
{can_use = set_can_use; }
bool get_can_use(){return can_use; }
void set_is_consumable(bool set_is_consumable)
{is_consumable = set_is_consumable; }
bool get_is_consumable(){return is_consumable; }
void set_is_magical(bool set_is_magical)
{is_magical = set_is_magical; }
bool get_is_magical(){return is_magical; }
void describe(); //简单地返回描述(见下文)
void clear_describe(); //清除描述
void add_describe(string line_to_add);
/ *上面的行检查描述是否设置为
need_set或desc_cleared,如果是这样description.clear()
然后push_back(line_to_add),否则只需
push_back(line_to_add)* /
private:
//这些是为每个创建的项目设置的统计数据
string need_set; //用这个初始化描述
string desc_cleared; //描述将被设置为此
//清除后
vector< stringdescription;
//这是允许多个线描述
float item_weight; //这些都是非常自我解释我认为
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};
#endif
但是当我读到的时候,我对某些事感到困惑。上面的工作只是
罚款,但我也想知道构造函数应该是这样的:
项目(浮动权重= 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\\
bool set_is_magical = false);
在cpp文件中有这样的构造函数体:
Item :: Item(float weight,\
bool set_can_equip,\
bool set_can_attack,\
bool set_can_defend,\
bool set_can_drink,\
bool set_can_eat,\
bool set_can_use)
{
item_weight =重量;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set ="你需要在这里设置一个描述!\ n" ;;
desc_cleared ="描述已被清除。\ n" ;;
description.clear();
description.push_back(need_set);
}
或者这个:
Item():浮动权重(0),\
bool can_equip(false),\
bool can_attack(false),\
bool can_defend(false),\
bool can_drink(false),\
bool can_eat(false),\
bool can_use(false),\
bool is_consumable(fal se),\
bool is_magical(false);
并丢失cpp文件中构造函数的整个主体?当我实例化
对象时,我将需要
来传递参数以覆盖默认值。
问题1:这是形成错误的类标题?
问题2:我应该将哪个用于初始化列表?
[edited to try and eliminate word wrapping in code]
I was simply wondering if this was a mal-formed class header:
#ifndef _items_h_
#define _items_h_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item
{
public:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink= false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false );
//Below are the set and get functions for
//all variables in this class
void set_weight( float weight )
{ item_weight = weight; }
float get_weight()
{ return item_weight; }
void set_can_equip( bool set_can_equip )
{ can_equip = set_can_equip; }
bool get_can_equip()
{ return can_equip; }
void set_can_attack( bool set_can_attack )
{ can_attack = set_can_attack; }
bool get_can_attack() { return can_attack; }
void set_can_defend( bool set_can_defend )
{ can_defend = set_can_defend; }
bool get_can_defend() { return can_defend; }
void set_can_drink( bool set_can_drink )
{ can_drink = set_can_drink; }
bool get_can_drink() { return can_drink; }
void set_can_eat( bool set_can_eat )
{ can_eat = set_can_eat; }
bool get_can_eat() { return can_eat; }
void set_can_use( bool set_can_use )
{ can_use = set_can_use; }
bool get_can_use() { return can_use; }
void set_is_consumable( bool set_is_consumable )
{ is_consumable = set_is_consumable; }
bool get_is_consumable() { return is_consumable; }
void set_is_magical( bool set_is_magical )
{ is_magical = set_is_magical; }
bool get_is_magical() { return is_magical; }
void describe(); // simply returns description (see below)
void clear_describe(); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.clear()
then push_back( line_to_add ), else just
push_back( line_to_add)*/
private:
// These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this
//after it has been cleared
vector<stringdescription;
// This is to allow multi-line descriptions
float item_weight; // These are pretty self explanatory I think
bool can_equip;
bool can_attack;
bool can_defend;
bool can_drink;
bool can_eat;
bool can_use;
bool is_consumable;
bool is_magical;
};
#endif
but as I read along I was confused over something. The above works just
fine, but I was also wondering should the constructor be this:
Item( float weight = 0,\
bool set_can_equip = false,\
bool set_can_attack = false,\
bool set_can_defend = false,\
bool set_can_drink = false,\
bool set_can_eat = false,\
bool set_can_use = false,\
bool set_is_consumable = false,\
bool set_is_magical = false );
with a constructor body like this in the cpp file:
Item::Item( float weight,\
bool set_can_equip,\
bool set_can_attack,\
bool set_can_defend,\
bool set_can_drink,\
bool set_can_eat,\
bool set_can_use )
{
item_weight = weight;
can_equip = set_can_equip;
can_attack = set_can_attack;
can_defend = set_can_defend;
can_drink = set_can_drink;
can_eat = set_can_eat;
can_use = set_can_use;
is_consumable = set_is_consumable;
is_magical = set_is_magical;
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}
or this:
Item() : float weight( 0 ),\
bool can_equip( false ),\
bool can_attack( false ),\
bool can_defend( false ),\
bool can_drink( false ),\
bool can_eat( false ),\
bool can_use( false ),\
bool is_consumable( false ),\
bool is_magical( false );
and lose the entire body of the constructor in the cpp file? I will need
to pass in parameters to override the defaults when I instantiate the
objects.
Question 1: Is this a mal-formed class header?
Question 2: Which should I use for the initialization list?
我可能会这样做(通知也重命名变量
和方法):
#ifndef ITEMS_HEADER_
#define ITEMS_HEADER_
#include< iostream>
#include< string>
#include< vector>
使用命名空间std;
class Item
{
public:
Item(浮动权重= 0,
bool set_can_equip = false,
bool set_can_attack = false,
bool set_can_defend = false,
bool set_can_drink = false,
bool set_can_eat = false,
bool set_can_use = false,
bool set_is_consumable = false,
bool set_is_magical = false);
//以下是set和get函数对于
//此类中的所有变量
空白重量(浮动重量)
{weight_ =重量; }
浮重()
{return weight_; }
void equipable(bool Can_equip)
{equipable_ = Can_equip; }
bool equipable()
{return equipable_; }
无法攻击(bool Can_attack)
{attackable_ = Can_attack; }
bool attackable()
{return attackable_; }
void defendable(bool Can_defend)
{defendable_ = Can_defend; }
bool defendable()
{return defendable_; }
无法饮用(bool Can_drink)
{drinkable_ = Can_drink; }
bool drinkable()
{return drinkable_; }
无法食用(bool Can_eat)
{eatable_ = Can_eat; }
bool eatable(){return eatable_; }
无效可用(bool Can_use)
{useable_ = Can_use; }
bool useable()
{return useable_; }
无耗材(bool Is_consumable)
{consumable_ = Is_consumable; }
bool consumable()
{return consumable_; }
虚空神奇(bool Is_magical)
{magical_ = Is_magical; }
bool magical()
{return magical_; }
void describe(); //简单地返回描述(见下文)
void clear_describe(); //清除描述
void add_describe(string line_to_add);
/ *上面的行检查描述是否设置为
need_set或desc_cleared,如果是这样description.clear()
然后push_back(line_to_add),否则只需
push_back(line_to_add)* /
private:
//这些是为每个创建的项目设置的统计数据
string need_set; //用这个初始化描述
string desc_cleared; //描述将被设置为此
//清除后
vector< stringdescription;
//这是允许多个线描述
float weight_; //这些都是非常自我解释我认为
bool equipable_;
bool attackable_;
bool defendable _;
bool drinkable_;
bool eatable_;
bool useable_;
bool consumable_;
bool magical _;
};
#endif
Item :: Item(浮动重量,
bool可装,
布尔可攻击,
bool可辩护,
bool可饮用,
bool eatable,
bool可用,
bool耗材,
attackable_(可攻击),defendable_(可辩护),
drinkable_(可饮用),eatable_(可食用),
可用_(可用),消耗品_(消耗品),
magical_(神奇)
{
need_set ="你需要在这里设置描述!\ n" ;;
desc_cleared ="描述已被清除。\ n;
description.clear();
description.push_back(need_set);
}
I would probably do it similar to this (notice also renaming of variables
and methods):
#ifndef ITEMS_HEADER_
#define ITEMS_HEADER_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Item
{
public:
Item( float weight = 0,
bool set_can_equip = false,
bool set_can_attack = false,
bool set_can_defend = false,
bool set_can_drink= false,
bool set_can_eat = false,
bool set_can_use = false,
bool set_is_consumable = false,
bool set_is_magical = false );
//Below are the set and get functions for
//all variables in this class
void weight( float Weight )
{ weight_ = Weight; }
float weight()
{ return weight_; }
void equipable( bool Can_equip )
{ equipable_ = Can_equip; }
bool equipable()
{ return equipable_; }
void attackable( bool Can_attack )
{ attackable_ = Can_attack; }
bool attackable()
{ return attackable_; }
void defendable( bool Can_defend )
{ defendable_ = Can_defend; }
bool defendable()
{ return defendable_; }
void drinkable( bool Can_drink )
{ drinkable_ = Can_drink; }
bool drinkable()
{ return drinkable_; }
void eatable( bool Can_eat )
{ eatable_ = Can_eat; }
bool eatable() { return eatable_; }
void useable( bool Can_use )
{ useable_ = Can_use; }
bool useable()
{ return useable_; }
void consumable( bool Is_consumable )
{ consumable_ = Is_consumable; }
bool consumable()
{ return consumable_; }
void magical( bool Is_magical )
{ magical_ = Is_magical; }
bool magical()
{ return magical_; }
void describe(); // simply returns description (see below)
void clear_describe(); // clears description
void add_describe( string line_to_add );
/*Line above checks to see if description is set to either
need_set or desc_cleared, and if so description.clear()
then push_back( line_to_add ), else just
push_back( line_to_add)*/
private:
// These are stats that will be set for every item created
string need_set; // description is initialized with this
string desc_cleared; // description will be set to this
//after it has been cleared
vector<stringdescription;
// This is to allow multi-line descriptions
float weight_; // These are pretty self explanatory I think
bool equipable_;
bool attackable_;
bool defendable_;
bool drinkable_;
bool eatable_;
bool useable_;
bool consumable_;
bool magical_;
};
#endif
Item::Item( float weight,
bool equipable,
bool attackable,
bool defendable,
bool drinkable,
bool eatable,
bool useable,
bool consumable,
bool magical ): weight_( weight ), equipable_( equipable ),
attackable_( attackable ), defendable_( defendable ),
drinkable_( drinkable ), eatable_( eatable ),
useable_( useable ), consumable_( consumable ),
magical_( magical )
{
need_set = "You need to set a description here!\n";
desc_cleared = "The description has been cleared.\n";
description.clear();
description.push_back( need_set );
}
Jim Langston写道:
Jim Langston wrote:
>
我可能会这样做(注意还要重命名变量
和方法):
#ifndef ITEMS_HEADER_
#define ITEMS_HEADER_
#包括< iostream>
#include< string>
#include< vector>
using namespace std;
>
I would probably do it similar to this (notice also renaming of variables
and methods):
#ifndef ITEMS_HEADER_
#define ITEMS_HEADER_
#include <iostream>
#include <string>
#include <vector>
using namespace std;
你确定你会这样做吗?
-
Ian Collins。
Are you sure you''d do this?
--
Ian Collins.
这篇关于在这里寻求集体智慧的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!