Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading in header files and in the cpp files

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?

like image 942
B.J Avatar asked Nov 02 '17 19:11

B.J


People also ask

What is operator overloading in C++?

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.

What are header and cpp files?

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 .

Which operators can be overloaded in cpp?

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 .

What is the difference between header and cpp?

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.

What is operator overloading in C++?

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?

Is operator overloading a member function or non-member function?

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.

What are the rules for operator overloading in Java?

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.

Is it a healthy practice to use function overloading and operator overloading?

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.


2 Answers

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.

like image 191
Strong will Avatar answered Sep 21 '22 02:09

Strong will


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.

like image 24
james Avatar answered Sep 20 '22 02:09

james