kyrpav
kyrpav

Reputation: 778

array initialization unknowledge

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.

Upvotes: 0

Views: 193

Answers (3)

Yuchen
Yuchen

Reputation: 33146

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.

Upvotes: 0

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};

Upvotes: 1

Sodved
Sodved

Reputation: 8607

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;
}

Upvotes: 1

Related Questions