Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++_Linux_Windows_Compiler Errors - Fatal编程技术网

C++ 其中C++;编译器我可以给结构变量赋值吗?

C++ 其中C++;编译器我可以给结构变量赋值吗?,c++,linux,windows,compiler-errors,C++,Linux,Windows,Compiler Errors,我不确定该如何命名这个问题。这可能是一个NoOB问题,但是无论如何……我现在不明白为什么C++中的赋值在结构>代码中,在Linux(Ubuntu)中工作,但是在Windows中没有(我在Windows中编译一个错误程序)。BR>它依赖于编译器,是吗?或者还有什么我应该知道的 #包括 #包括 const char data[]=“data.txt”; const char result[]=“data_result.txt”; 常数Cn=10; 结构玩家{ /*在结构中赋值在windows中不起作

我不确定该如何命名这个问题。这可能是一个NoOB问题,但是无论如何……我现在不明白为什么C++中的赋值在<代码>结构>代码中,在Linux(Ubuntu)中工作,但是在Windows中没有(我在Windows中编译一个错误程序)。BR>它依赖于编译器,是吗?或者还有什么我应该知道的

#包括
#包括
const char data[]=“data.txt”;
const char result[]=“data_result.txt”;
常数Cn=10;
结构玩家{
/*在结构中赋值在windows中不起作用*/
健康指数=100;
智力法力=120;
int经验=0;
};
结构玩家[Cn];
int main(){
返回0;
}

将C++中的结构或类赋值为默认值,这只是C++ 11中的一个特性。因此,很可能说Windows编译器要么不支持C++ 11,要么没有接收到用C++ 11编译的正确编译参数。

< p>你的问题是“C++编译器可以将值分配给结构变量”。 我的回答是“从所有编译C++编译器,符合C++ 98前进”。所有属性都允许初始化结构数据属性

即使在我设置编译器选项std=c++98时,下面的代码仍然有效

它所做的是避免使用C++11特性,并将数据属性初始化移到用户定义的默认ctor中

struct Players
{
   int Health;
   int Mana;
   int Experience;

   Players() : Health(100), Mana(120), Experience(0)
      {
      }   

   // an array of Cn Players will build Cn copies, 
   // all initialized to these same 3 values of the default ctor.

};
是的,您的代码结构确实有编译器提供的ctor(和一些其他方法属性)

是的,我想你不是真的想用c++98


更新

下面是另一个默认的ctor,尽管看起来可能不是这样

struct Players
{
   int Health;
   int Mana;
   int Experience;

   Players(int aHealth=100, int aMana=120, int anExperiance=0) : 
           Health(aHealth), Mana(aMana), Experience(anExperience)
      {
      }   

};
这是默认的ctor,因为您可以将其作为“Players();”调用,并且所有的方法默认值都将应用


您可以使用“=delete.”语句控制编译器为您的结构/类提供的内容

可以命令编译器不提供的方法

  //  coding standard: disallow when not used
  T(void)                  = delete; // default ctor    (1)
  ~T(void)                 = delete; // default dtor    (2)
  T(const T&)              = delete; // copy ctor       (3)
  T& operator= (const T&)  = delete; // copy assignment (4)
  T(const T&&)             = delete; // move ctor       (5)
  T& operator= (const T&&) = delete; // move assignment (6)
只需替换你的类名(即玩家)


请记住,如果不想使用这些方法之一,则不允许使用。如果您提供了编译器提供方法的替代方法,请不要拒绝。

这取决于您的编译器是否支持C++11。请发布代码,而不是代码的图像。Visual Studio 2015 Community Edition是免费的,并支持此类初始化。您需要在问题中包含哪些编译器,这可能是重复的(包括版本号)以及您传递给他们的论点。这将使您的问题更容易回答。