1

How can I initialize struct array with parameters in constructor? Now I have this code:

    struct roundMatrix {
        private:
            int **matrix;

        public:
            roundMatrix(int teamsNumber) {
                matrix = new int*[teamsNumber];
                for(int i=0;i<teamsNumber;i++) {
                    matrix[i] = new int[teamsNumber];
                }
            }

            int addPartners(int first, int second) {
                if(matrix[first][second] == -1 && matrix[second][first] == -1) {
                    matrix[first][second] = 0;
                    matrix[second][first] = 0;
            }
            else {
                return -1;
            }
            return 0;
        }
     };

    ...

Then I need to inintialize array of roundMatrix with parameter:

    roundMatrix rounds[roundsNumber](teamsNumber);

And I got an error:

variable-sized object 'rounds' may not be initialized

One more question. How can I initialize vector with struct and constructor paramaters?

Long Smith
  • 1,339
  • 3
  • 21
  • 40

2 Answers2

3

You cannot initialize array in that way. It should be written as:

roundMatrix rounds[roundsNumber] = {teamsNumber, teamsNuber, ...);

Alternatively you need to implement a default constructor for roundMatrix class that will automatically initialize the items in your array.

vahancho
  • 20,808
  • 3
  • 47
  • 55
2

At first your struct is class. struct should be used in C++ without methods, inheritance, encapsulation and other class's stuff as same as in standart C code.

Next, class names should be in upper camel case: first character of the name should be in uppercase and each new word in the name should begin from uppercase character. By the way your corporate code conventions may override this default convention which uses almost everywhere in C++ code.

And last: in case you have an array of objects of this class you can't call constructor for each object of this class during initialization. You can do something like that:

roundMatrix *rounds = new roundMatrix[roundsNumber];

for(i = 0; i < roundsNumber; i++)
    rounds[i] = roundMatrix(teamsNumber);
VP.
  • 15,509
  • 17
  • 91
  • 161
  • i can understand inheritance and encapsulation, but why shouldn't you use a `struct` with, say, a constructor and a method or two? aren't structs and classes equivalent in c++, where class members are private by default, as opposed to structs, where they're public? – stellarossa Nov 14 '13 at 12:01
  • Classes and structs are equivalent in C++, except that classes have private access specifier by default. Both can have member functions and member variables. – vahancho Nov 14 '13 at 12:02
  • Yes, classes and structs are almost same things in C++, but better if class will mean `C++ class` and struct will mean `C struct` for better reading and understanding the code, is not it? – VP. Nov 14 '13 at 12:03
  • And how can I initialize vector with struct and constructor paramaters? – Long Smith Nov 14 '13 at 12:31
  • Look here: [How to initialize vector in C++](http://stackoverflow.com/questions/8906545/how-to-initialize-a-vector-in-c) – VP. Nov 14 '13 at 12:36
  • Doesn't this assign the address of unnamed temporaries on the stack into the rounds array? As far as I can see, your program will lead to undefined behaviour once the elements in rounds are attempted to be used. The first array assigned statement is valid, although it creates garbage that can never be cleaned up, once the pointer values have been overwritten in the for loop below, and even if you fixed that, you shouldn't advise raw pointer usage, as it will easily lead to exception-insafety. – TamaMcGlinn Aug 08 '16 at 18:12