0

I know the basic way to initialize arrays.I get an error on my compiler about an int array i what to initialize on a constructor that i do not understand it.I need some help. my code is:

Cpp file:

#include <iostream>
using namespace std;
#include "ValidationController.h"

ValidationController::ValidationController() {
    // TODO Auto-generated constructor stub
    monthTable[12]={0,3,3,6,1,4,6,2,5,0,3,5};
}

ValidationController::~ValidationController() {
    // TODO Auto-generated destructor stub
}

and the header file:

#ifndef VALIDATIONCONTROLLER_H_
#define VALIDATIONCONTROLLER_H_

class ValidationController {
public:
    int monthTable[];//={0,3,3,6,1,4,6,2,5,0,3,5};
    ValidationController();
    virtual ~ValidationController();
};

#endif /* VALIDATIONCONTROLLER_H_ */

the error i get is:

..\src\ValidationController.cpp:13: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

and

..\src\ValidationController.cpp:13: error: cannot convert '' to 'int' in assignment

I do not want to make it static. Is the there any solution that keeps the declaration to the header file?Or should i just declare it and initialize it in the .cpp file at once after imports.

kyrpav
  • 756
  • 1
  • 13
  • 43
  • Initialize it in the constructor initializer list. – chris Sep 24 '13 at 16:17
  • If the content of the month table does not change, then consider making it `static const` which allows for list initialization even in pre C++11. – Nobody moving away from SE Sep 24 '13 at 16:19
  • Use std::vector instead. Also, the compiler already offered a solution in its error message: add `-std=gnu++11` to your compiler flags and read about C++11 initializer lists. – Nikos C. Sep 24 '13 at 16:21
  • 1
    @NikosC.: If c++11 is an option then why bother with `std::vector` when an `std::array` will do? – Nobody moving away from SE Sep 24 '13 at 16:26
  • Soory jefffrey i do not uinderstand you according to http://www.cplusplus.com/doc/tutorial/arrays/ i do not understand the difference in C style and C++ style of arrays can you give me a hints of a reference? – kyrpav Sep 24 '13 at 16:32
  • @kyrpav, By C++, he means `std::array`. The tutorial doesn't even mention it, but it's far superior. For example, it actually knows its size. – chris Sep 24 '13 at 16:34
  • i hhave added the -std=gnu++11 to the compiler flags and the error now is the second one:.\src\ValidationController.cpp:13: error: cannot convert '' to 'int' in assignment – kyrpav Sep 24 '13 at 16:37
  • @kyrpav: That is because in your code you assign to the 13th element of your array. – Nobody moving away from SE Sep 24 '13 at 16:38
  • sorry for not understanding.Nobody what do you mean.The initialization is at the 13 line of the cpp – kyrpav Sep 24 '13 at 16:52
  • 1
    I understand that you all are angry about not professional questions but since i am not a professional programmer but i really want to learn in order to keep my jobs it is important to me when i do not find a solution to ask so i would like to ask you to remove the negative points cause i did tried and searched i gave you also a reference.Thank you anyway – kyrpav Sep 24 '13 at 16:58
  • @kyrpav: Noone is angry at you. Those are all valuable hints. I did not mean line 13, but `monthTable[12]={0,3,3,6,1,4,6,2,5,0,3,5};` which means "assign {0,3,...} to `monthTable[12]` which is the 13th element of the array `monthTable` and not "initialize array monthTable of size 12 with {0,3,...}". – Nobody moving away from SE Sep 24 '13 at 17:02

3 Answers3

1

As suggested in comments, you need to make this static and initialise it in your .cpp

class ValidationController {
public:
    static int monthTable[];
    ValidationController();
    virtual ~ValidationController();
};

int ValidationController::monthTable[]={0,3,3,6,1,4,6,2,5,0,3,5};

ValidationController::ValidationController()
{
    // TODO Auto-generated constructor stub
}

ValidationController::~ValidationController() {
    // TODO Auto-generated destructor stub
}

int main()
{
    ValidationController v();
    return 0;
}
Sodved
  • 8,428
  • 2
  • 31
  • 43
  • thank you for this solution i know how to do this as i mentioned at the question i just want to initialize it as a variable of an instance – kyrpav Sep 24 '13 at 17:01
1

If you can use C++11 then write: In the header file: #include

class ValidationController {
public:
    std::array<int, 12> monthTable;
    ValidationController();
    virtual ~ValidationController();
};

