The following 3 code blocks are the main.cpp, static_class_array.cpp, and static_class_array.h respectively. I'm getting the following error:
static_class_array.cpp||In constructor 'static_array_class::static_array_class()':|
stati开发者_如何学JAVAc_class_array.cpp|5|error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment|
||=== Build finished: 1 errors, 0 warnings ===|
#include "static_class_array.h"
int main()
{
static_array_class* array_class;
array_class = new static_array_class();
delete array_class;
return 0;
}
#include "static_class_array.h"
static_array_class::static_array_class()
{
static_array_class::array[3] = {0,1,2};
}
static_array_class::~static_array_class(){}
#ifndef STATIC_CLASS_ARRAY_H
#define STATIC_CLASS_ARRAY_H
class static_array_class
{
private:
static int array[3];
public:
static_array_class();
~static_array_class();
};
#endif
I think that what you want in the implementation file is:
static_array_class::static_array_class()
{
}
static_array_class::~static_array_class(){}
int static_array_class::array[3] = {0,1,2};
Explanation of error message
"cannot convert 'brace-enclosed initializer list' to 'int' in assignment"
in submitted code.
This is because the code:
static_array_class::array[3] = {0,1,2};
is interpreted as meaning that {0,1,2}
should be assigned to element 3
in the array. Element 3
is of type int
, (and incidentally not allocated being the fourth element), so this is like:
int i = 0;
i = {0,1,2};
Hence the error message.
They are not the same type;
Your class is a class which includes a an array -- they other is just an array.
With a static definition of a class member you need to declare the actual instance outside the class, just like with any other static,
int static_array_class::array[3] = {0,1,2}; // note this line is outside the constructor
static_array_class::static_array_class()
{
}
static_array_class::~static_array_class(){}
精彩评论