c++ error - function cannot be overloaded [duplicate] - c++

This question already has answers here:
Function overloading by return type?
(14 answers)
Closed 5 years ago.
Following is a c++ code for copying a binary tree. I am trying to overload copy function. I think it should work because return type of these two functions is different.
node* copy(node *onode,node *cnode)
{
if(root==NULL)
root=onode;
if(onode)
{
cnode=new node;
cnode->data=onode->data;
cnode->left=copy(onode->left,cnode->left);
cnode->right=copy(onode->right,cnode->right);
return cnode;
}
return cnode;
}
void copy(node *onode,node* cnode)
{
onode=copy(onode,cnode);
}
However, I get following error at compilation.
error: ‘void tree::copy(node*, node*)’ cannot be overloaded
void copy(node onode,node cnode)
error: with ‘node* tree::copy(node*, node*)’
node* copy(node *onode,node *cnode)
Thanks.

Return types can be different only if parameters are different according to rules of overloading

Related

operator== in C++ struct [duplicate]

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 1 year ago.
I'm trying to define an == operator in a structure:
typedef struct _tBScan {
QString strQuadpackNumbers;
uint uintBegin, uintEnd;
bool operator==(const struct _tBScan& a, const struct _tBScan& b) {
return (a.strQuadpackNumbers.compare(b.strQuadpackNumbers) == 0 &&
a.uintBegin == b.uintBegin && a.uintEnd == b.uintEnd);
}
} tBScan;
This won't compile, I get:
C2804: binary 'operator ==' has too many parameters
C2333: '_tBScan::operator ==' error in function: declaration: skipping function body
I'm using Qt 5.9.2 and MSVC 2015, I need to define this so I can use the QList compare function.
When overloading an binary operator as a member function, the first parameter is this pointer. In the signature you have defined the operator==, it will take 3 arguments. However, it can only take two.
In you case I would recommend making it a non-member function.
typedef struct _tBScan {
QString strQuadpackNumbers;
uint uintBegin, uintEnd;
} tBScan;
bool operator==(const struct _tBScan& a, const struct _tBScan& b) {
return (a.strQuadpackNumbers.compare(b.strQuadpackNumbers) == 0 &&
a.uintBegin == b.uintBegin && a.uintEnd == b.uintEnd);
}
When you overload the, let's say, operator# the expression _tBScan # _smt is resolved into.
_tBScan.operator#(_smt);
When it's not a member function the expression is resolved into
operator#(_tBScan, _smt);
So the compiler searches the overload of either of them.

Why does calling a functor with an undeclared variable work? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.

How to call a pointer to a member function which is retrieved from a map? [duplicate]

This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 5 years ago.
I'm trying to call a pointer to a member function, which I retrieve from a map. The call th.fpObjHandler(md, evd, tokenIdx) seems erroneous, I've tried various different syntaxes (e.g. .* and ->*), but I cannot seem to get it right. Hopefully somebody here is able to help.
struct TokenHandler
{
int tokenIdx;
eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};
Schema::event(MessageData &md, EventDataImpl &evd)
{
int tokenIdx = 0;
std::map<std::string, TokenHandler> tokenHandlerMap;
tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };
// ..
// ....
TokenHandler th = tokenHandlerMap.at(key);
if (th.fpObjHandler != NULL) {
th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
//..
//...
}
}
eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
//..
//...
return OK;
}
Schema.cpp:111:54: error: must use ‘.’ or ‘->’ to call
pointer-to-member function in ‘th.TokenHandler::fpObjHandler (...)’,
e.g. ‘(... ->* th.TokenHandler::fpObjHandler) (...)’
th.fpObjHandler(md, evd, tokenIdx);
^
First you need an instance of the class Schema, then use .* or ->*, something like:
Schema schema;
(schema.*th.fpObjHandler)(md, evd, tokenIdx);
Or, as you are already in a method of Schema:
(this->*th.fpObjHandler)(md, evd, tokenIdx);

What is an Equals Operator and how does it work? [duplicate]

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 7 years ago.
New to C++ and I need help understanding what a piece of code does. I was given a header file with a bunch of functions I need to define but I'm a little lost on what one means or how I would even begin to define it.
HERE IT IS:List& operator = (const List& source);
Also if you could show me how I would begin to define it that would really help. Because so far this is how I learned to define functions:
ClassName::FunctionName {
//Code goes here
}
So is this how I would do it?
List::List& {
//Code goes here
}
ClassName::FunctionName {
//Code goes here
}
is not right. It should be
ReturnType ClassName::FunctionName(Parameters) {
//Code goes here
}
Now in your example the class name is List, the return type is List&, the function name is operator= and the parameters are const List& source. So
List& List::operator=(const List& source) {
//Code goes here
}

Evaluating C++ pointer to function expression [duplicate]

This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 8 years ago.
I have a C++ class which contains the following definition:
class SomeClass:
public BaseClass
{
public:
SomeClass();
bool SomeClass::MyFunc( Json::Value& jsonRoot)
typedef bool(SomeClass::*PFUNC)(Json::Value&);
std::map<std::string, PFUNC> m_map;
}
later on in the c++ code i add values to the map variable using the following line:
SomeClass::SomeClass()
{
m_map["func"] = &SomeClass::MyFunc;
}
and for execution within one of SomeClass's methods:
std::map<std::string, PFUNC>::iterator itFunction = m_map.find("func");
if (itFunction != m_map.end())
{
PFUNC pfParse = m_map["func"];
Json::Value x;
this->*pfParse(x);
}
I end up getting the following compilation error:
error C2064: term does not evaluate to a function taking 1 arguments
I even tried using the iterator explicitly - this->*iterator->second(...) but ended up with the same error.
what am i doing wrong?
thanks
() has higher precedence than ->* so pfParse(x) is evaluated first. You need to use parenthesis to sequence the evaluation:
(this->*pfParse)(x);