In the source file:

ValidationController::ValidationController() 
: monthTable{0,3,3,6,1,4,6,2,5,0,3,5}
{}

or if you do not need to change the contents:

In the header file:

#include <array>

class ValidationController {
public:
    const static std::array<int, 12> monthTable;
    ValidationController();
    virtual ~ValidationController();
};

In the source file:

const std::array<int, 12> ValidationController::monthTable = {0,3,3,6,1,4,6,2,5,0,3,5};
  • how can i check that i can use C++11 cause i thought i could but i get error that i can array name at std::array can not be resolved.Even after i include the – kyrpav Sep 24 '13 at 16:55
  • @kyrpav When you add the `-std=gnu++11` or `-std=c++11` to the compile command and the compiler does not complain about it, then it should support it. – Nobody moving away from SE Sep 24 '13 at 17:00
  • ok so what do i get error that symbol array can not be resolved? – kyrpav Sep 24 '13 at 17:02
  • What compiler are you using and what does your compile command look like? – Nobody moving away from SE Sep 24 '13 at 17:02
  • well i think you speak about: mingw and the flags are:-O0 -std=gnu++11 -g3 -Wall -c -fmessage-length=0 – kyrpav Sep 24 '13 at 17:05
  • I use eclipse kepler also and it is a c++ project with MinGW – kyrpav Sep 24 '13 at 17:08
  • If you use eclipse then make sure that the error you see is coming from the compiler and not from the indexer. Using C++11 features in Eclipse requires some additional settings to avoid spurious errors. Have a look here: http://stackoverflow.com/questions/17457069/enabling-c11-in-eclipse-kepler-cdt – Nobody moving away from SE Sep 24 '13 at 17:10
  • i did what it says (the part I was already done before) but the error persist – kyrpav Sep 24 '13 at 17:15
  • So this means that the error is from the MinGW indexer and how do i solve it? – kyrpav Sep 24 '13 at 17:20
  • As I can't see what is happening at your site I don't know what is wrong exactly. I am pretty sure that you are looking at the "Problems" tab and see some messages there, but they can be from the compiler and from the indexer. To shut off the errors from the indexer you could go to Window -> Preferences -> C/C++ -> Code Analysis and there uncheck "Syntax and semantic errors". – Nobody moving away from SE Sep 24 '13 at 17:26
  • it is from the indexer.If i uncheck it the error goes away.The error is that it can not resolve array from std::array. This is the only error also in order to add the last prefix "-std=c++0x" to the compiler specs i had to uncheck the "use global provider shared between project" box and then the command is : "${COMMAND} -E -P -v -dD "${INPUTS}" -std=c++0x" i tried to put it and before inputs but nothing changed.Also i have a cdt built in compiler setting and one that says MinGW at the end.I did it to all of them.But the error still persist – kyrpav Sep 24 '13 at 17:34
  • As long as your code compiles the indexer problem is a mere annoyance. Sometimes you need to restart Eclipse to let the changes take effect and in any circumstances you must rebuild the index after you have changed these settings to get the updated headers. – Nobody moving away from SE Sep 24 '13 at 17:54
  • since i am not a professional programmer it is important not to have any errors that i can not be sure that they affect the programm. I need to be calm in other way.I did restart eclipse and tried to rebuilt the project and the header has the same problem.Please any help would be great.What could i do if this happend to std::cout or cin? – kyrpav Sep 24 '13 at 18:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37963/discussion-between-nobody-and-kyrpav) – Nobody moving away from SE Sep 24 '13 at 18:08
0

Both @Sodved's solution (using static) and @Nobody's solution (using Array) look very good to me. But if you insist that you don't want to make your array static or use any additional data structure, you can try this.

class ValidationController {
    public:
    int monthTable[12];
    ValidationController();
    virtual ~ValidationController();
    };


ValidationController::ValidationController()
{
    // TODO Auto-generated constructor stub
    static const int temp[12] = {0,3,3,6,1,4,6,2,5,0,3,5};
    memcpy( monthTable, temp, 12*sizeof(int) );
}

ValidationController::~ValidationController() {
    // TODO Auto-generated destructor stub
}

int main()
{
    ValidationController v;
    return 0;
}

Basically, you still keep one copy of the data in the constructor. But you will also have a copy of the data for any instance of the class ValidationController.

Yuchen
  • 30,852
  • 26
  • 164
  • 234