By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Non-default constructor allowed in struct?

P: n/a
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::operator=(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.

--
Marcus Kwok
Nov 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Marcus Kwok wrote:
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
Nov 10 '05 #2

P: n/a
Marcus Kwok wrote:
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)
{}
};

--
Best regards,
Andrey Tarasevich
Nov 10 '05 #3

P: n/a
Andrey Tarasevich <an**************@hotmail.com> wrote:
Marcus Kwok wrote:
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)
{}
};


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.

--
Marcus Kwok
Nov 10 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.