Non-default constructor allowed in struct?

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::eek: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.
 
J

John Harrison

Marcus said:
Is it in the standard that one cannot declare a member of a struct that
uses a non-default constructor?

No, but you are doing in the wrong way.
struct Rcvr_values
{
Matrix signal(NUM_SEASONS, NUM_HALF_HOURS); // error is this line
};

I think you know the answer really

struct Rcvr_values
{
Rcvr_values() : signal(NUM_SEASONS, NUM_HALF_HOURS) {}
Matrix signal;
};

john
 
A

Andrey Tarasevich

Marcus said:
Is it in the standard that one cannot declare a member of a struct that
uses a non-default constructor?
...
struct Rcvr_values
{
Matrix signal(NUM_SEASONS, NUM_HALF_HOURS); // error is this line
};
...

You can't "use" constructors (i.e. specify initializers) in the actual
declaration of the data member. The above syntax simply doesn't make any sense
in C++.

If you want to perform custom initialization of data member, you have to do it
in the initializer list of the enclosing struct's constructor:

struct Rcvr_values
{
Matrix signal;

Rcvr_values() : signal(NUM_SEASONS, NUM_HALF_HOURS)
{}
};
 
M

Marcus Kwok

Andrey Tarasevich said:
You can't "use" constructors (i.e. specify initializers) in the actual
declaration of the data member. The above syntax simply doesn't make any sense
in C++.

If you want to perform custom initialization of data member, you have to do it
in the initializer list of the enclosing struct's constructor:

struct Rcvr_values
{
Matrix signal;

Rcvr_values() : signal(NUM_SEASONS, NUM_HALF_HOURS)
{}
};

Thank you John and Andrey. What I believe is happening is that the line:

Matrix signal(...);

is interpreted by the compiler as a function declaration for a function
named "signal" that returns a Matrix, so it expected a typename
immediately after the open parenthesis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top