table initialization

A

AdlerSam

Hi,

please consider the following example:

struct A {
A(int) {}
A() {}
};
struct B {
B(const A &a): _a(a) {}
B(int x, const A &a): _a(a) {}
void f() {}
A _a;
};
int main() {
B v[] = {
B(A(3)),
B(27, A()),
B(A())
};
v[0].f();
v[1].f();
v[2].f();

B x(A(3));
B y(27, A());
B z(A());
x.f();
y.f();
z.f();
}

The last line doesn't compile:

tst.cpp:26: error: request for member ‘f’ in ‘z’, which is of non-
class type ‘B ()(A (*)())’

Obviously, because the compiler thinks that z is a function.

While it is easy to work arround the problem with an intermediate
variable for class B, it annoys me because I need to instantiate a
rather big amount of objects, an I'd like to do it in a table-like
manner.

Thus, my question: Is there a way to tell the compiler that z is an
object while keeping the table-based initialization sceme?

Thanks,

Sam
 
S

Saeed Amrollahi

Hi,

please consider the following example:

struct A {
    A(int) {}
    A() {}};

struct B {
    B(const A &a): _a(a) {}
    B(int x, const A &a): _a(a) {}
    void f() {}
    A _a;};

int main() {
    B v[] = {
        B(A(3)),
        B(27, A()),
        B(A())
    };
    v[0].f();
    v[1].f();
    v[2].f();

    B x(A(3));
    B y(27, A());
    B z(A());
    x.f();
    y.f();
    z.f();

}

The last line doesn't compile:

tst.cpp:26: error: request for member ‘f’ in ‘z’, which is of non-
class type ‘B ()(A (*)())’

Obviously, because the compiler thinks that z is a function.

While it is easy to work arround the problem with an intermediate
variable for class B, it annoys me because I need to instantiate a
rather big amount of objects, an I'd like to do it in a table-like
manner.

Thus, my question: Is there a way to tell the compiler that z is an
object while keeping the table-based initialization sceme?

Thanks,

Sam

Hi

It's about "The Most Vexing Parse" problem.
C++ has parsing ambiguity between a function declaration and a
constructor invocation when the constructor arguments are temporaries.
This is known as the "most vexing parse" problem.
Put the temporary object inside (). I mean
B z((A()));
For further information, please see
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439
and
http://en.wikipedia.org/wiki/Most_vexing_parse

HTH,
-- Saeed Amrollahi
 
S

Saeed Amrollahi

Hi,

please consider the following example:

struct A {
    A(int) {}
    A() {}};

struct B {
    B(const A &a): _a(a) {}
    B(int x, const A &a): _a(a) {}
    void f() {}
    A _a;};

int main() {
    B v[] = {
        B(A(3)),
        B(27, A()),
        B(A())
    };
    v[0].f();
    v[1].f();
    v[2].f();

    B x(A(3));
    B y(27, A());
    B z(A());
    x.f();
    y.f();
    z.f();

}

The last line doesn't compile:

tst.cpp:26: error: request for member ‘f’ in ‘z’, which is of non-
class type ‘B ()(A (*)())’

Obviously, because the compiler thinks that z is a function.

While it is easy to work arround the problem with an intermediate
variable for class B, it annoys me because I need to instantiate a
rather big amount of objects, an I'd like to do it in a table-like
manner.

Thus, my question: Is there a way to tell the compiler that z is an
object while keeping the table-based initialization sceme?

Thanks,

Sam

Hi

It's about the Vexing phrase.
C++ has parsing ambiguity between a function declaration
and a constructor invocation when the constructor arguments
are temporaries. This is known as the "most vexing parse" problem.
Put the temporary object inside (). I mean
B z((A()));
For further information, please see:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439
and
http://en.wikipedia.org/wiki/Most_vexing_parse

HTH,
-- Saeed Amrollahi
 
A

AdlerSam

Put the temporary object inside (). I mean
B z((A()));
Thanks a lot, the workaround is _exactly_ what I was looking for!
This is known as the "most vexing parse" problem.
And while I new the problem before, it's nice being able to name it
now.
 
J

James Kanze

please consider the following example:
struct A {
A(int) {}
A() {}};
struct B {
B(const A &a): _a(a) {}
B(int x, const A &a): _a(a) {}
void f() {}
A _a;};
int main() {
B v[] = {
B(A(3)),
B(27, A()),
B(A())
};
v[0].f();
v[1].f();
v[2].f();
B x(A(3));
B y(27, A());
B z(A());
x.f();
y.f();
z.f();
}
The last line doesn't compile:
tst.cpp:26: error: request for member ‘f’ in ‘z’, which is of non-
class type ‘B ()(A (*)())’
Obviously, because the compiler thinks that z is a function.

That's because it's declared as a function:).
While it is easy to work arround the problem with an intermediate
variable for class B, it annoys me because I need to instantiate a
rather big amount of objects, an I'd like to do it in a table-like
manner.
Thus, my question: Is there a way to tell the compiler that z is an
object while keeping the table-based initialization sceme?

Just add an extra pair of parentheses: B z((A()));
or use copy initialization: B z = B(A());
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top