Operator << Must Take Exactly One Argument

operator must take exactly one argument

The problem is that you define it inside the class, which

a) means the second argument is implicit (this) and

b) it will not do what you want it do, namely extend std::ostream.

You have to define it as a free function:

class A { /* ... */ };
std::ostream& operator<<(std::ostream&, const A& a);

Getting an error of the form operator must take exactly one argument when overloading operator

I think that the issue here is that you're (probably unintentionally) mixing and matching two different approaches.

When you write

friend std::ostream& operator <<( std::ostream& os , const BigInt ¶m );

inside of the class definition, you are saying that there will be a free function named operator << that takes in an ostream and a BigInt. When you write

std::ostream& BigInt::operator <<( std::ostream& os , const BigInt & param )
{
...
};

you are defining a member function of BigInt named operator << taking two arguments. Those two arguments, combined with the implicit this pointer, adds up to three arguments - more than you intended. Note that although this function is called operator<<, it's not the operator<< you declared as a friend in the class definition.

To fix this, you have a few options. First, when you define operator<<, you can omit the BigInt:: prefix, which will fix things. Alternatively, combine your implementation with the friend definition inside the class definition:

friend std::ostream& operator <<( std::ostream& os , const BigInt & param )
{
...
}

Function must have exactly one argument

Check the reference

The overloads of operator>> and operator<< that take a std::istream&
or std::ostream& as the left hand argument are known as insertion and
extraction operators. Since they take the user-defined type as the
right argument (b in a@b), they must be implemented as non-members.

Hence, they must be non-member functions, and take exactly two arguments when they are meant to be stream operators.

If you are developing your own stream class, you can overload operator<< with a single argument as a member function. In this case, the implementation would look something like this:

template<class T>
TOutputFile &operator<<(const T& a) {
// do what needs to be done
return *this; // note that `*this` is the TOutputFile object as the lefthand side of <<
}

bool must take exactly one argument == operator implementation file

When compiling the program I am getting an error that says this function must take exactly one argument. [...] What am I doing incorrectly?

Your compiler is telling you exactly what you're doing wrong. You are writing operator== as a member function taking the wrong number of arguments. It must take exactly one.

You are comparing the implicit this object to the other object, so your code should be:

bool RewardCard::operator ==(const RewardCard& rhs)
{
return name == rhs.name && id == rhs.id && store == rhs.store;
}

operator(ostream&, const BigUnsignedI&) must take exactly one argument

BigUnsigned is a template type so

std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu)

Will not work as there is no BigUnsigned. You need to make the friend function a template so you can take different types of BigUnsigned<some_type>s.

template <typename I>
class BigUnsigned{
const size_t cell_size=sizeof(I);
std::vector<I> _integers;
public:
BigUnsigned();
BigUnsigned(I);
template<typename T>
friend std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu);
};

template<typename T>
std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu){
for (auto integer : bu._integers){
out<<integer<<std::endl;
}
return out;
}

The reason the second example works is that since it is declared inside the class it uses the template type that the class uses.

overloading operator c++, I am trying to cout the element of class

It seems you mean the following

std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}

provided that all used in the operator data members are public data members of the class Box. The operator shall be declared and defined outside the class definition.

If one of the used data members is a private data member of the class then the function should be a friend function of the class and shall be declared (and may be defined) in the class definition. For example

class Box
{
//...
friend std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}
//...
};

Pay attention to that it is better to remove in the return statement the operand std::endl. In this case 1) the operator will be more flexible because you can output additional information in the same line and 2) this statement

std::cout << box;

will not confuse readers of the code because they will not see the operand std::endl.

Without this operand in the operator definition in the caller of the operator you can write

std::cout << box << std::endl;

and this statement more clear expresses the intention of the programmer.



Related Topics



Leave a reply



Submit