[] overloading c++ array of functions not allowed - c++

Hello im trying to create a graph and im working on my function prototypes, on my [] operator overload function im getting the following error:array of functions not allowed here is my prototype:
BinaryTree& ooperator[](int vertex);
binary tree is a binary tree class what do i need to change to get this to work

You've misspelled operator as ooperator. Oops!
BinaryTree& operator[](int vertex);
// ^

Related

CPOI and CWaypoint

I have used operator overloading for +=operator like this
CRoute& CRoute::operator+=(string name ){
CWaypoint *p=this->m_pWpDatabase->getPointerToWp(name);
if(p!=NULL){
this->addWaypoint(name);
}
else{
cout<<" Waypoint not found in DB "<<endl;
}
return *this;
}
Now i want to do same operation += to add poi to my route,i am using the syntax
CRoute& operator+=(string namepoi);
but i am getting error message as 'CRoute& CRoute::operator+=(std::string)' cannot be overloaded
Can someone help??
Function signature of your suggested operator overloaded function is conflicting with the one existing currently. So if you would like to create operator overloading for poi why can't you pass the poi object instead its name?

Overloaded operator has too many parameters, visual studio c++

I'm trying to declare an overload, non-friend, non-member ' - - operator in a header file:
Quad operator-(const Quad &qu1, const Quad &qu2);
But I am getting:
"error C2804: binary 'operator -' has too many parameters"
This code is right from the book and problem statement and I cannot seem to resolve it. Thanks for your help.
Binary operators in class definition scope must take only one argument.
Quad operator-(const Quad &quRight)
{
Quad res;
res.x = this->x - quRight.x;
// all other components
// ...
return res;
}
Or you can move operator overloading outside of class.

Overloaded methods - (Error C2664: Cannot convert from vector<T> to T)

I'm trying to call an overloaded method from the other overloaded member. I am getting an error C2664: Cannot convert argument 2 from std::vector<PK_BODY_T*, std::allocator<_Other>> to PK_BODY_T
Code:
std::vector<PK_BODY_t*> FillHoles(std::vector<std::vector<PK_EDGE_t>> holes, PK_BODY_t inputBody)
{
std::vector<PK_BODY_t*> vectorBodies;
PK_BODY_t *pointerInputBody = new PK_BODY_t(inputBody);
vectorBodies.push_back(pointerInputBody);
std::vector<PK_BODY_t*> returnVector;
returnVector = FillHoles(holes, vectorBodies); //<-- ERROR HERE. Calling overloaded method.
delete pointerInputBody;
return returnVector;
}
/* overloaded version of FillHoles
*/
std::vector<PK_BODY_t*> FillHoles(std::vector<std::vector<PK_EDGE_t>> holes, std::vector<PK_BODY_t*> inputBody)
{
//...
std::vector<PK_BODY_t*> fillHoleOutput = FillOneHole(currentBody, currentHole);
return fillHoleOutput;
}
It appears as if the first method is trying to call itself here, instead of the second overloaded method. How do I force it to use the second method?
You did not show how the functions are declared and in what scopes whether one function hides other function.
But in any case just declare the second overloaded function inside the first overloaded function
std::vector<PK_BODY_t*> FillHoles(std::vector<std::vector<PK_EDGE_t>> holes, PK_BODY_t inputBody)
{
std::vector<PK_BODY_t*> FillHoles(std::vector<std::vector<PK_EDGE_t>> holes, std::vector<PK_BODY_t*> inputBody);
//...
C++ files are compiled from top to bottom. From the vantage point of the top method, it cannot 'see' the second method, because it has not been compiled yet (as the second method is after the first method).
You must either declare methods in a header .h file, or forward declare the methods at the top of the .cpp file.
http://www.learncpp.com/cpp-tutorial/19-header-files/
http://www.learncpp.com/cpp-tutorial/17-forward-declarations/

C++ References and Pointers

I have the following code in my program for a blackjack game:
Player *p;
Deck *d = new Deck();
Hand *playerHand = new Hand(), *dealerHand = new Hand();
p = get_Simple(); //This returns a pointer to an instance of a Simple Player for my game
Card dealerCard = d->deal();
p->draw(dealerCard, playerHand);
draw is defined as
virtual bool draw(Card dealer, const Hand &player);
When I try to run this code, I get the following error:
error: no matching function for call to 'Player::draw(Card&, Hand*&);
note: candidates are: virtual bool Player::draw(Card, const Hand&);
The quick-fix would be to match the call with the signature:
p->draw(dealerCard, *playerHand);
The correct way would be to go about your code and get rid of dynamic allocation and raw pointers, replacing them with smart pointers.
The error message is quite clear (after seeing it a couple of times):
error: no matching function for call to 'Player::draw(Card&, Hand*&);
note: candidates are: virtual bool Player::draw(Card, const Hand&);
In the code you are trying to pass a Card and a Hand* to a function, but the compiler did not find an overload that takes those. The closest it got is a function that takes a Card and a const Hand&.
You need to dereference the pointer to get to the object:
p->draw(dealerCard, *playerHand);
Alternatively consider whether you need to hold all objects by pointer or not. Code is much simpler if you avoid pointers.

Replacing a set object with a new set object

I have a class with a private field:
std::set<std::string> _channelNames;
.. and an optional setter function:
void setChannelNames(std::set channelNames);
In the setter function, how do I replace the private _channelNames field with the one passed from the setter function?
I tried:
void Parser::setChannelNames(std::set channelNames) {
this->_channelNames = channelNames;
}
But this produced an error in VS2005:
Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::set' (or there is no acceptable conversion) parser.cpp 61
I am definitely a C++ novice, and expect that I should be doing some pointer work here instead.
Any quick tips?
Thanks!
You just have to specialize template. You cannot use std::set without specialization.
void Parser::setChannelNames(const std::set<std::string> & channelNames) {
this->_channelNames = channelNames;
}