c++ - 使用流运算符构建错误

标签 c++ codeblocks stream-operators

<分区>

无法构建代码块,因为我为 cout(ing) 我的类 Duree 实例插入了一个新的流运算符。

我在“Duree”类下发起了一个原型(prototype)“afficher”

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

流运算符函数在类头之外。

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

和源文件夹下的方法:

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

主文件夹下,执行

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

构建控制台返回以下内容:

------------ 构建:在 lesOperateurs2 中调试(编译器:GNU GCC 编译器)----------------

g++ -o bin/Debug/lesOperateurs2 obj/Debug/Duree.o obj/Debug/main.o
体系结构 x86_64 的 undefined symbol : “operator<<(std::__1::basic_ostream >&, Duree const&)”,引用自: _main 在 main.o 中 ld: 找不到体系结构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) 进程终止,状态为 1(0 分钟,0 秒) 0 个错误,0 个警告(0 分钟,0 秒)

它有时似乎有效,你知道发生了什么吗?

完整代码如下

持续时间

#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;
}

和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;
}

最佳答案

您需要在类声明中将重载运算符标记为 friend:

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

此外,我没有看到它的实现:

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

编辑:

在@user4581301 的建议下,没有将其加为好友:

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

好机会!

关于c++ - 使用流运算符构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42701980/

相关文章:

c++ - G++ -M32 -M64 在 Windows 上切换

c++ - 如何为提升日志和自定义类型定义输出流运算符

C++ IBM i Access API 运行远程命令并获取输出

java - 如何制作一个同时编译 C、C++ 和 Java 文件的 makefile

c++ - 如何测试 Audio Session 的相等性

c - 链接代码::阻止自定义库错误(找不到)

c++ - 代码块停止使用 gnome-terminal (linux) 执行

c++ - 抽象类中的运算符 <<,C++

c++ - nullptr 的运算符 <<(流输出)

c++ - Webkit GTK : Stop loading and reset web view