-2

I was unsure of how should I name this question. It's probably a noob question but anyway...
I don't really now why assigning values in struct in C++ does work in Linux(Ubuntu) but doesn't in Windows(I get an error compiling same program in windows)
It depends on compiler, yes? Or there is something else I should know?

#include <iostream>
#include <fstream>

const char data[] = "data.txt";
const char result[] = "data_result.txt";
const int Cn = 10;

struct Players{

    /*Assigning values in struct does not work in windows */
    int Health = 100;
    int Mana = 120;
    int Experience = 0;

};

struct Players Player[Cn];

int main(){


    return 0;
}
DeathTails
  • 468
  • 4
  • 12
Karolis
  • 137
  • 4
  • 12
  • 4
    It depends on your compiler if it supports C++11. – knivil Mar 18 '16 at 17:02
  • 3
    Please post the code instead of an image of the code. – R Sahu Mar 18 '16 at 17:02
  • 2
    Visual Studio 2015 Community Edition is free and supports such initializations. – Pixelchemist Mar 18 '16 at 17:21
  • 1
    Possible duplicate of [Windows C++ compiler with full C++11 support (should work with Qt)](http://stackoverflow.com/questions/19425482/windows-c-compiler-with-full-c11-support-should-work-with-qt) – sashoalm Mar 18 '16 at 17:43
  • 1
    You need to include in the question what compilers (including the version number) you're using and the arguments you're passing to them. That would make your question much easier to answer. – Rob K Mar 18 '16 at 18:06

2 Answers2

4

Assigning "default" values to structs or classes in C++ is a feature that only became a thing in C++11. So it's probably fair to say that your Windows compiler either doesn't support C++11, or else isn't receiving the correct compile arguments to compile with C++11.

Xirema
  • 19,889
  • 4
  • 32
  • 68
2

Your question is "In which C++ compilers can I assign values to struct variables".

My answer is "from all compilant c++ compilers, conforming to c++98 forward". All allow struct data attributes to be initialized.

The following works even when I set my compiler option std=c++98

What it does is avoid the C++11 feature and moves the data attribute initialization into a user defined default 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.

};

Yes, your code struct does have a compiler provided ctor (and a few other method attributes.)

And yes, I suppose you don't really want to use c++98.


update

The following is yet another default ctor, though it may not look like it.

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

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

};

This is default ctor because you can invoke it as "Players players();", and all the method defaults will apply.


You may control what the compiler provides for your struct/class by using the "= delete." statement.

The methods you can order the compiler to not provide

  //  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)

Just replace T your class name (i.e. Players)

Remember, you disallow when you want to not have one of these methods. Do not disallow if you provide an alternative to the compiler provide method.

2785528
  • 5,438
  • 2
  • 18
  • 20