Reputation: 3036
I'm having trouble understanding this compiler error. I wrote class(*)() in the post title because the class I am instantiating is called "opaque", but that name is not informative or relevant. It is a templatized Circular Buffer and some tests. I am using the opaque class to test the data structure with full class/struct types. I am also testing with primitives (int at the moment) and that test function does not give me this compiler error. There is more code than this, but I've provided what I think are the relevant sections. The full code is here if you are interested.
gcc error:
tests.cpp: In function ‘bool opaque_fill_test(int)’:
tests.cpp:97:23: error: no matching function for call to ‘CircBuf<opaque>::remove(opaque (*)())’
tests.cpp:
struct opaque {
int id;
opaque(int n): id(n) {}
opaque operator=(const opaque &o) { this->id = o.id; }
};
opaque rcv();
CircBuf<opaque> c(size);
for (int i=0; i<size; i++) {
if ( c.remove(&rcv)) {
if (rcv.id != i ) {
cout << "wrong value: " << rcv << " "
<< "should be: " << i << endl;
return false;
}
} else {
cout << "remove fail at i=" << rcv << endl;
return false;
}
}
CircBuf.h:
template<typename T> class CircBuf {
...
template<typename T> bool CircBuf<T>::remove(T *t) {
...
*t = data[front];
...
if i declare an opaque* and pass that to remove instead:
opaque rcv();
opaque* p = &rcv;
for (int i=0; i<size; i++) {
if ( c.remove(p)) {
...
...
i get a similar error:
tests.cpp: In function ‘bool opaque_fill_test(int)’:
tests.cpp:96:16: error: cannot convert ‘opaque (*)()’ to ‘opaque*’ in initialization
Upvotes: 2
Views: 198
Reputation: 137810
You hit the most vexing parse.
if i declare an opaque* and pass that to remove instead:
opaque rcv();
That declares a function returning opaque
, not a default-initialized opaque
. Instead use opaque rcv;
or opaque rcv = {};
.
As for the error message, opaque (*)()
is the type of a pointer to a function returning opaque
. The name of the "function" was implicitly converted to a pointer when used for something besides a call.
Upvotes: 0
Reputation: 4992
If you have the declaration foo x();
, the expression &x
will actually be a function pointer assignable to something like foo *(y)()
. The p
variable in your last example isn't such a function pointer, but simply a pointer to an opaque
struct. Thus you cannot assign the function pointer to that variable.
[edit]
Just remembered: maybe you meant to declare an opaque
variable and initialize it using the default constructor. Leave out the parentheses then. It's a left-over syntax from C. You can only use the parentheses there if you actually put values in between to call a different constructor.
Upvotes: 1
Reputation: 44444
What you are doing with:
opaque rcv();
is that you are "declaring a function" with the name rcv
having the return type opaque
, and taking no arguments. Check this link
Drop the paranthesis:
opaque rcv;
Upvotes: 1