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.
Related
I try to compile the following code:
class CFileOperations
{
...
template <typename T>
inline void load_and_save_data(std::fstream* stream, T& value, const EOperation operation)
{
switch (operation) {
case EOperation::OpSave:
*stream << value; <-- here
break;
case EOperation::OpLoad:
*stream >> value; <-- and here
break;
}
}
...
};
I get the following errors:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)
For example, I use it this way, with number being an 'int':
this->load_and_save_data(stream, number, operation);
I'm using Visual C++ 2019.
What's the root cause, and how to solve it. Any idea ?
My bad, one of the calls was with a 'class enum'. Of course, >> and << are not defined for it.
For #cdhowie, here are two examples of the resulting simplicity (with the help of load_and_save_data template methods):
Here mMembers is a std::unorderedmap (cf. save_and_load_data in the question above, I have also one for the starndard containers):
void CHexArea::load_and_save()
{
this->load_and_save_data((char&)mColor);
this->load_and_save_data(mTouchLeft);
this->load_and_save_data(mTouchRight);
this->load_and_save_data(mTouchBottom);
this->load_and_save_data(mTouchTop);
this->load_and_save_data(mMembers);
}
Here, in preferences, there are two versions of files:
void CHexPreferences::load_and_save()
{
if( this->is_loading() ) {
this->reset(); // version's forward compatibility
}
int version = 2;
this->load_and_save_data(version);
this->load_and_save_data(mBoardOrientation);
this->load_and_save_data(mBoardSize);
this->load_and_save_data(mComputerStarts);
this->load_and_save_data(mComputerInitialTurns);
if( version >= 2) {
this->load_and_save_data(mComputerTilesPerTurn);
}
this->load_and_save_data(mDebugFlags);
}
Simple and clear.
Of course, there are two methods (load() and save()) that are the outer interface and calls those here above, but: 1. They are part of a library (no need to rewrite them, OO as usual) and 2. The core of the load/save is written only once in load_save_data, with the advantage of simplicity, and having corresponding load and save code (types, order...).
Of course, there are cons, but I hope you'll see that it may make sense for some people to think that there are (IMHO very strong) pros as well.
The rest is a matter of taste.
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?
For the life of me, I cannot get this code to work. I'm attempting to convert my code from C# to C++ following the deprecation of the XNA framework, but a stubborn method does not want to be converted. In C# it is:
public Tile GetTileAtPosition(bool screenOrGame, Vector2 position)
{
if (screenOrGame)
{
return Array.Find(tileList, tile => tile.Position == position / 24);
}
else
{
return Array.Find(tileList, tile => tile.Position == position);
}
}
In C++, the code I'm attempting to use in place of this is:
Tile Level::GetTileAtPosition(bool screenOrGame, sf::Vector2f position)
{
vector<Tile>::iterator it;
if (screenOrGame)
{
it = find(tileList.begin(), tileList.end(), [position](const Tile &t) { return t.GetPosition() == sf::Vector2f(position.x / 24, position.y / 24); });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);
}
else
{
it = find(tileList.begin(), tileList.end(), [position](const Tile& t) { return t.GetPosition() == position; });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);
}
}
On the C++ assignment lines (it = ...) I am getting a painstaking error that I cannot figure out the cause of, or a solution for. It returns:
error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const Blobby::Level::GetTileAtPosition::<lambda_29eb981cd341d9c05d39c4654bc470b9>' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186
Is there any way to fix this error, or a better/more practical way to implement the method in C++?
In C++, the versions taking a comparator are sometimes suffixed with _if. This is the case for std::find. std::find takes an element to find, whereas std::find_if takes a comparator that implements equality. The error simply means that it couldn't find a match for a Tile being equivalent to a lambda.
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);
// ^
I'm currently hard at work on an assignment piece, which contains several custom datatypes. I've run into a problem where list is complaining that I am trying to remove a custom data type from a list of that same data type.
Error 3 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'customer' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\list 1194 1 Assignment 1 - Video Store MIS
The relevant code is here:
void customerCollection::removeCustomer(customer person)
{
customers.remove(person);
}
and the custom data type does have a == operator defined:
bool customer::operator==(customer &other) const
{
return (l_fullName == other.getName()) &&
(l_contactNumber == other.getNumber()) &&
(l_password == other.getPassword()) &&
(l_username == other.getUsername());
}
Is there any reason that the list type can't see the overloaded operator?
The customerCollection and customer data types are required parts of the program.
[EDIT] The overloaded operator is defined as public in the header file.
bool customer::operator==(customer &other) const
Try changing that to
bool customer::operator==(const customer &other) const
It is possible the code of the customers collection passes a const-qualified customer to the equality operator. At least, it is more idiomatic (and logical).
I'm inclined to say the reason is that the parameter is not const:
bool customer::operator==(const customer& other) const
depending on how remove is defined.