The meaning of & as function parameter in a compile error - c++

I'm new to C++ and am trying to interpret what the compiler is telling me. I'm calling the function this way:
Object *clientConnection = new Object();
function(clientConnection);
and getting the following error:
error: no matching function for call to 'function(Object*&)'
I'm trying to give a meaning to the following part Object*&. If I passed a pointer of the Object to the function what's with catch with the &?

It means you passed an lvalue of type Object*. If you passed an rvalue of type Object*, you would see a different error:
function(&*clientConnection);
should give
error: no matching function for call to 'function(Object*)'
This information is part of the error message, because some functions can only be called with lvalues, and if you pass an rvalue, this lack of & points you towards the problem.

You could implement function(clientConnection) in two ways
Call by value where the content of variable clientConnection will
be copied in 'p'
void function(Object* p)
Call by reference where 'p' is an alias of clientConnection in
function body.
void function(Object* &p)
When both of above definitions are missing, the compiler prints one of them mostly
function(Object*&)

Related

Function taking a raw pointer tries to accept the pointer by reference as outlined in error msg

While looking for some help with a problem of mine, I've found this post: error: no matching function for call to 'begin(int*&)' c++
I get it why there's an error there, but why does it say "... for call to 'begin(int*&)'" and not "... for call to 'begin(int*)"? It looks to me like the ia pointer is being passed by value and not reference.
Just for reference (no pun intended) - the following code didn't compile with the message no matching function for call to 'begin(int&)':
#include <iostream>
#include <iterator>
using namespace std;
void print(int ia[])
{
int *p = begin(ia);
while(p != end(ia))
cout<<*p++<<'\t';
}
int main()
{
int ia[] = {1,2,3,4},i;
print(ia);
return 0;
}
Since no matching function could be found, the compiler tells you the specific types of the parameters being passed as that's the only information it has to work with. Since you have a named lvalue here it's first assumption is that you're passing by reference and so that's what the diagnostic message prints.
If for example you had passed a literal there would be no way for a reference function to be correct so it would assume you were passing by value.
For me the code above compiles with the same error:
'no matching function for call to ‘begin(int*&)'
And what is more important:
error: request for member ‘begin’ in ‘__cont’, which is of non-class type ‘int*’
Take a look at the std::begin signature: http://en.cppreference.com/w/cpp/iterator/begin
In your example template ia will be int* as array gets implicitly converted. Next take a look link above and see that all signatures that are not arrays has:
-> decltype(c.begin()).

How to take address of non-static member function to use within QtConcurrent?

I'm trying to run non-static member function in the other thread. If I go:
void *(PortManager::*innerAskPtr)() = &this->innerAsk;
QFuture<void> f = QtConcurrent::run(innerAskPtr);
it prompts that
ISO C++ forbids taking the adress of an unqualified or parenthesized non-static member function to form a pointer to member function.
but if I delete this extra reference symbol:
void *(PortManager::*innerAskPtr)() = this->innerAsk;
QFuture<void> f = QtConcurrent::run(innerAskPtr);
it goes that it
cannot convert 'PortManager::innerAsk' from type 'void (PortManager::)()' to type 'void* (PortManager::*)()`
What to add on the right side to get these extra stars (*) on the left?
But still, even if I would get there, there is always another error; about the run(T(*)()):
no matching function for call to 'run(void* (PortManager::*&)())
it's so over my head to understand how this reference got there...
The documentation for QtConcurrent::run seems to explain all this.
Using Member Functions
QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.
There are code examples immediately following this text.
In your code:
void *(PortManager::*innerAskPtr)() = this->innerAsk;
QFuture<void> f = QtConcurrent::run(innerAskPtr);
the error message indicates that this->innerAsk returns void, but you are trying to assign it to a pointer-to-member-function returning void *. You probably meant:
void (PortManager::*innerAskPtr)() = &PortManager::innerAsk;
but you don't need to do this in order to call QtConcurrent::run, as the code examples show you can just write:
QtConcurrent::run( this, &PortManager::innerAsk );

error assigning a function pointer passed as an argument in the constructor to a private instance variable

