M
Marcus Kwok
Is it in the standard that one cannot declare a member of a struct that
uses a non-default constructor? Here is my simplified code; the Matrix
class was taken from the FAQ, and works well otherwise (error checking
was removed for simplicity).
#include <iostream>
#include <vector>
const int NUM_SEASONS = 4;
const int NUM_HALF_HOURS = 48;
//////////////////////////////////////// Matrix class taken from FAQ:
class Matrix
{
public:
Matrix(unsigned rows, unsigned cols);
~Matrix();
Matrix(const Matrix& m);
Matrix& operator=(const Matrix& m);
private:
unsigned rows_, cols_;
double* data_;
};
inline Matrix::Matrix(unsigned rows, unsigned cols)
: rows_(rows)
, cols_(cols)
{
data_ = new double[rows * cols];
}
inline Matrix::~Matrix()
{
delete[] data_;
}
inline Matrix::Matrix(const Matrix& m) { }
inline Matrix& Matrix:perator=(const Matrix& m) { return *this; }
//////////////////////////////////////// End of Matrix class from FAQ
struct Rcvr_values
{
Matrix signal(NUM_SEASONS, NUM_HALF_HOURS); // error is this line
};
struct Rcvr
{
std::vector<Rcvr_values> values;
};
int main()
{
Rcvr a;
return 0;
}
Using VC++ 7.1, I get the following compiler error:
test.cpp(37) : error C2061: syntax error : identifier 'NUM_SEASONS'
However, if I change the constructor of Matrix to:
inline Matrix::Matrix(unsigned rows = NUM_SEASONS,
unsigned cols = NUM_HALF_HOURS)
and I change Rcvr_values to:
struct Rcvr_values
{
Matrix signal;
};
then I get no compiler error.
uses a non-default constructor? Here is my simplified code; the Matrix
class was taken from the FAQ, and works well otherwise (error checking
was removed for simplicity).
#include <iostream>
#include <vector>
const int NUM_SEASONS = 4;
const int NUM_HALF_HOURS = 48;
//////////////////////////////////////// Matrix class taken from FAQ:
class Matrix
{
public:
Matrix(unsigned rows, unsigned cols);
~Matrix();
Matrix(const Matrix& m);
Matrix& operator=(const Matrix& m);
private:
unsigned rows_, cols_;
double* data_;
};
inline Matrix::Matrix(unsigned rows, unsigned cols)
: rows_(rows)
, cols_(cols)
{
data_ = new double[rows * cols];
}
inline Matrix::~Matrix()
{
delete[] data_;
}
inline Matrix::Matrix(const Matrix& m) { }
inline Matrix& Matrix:perator=(const Matrix& m) { return *this; }
//////////////////////////////////////// End of Matrix class from FAQ
struct Rcvr_values
{
Matrix signal(NUM_SEASONS, NUM_HALF_HOURS); // error is this line
};
struct Rcvr
{
std::vector<Rcvr_values> values;
};
int main()
{
Rcvr a;
return 0;
}
Using VC++ 7.1, I get the following compiler error:
test.cpp(37) : error C2061: syntax error : identifier 'NUM_SEASONS'
However, if I change the constructor of Matrix to:
inline Matrix::Matrix(unsigned rows = NUM_SEASONS,
unsigned cols = NUM_HALF_HOURS)
and I change Rcvr_values to:
struct Rcvr_values
{
Matrix signal;
};
then I get no compiler error.