c++codeblocksstream-operators

Build bug with stream operator


Code blocks can't build since I inserted a new stream operator for cout(ing) my class Duree instances.

I initiated a prototype "afficher" under class "Duree"

void afficher(std::ostream &out) const;

The stream operator function is outside the class header.

std::ostream& operator<<(std::ostream& out, Duree const& duress);

and under the source folder the method:

void Duree::afficher(ostream &out) const
{
    out << m_heures << "h" << m_minutes << "m" << m_secondes << "s";
}

under the main folder, the execution

cout <<duree1 <<"et"<<duree2 << end;

the build console returns the following:

-------------- Build: Debug in lesOperateurs2 (compiler: GNU GCC Compiler)---------------

g++ -o bin/Debug/lesOperateurs2 obj/Debug/Duree.o obj/Debug/main.o
Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream >&, Duree const&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Process terminated with status 1 (0 minute(s), 0 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

It seems to works sometimes, would you have an idea of what is happening.

complete code below

Durée.h

#ifndef DEF_DUREE
#define DEF_DUREE

class Duree
{
    public:

    Duree(int heures = 0, int minutes = 0, int secondes = 0);


    Duree& operator+=(const Duree &duree);
    Duree& operator-=(const Duree &duree);

//   void afficher() const;
//  void afficher(std::ostream &flux) const;
    void afficher(std::ostream &out) const;

    private:

    int m_heures;
    int m_minutes;
    int m_secondes;
};

Duree operator+(Duree const& a, Duree const& b);
Duree operator-(Duree const& a, Duree const& b);


std::ostream& operator<<(std::ostream& out, Duree const& duree);

#endif // DUREE_H_INCLUDED

Duree.cpp

#include <iostream>
#include "Duree.h"

using namespace std;

Duree::Duree(int heures, int minutes, int secondes)
    : m_heures(heures), m_minutes(minutes), m_secondes(secondes)
{

}

Duree& Duree::operator+=(const Duree &duree2)
{
    // 1 : ajout des secondes
    m_secondes += duree2.m_secondes; // Exceptionnellement autorisé car même classe
    // Si le nombre de secondes dépasse 60, on rajoute des minutes et on met un nombre de secondes inférieur à 60
    m_minutes += m_secondes / 60;
    m_secondes %= 60;

    // 2 : ajout des minutes
    m_minutes += duree2.m_minutes;
    // Si le nombre de minutes dépasse 60, on rajoute des heures et on met un nombre de minutes inférieur à 60
    m_heures += m_minutes / 60;
    m_minutes %= 60;

    // 3 : ajout des heures
    m_heures += duree2.m_heures;

    return *this;
}

Duree& Duree::operator-=(const Duree &duree2)
{

int totalSecondes(0);
int totalSecondes2(0);

totalSecondes = m_secondes + m_minutes*60 + m_heures*3600;
totalSecondes2 = duree2.m_secondes + duree2.m_minutes*60 + duree2.m_heures*3600;

totalSecondes -= totalSecondes2;

if(totalSecondes<=0)
{
totalSecondes=0;
}

m_heures = totalSecondes/3600;
totalSecondes %= 3600;

m_minutes = totalSecondes/60;
totalSecondes %=60;

m_secondes = totalSecondes;

    return *this;
}
/*
void Duree::afficher() const
{
    cout << m_heures << "h" << m_minutes << "m" << m_secondes << "s" << endl;
}
*/

void Duree::afficher(ostream &out) const
{
    out << m_heures << "h" << m_minutes << "m" << m_secondes << "s";
}

/*
void Duree::afficher(ostream &flux) const
{
    flux << m_heures << "h" << m_minutes << "m" << m_secondes << "s";
}
*/


Duree operator+(Duree const& a, Duree const& b)
{
    Duree copie(a);
    copie += b;
    return copie;
}

Duree operator-(Duree const& a, Duree const& b)
{
    Duree copie(a);
    copie -= b;
    return copie;
}

and main.cpp

#include <iostream>
#include "Duree.h"

using namespace std;

int main()
{
    Duree duree1(18, 00, 42), duree2(2, 53, 27);
    Duree resultat;

    duree1.afficher();
    cout << "-" << endl;
    duree2.afficher();

   resultat = duree1 - duree2;

    cout << "=" << endl;
    resultat.afficher();


    cout <<duree1 <<"et"<<duree2 << endl;

    return 0;
}

Solution

  • You need to mark your overloaded operator as friend in your class declaration:

    friend std::ostream& operator<<(std::ostream& out, Duree const& duree);
    

    Also, I didn't see its implementation:

    std::ostream& operator<<(std::ostream& out, Duree const& duree)
    {
        out << duree.m_heures << "h" << duree.m_minutes << "m" << duree.m_secondes << "s";
        return out;
    }
    

    EDIT:

    Without making it a friend, after @user4581301 suggestion:

    std::ostream& operator<<(std::ostream& out, Duree const& duree)
    {
        duree.afficher(out);
        return out;
    }
    

    Bonne chance!