I'm trying to assign a function pointer that is an argument in the constructor of a class to a private instance variable. (The purpose of this is to be able to pass a callback function to the constructor and use said callback function in member functions of the class.)
I thought I declared the instance variable correctly as a function pointer. The way I read the code snippet, the left operand "cmp" is a pointer, not a function, just like the right operand. (Both are pointers to functions of the same type, or so I thought.) However, the error message says "function as left operand." What am I doing wrong or misinterpreting? Is there a "most vexing parse" issue somewhere?
How can I fix this error and assign the function pointer?
I'm getting the following error message:
"error C2659: '=' : function as left operand"
The error is occurring in the following snippet of code:
template <typename Type>
PQueue<Type>::PQueue(int (cmpFn)(Type,Type))
{
cmp = cmpFn;
}
The compiler is complaining about
cmp = cmpFn;
The constructor prototype is
PQueue(int (cmpFn)(Type, Type) = OperatorCmp);
(OperatorCmp is just another function pointer.)
I declared the instance variable in the private section as
int (cmp)(Type, Type);
The IDE I'm using is Visual C++ 2008 Express, if that matters. (using old version to be compatible with old course materials).
The declaration
int (cmp)(Type, Type);
indeed declares a function, not a function pointer. However, the same syntax used in the parameter list for a function declaration denotes a function pointer, so cmpFn is in fact of type int (*)(Type, Type). Just change the declaration for cmp to
int (*cmp)(Type, Type);
and you'll be good.

Parameter passing error

I have made a function like this:
void function(Objectx &x);
And I call the function like this:
Objectx o;
function(o);
in the same class.
When I compile it I get this:
error: no matching function for call to ‘function(Objectx)’
note: candidate is: void function (Objectx&)
Sorry if is a lame question, but I didn't find a solution anywhere. Do you have any suggestions?
I don't think you've shown the real code causing the error. (For one thing, the alleged code has Obectx and the error says Objectx)
That error would occur if you passed a temporary value (rvalue), because an non-const reference cannot bind to an rvalue.
If the function doesn't change its parameter, change the signature to:
void function(const Objectx &x);
If the function does change its parameter, you will need to store the temporary value to a variable, and pass the variable. That way any changes made by the function end up in a variable you can access after the call.

No matching function for call (expects reference to pointer instead of pointer)

I get the error from xcode (3.2.4)/gcc(4.0):
/Users/admin/scm/audacity/mac/../src/toolbars/DeviceToolBar.cpp: In member function 'void DeviceToolBar::ShowInputDialog()':
/Users/admin/scm/audacity/mac/../src/toolbars/DeviceToolBar.cpp:817: error: no matching function for call to 'DeviceToolBar::ShowComboDialog(wxChoice*&, wxString)'
/Users/admin/scm/audacity/mac/../src/toolbars/DeviceToolBar.h:74: note: candidates are: void DeviceToolBar::ShowComboDialog(wxChoice*, wxString&)
So it looks like it expects a reference to a pointer in ShowComboDialog, but I don't know why as the signatures are clearly normal pointers. Furthermore if it was expecting a reference to a pointer the way I am calling it should work.
This is the first error, and there are no special warnings before it.
Also, this compiles in MSVC 2008 express.
Please give me a clue.
//in the class def
//(only relevant portions included
class DeviceToolBar:public ToolBar {
public:
DeviceToolBar();
virtual ~DeviceToolBar();
void ShowInputDialog();
private:
void ShowComboDialog(wxChoice *combo, wxString &title);
wxChoice *mInput;
};
//in the cpp file
void DeviceToolBar::ShowInputDialog()
{
ShowComboDialog(mInput, wxString(_("Select Input Device")));
}
void DeviceToolBar::ShowComboDialog(wxChoice *combo, wxString &title)
{
//...
}
The problem is not the first parameter; its the second. You're passing in a temporary wxString, but the function is expecting a reference. C++ will automatically convert a temporary to a const reference, but it cannot convert it to a reference. You need to make ShowComboDialog take a const reference as its second parameter.
Your ShowComboDialog takes a wxString by non-const reference and you are trying to pass a temporary object as an argument to this parameter. You can only bind const references to temporary objects.
You either need to change ShowComboDialog to take its second argument either by value (wxString) or by const reference (const wxString&) or you need to create a variable for the wxString that you create when you call the function and then pass (a reference to) that variable instead.