I got an error, when I am trying to overload an operator.
My header file:
#include<iostream>
#include<string>
using namespace std;
#ifndef HALLGATO_H
#define HALLGATO_H
class Hallgato {
private:
char* nev;
char* EHA;
int h_azon;
unsigned int kepesseg;
public:
friend ostream& operator<<(ostream& output, const Hallgato& H);
};
#endif
My cpp file:
#include<iostream>
#include "Hallgato.h"
using namespace std;
ostream& Hallgato::operator<<(ostream& output, const Hallgato& H) {
output << "Nev: " << H.nev << " EHA: " << H.EHA << " Azonosito: " << H.h_azon << " Kepesseg: " << H.kepesseg << endl;
return output;
}
};
In my .cpp file, when I want to define the overloaded operator <<
, I got an error. Why?
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.
C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of . h and contains class definitions and functions. The implementation of the class goes into the .
Most can be overloaded. The only C operators that can't be are . and ?: (and sizeof , which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: and .
cpp files contain computer code. They are used differently, however: by convention, . h files are header files designed to be copied into another program file; a . cpp file has code designed to be compiled, and if necessary, combined using a linker.
So the main idea behind “Operator overloading” is to use c++ operators with class variables or class objects. Redefining the meaning of operators really does not change their original meaning; instead they have been given additional meaning along with their existing ones. What is the difference between operator functions and normal functions?
The operator overloading function may be a member function when a Left operand is an object of the Class. When the Left operand is different, the Operator overloading function should be a non-member function.
Here are rules for Operator Overloading: For it to work, at least one operand must be a user-defined class object. You can only overload existing operators. You can’t overload new operators. Some operators cannot be overloaded using a friend function. However, such operators can be overloaded using member function.
It is a healthy practice to use function overloading and operator overloading in C++ because if any errors are present in the code, they are resolved before runtime as function and operator overloading in C++ are types of compile-time polymorphism.
The operator is not a member of the class, it is a friend so
ostream& Hallgato::operator<<(ostream& output, const Hallgato& H) {
should be
ostream& operator<<(ostream& output, const Hallgato& H) {
also to be able to use the operator from other files you should add a prototype into the header file.
The header file would become this
hallgato.h
#ifndef HALLGATO_H
#define HALLGATO_H
#include<iostream>
#include<string>
class Hallgato {
private:
char* nev;
char* EHA;
int h_azon;
unsigned int kepesseg;
public:
friend std::ostream& operator<<(std::ostream& output, const Hallgato& H);
};
std::ostream& operator<<(std::ostream& output, const Hallgato& H);
#endif /* End of HALLGATO_H */
Somewhere in a ".cpp" file you would implement the operator function, you can also do it in the header file but then you would have to recompile often with some compilers.
hallgato.cpp
#include "hallgato.h"
std::ostream& operator<<(std::ostream& output, const Hallgato& H)
{
/* Some operator logic here */
}
NOTE:
When you modify header files, many compilers usually do not re-include them in your .cpp
files. This is done to avoid unnecessary recompilation. To force a re-include, you have to make some modifications(delete empty line) to the source files which include those headers or force recompilation in your compiler/IDE.
In header file you declared friend method for class
friend ostream& operator<<(ostream& output, const Hallgato& H);
this method shoud be defined (in cpp) without Hallgato::
ostream& operator<<(ostream& output, const Hallgato& H)
because this method is not part of Hallgato class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With