J
jason.cipriani
The following program fails to compile with what appears to be a
really strange error:
struct A {
};
struct B {
B (const A&);
void f();
};
int main () {
B b(A()); // pass temporary A to constructor.
b.f();
}
Comeau gives:
"ComeauTest.c", line 12: error: expression must have class type
b.f();
MSVC gives:
c:\temp\pqueue\pqueue.cpp(12) : error C2228: left of '.f' must have
class/struct/union
And "geordi", some weird C++ compiling IRC bot, gives:
error: request for member 'f' in 'b', which is of non-class type 'B ()
(A (*)())'
What the heck is going on here? I am able to make it compile by
putting the A() inside an extra set of parentheses:
int main () {
B b((A()));
b.f();
}
But I'm not sure why it's mistaking what I'm trying to do for a
function pointer declaration. I first ran into this error in the
seemingly reasonable program:
#include <queue>
int main () {
std:riority_queue<int> pq(less<int>());
pq.push(0);
}
Where I was attempting to pass a predicate function to the
constructor. Although the following fails with the same error:
#include <queue>
int main () {
std:riority_queue<int> pq();
pq.push(0);
}
In fact, any program of this form fails to compile:
struct A { void f(); };
int main () {
A a();
a.f();
}
And that completely boggles my mind for two reasons:
1. While empty parens for default constructor are not the usual
style, that seems like entirely reasonable code.
2. I'm no master, but I am completely confused as to how I've never
noticed this before, I feel like I've been programming long enough to
have run into it at least once.
Why are these programs not compiling? I mean, I understand that it
thinks I'm declaring function pointers for some reason, but it doesn't
make any sense to me why it would think that.
Thanks,
JC
really strange error:
struct A {
};
struct B {
B (const A&);
void f();
};
int main () {
B b(A()); // pass temporary A to constructor.
b.f();
}
Comeau gives:
"ComeauTest.c", line 12: error: expression must have class type
b.f();
MSVC gives:
c:\temp\pqueue\pqueue.cpp(12) : error C2228: left of '.f' must have
class/struct/union
And "geordi", some weird C++ compiling IRC bot, gives:
error: request for member 'f' in 'b', which is of non-class type 'B ()
(A (*)())'
What the heck is going on here? I am able to make it compile by
putting the A() inside an extra set of parentheses:
int main () {
B b((A()));
b.f();
}
But I'm not sure why it's mistaking what I'm trying to do for a
function pointer declaration. I first ran into this error in the
seemingly reasonable program:
#include <queue>
int main () {
std:riority_queue<int> pq(less<int>());
pq.push(0);
}
Where I was attempting to pass a predicate function to the
constructor. Although the following fails with the same error:
#include <queue>
int main () {
std:riority_queue<int> pq();
pq.push(0);
}
In fact, any program of this form fails to compile:
struct A { void f(); };
int main () {
A a();
a.f();
}
And that completely boggles my mind for two reasons:
1. While empty parens for default constructor are not the usual
style, that seems like entirely reasonable code.
2. I'm no master, but I am completely confused as to how I've never
noticed this before, I feel like I've been programming long enough to
have run into it at least once.
Why are these programs not compiling? I mean, I understand that it
thinks I'm declaring function pointers for some reason, but it doesn't
make any sense to me why it would think that.
Thanks,
JC