-1

Im am writing a code using the concept of Composition: Objects as Members of Classes and this error appeared for me: [Error] no match to 'operator<<'. Any one can help me? The error started appearing when i tried to include the trabalhador.h and trabalhador.cpp files. If i try to compile the code without this two files it works but i need bouth because im trying to implement this concept by my own.

Here is the code:


//main

#include <iostream>
using std::cout;

#include "trabalhador.h"

int main()
{
    cout << "INFORME OS DADOS SOLICITADOS: \n\n";

    cout << "Nome do funcionario: ";

    info i1;

    cout << "CPF: ";

    info i2;

    trabalhador t(i1, i2);
    
    return 0;
}

//trabalhador.h

#ifndef TRABALHADOR_H

#define TRABALHADOR_H

#include "info.h"

class trabalhador

{

    public:

        trabalhador (const info &, const info &);

        void print () const;

    private:

        const info funcionarioNome;

        const info funcionarioCPF;

};

#endif

//trabalhador.cpp

#include <iostream>

using std::cout;

#include "trabalhador.h"

#include "info.h"

trabalhador::trabalhador (const info &infoNome, const info &infoCPF)

:funcionarioNome (infoNome),

funcionarioCPF (infoCPF)

{

    cout << "Dados do funcionario: ";

}

void trabalhador::print() const

{

    cout << funcionarioNome << funcionarioCPF;

}

//info.h

#include <string>

using std::string;

#ifndef INFO_H

#define INFO_H

class info

{

    public:

        info (string = "");

        void setInfoDado (string);

        void setInfo ();

    private:

        string infoDado;

};

#endif

//info.cpp

#include <iostream>

using std::cout;

using std::cin;

#include <string>

using std::string;

using std::getline;

#include "info.h"

info::info (string info)

{

    setInfoDado (info);

}

void info::setInfoDado (string info)

{

    infoDado = info;

    setInfo();

}

void info::setInfo ()

{   

    string nome;

    getline (cin, nome);

    infoDado = nome;

}
ROR
  • 1
  • 2
  • 1
    Does this answer your question? [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – kesarling He-Him Sep 25 '20 at 03:51
  • Does this answer your question? [no match for ‘operator<<’ in ‘std::operator](https://stackoverflow.com/questions/22588163/no-match-for-operator-in-stdoperator) – Paul Rooney Sep 25 '20 at 04:41

2 Answers2

1

You need to provide an overloaded operator<< for your info class.

The requirement for this stems from the line

cout << funcionarioNome << funcionarioCPF;

It needs to know how to render the class using an ostream.

It's as simple as

class info
{
    public:
        // ...
        friend std::ostream& operator<<(std::ostream& os, info const& inf)
        {
            os << inf.infoDado;
            return os;
        }
    // ...
};

See the full code here.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • But i dont want to use the "friend" concept as you used in your code. I want to use just the concept of Composition: Objects as Members of Classes – ROR Sep 25 '20 at 05:12
  • I'm not exactly sure what you mean by 'concept of Composition: Objects as Members of Classes'. This is the usual way to achieve output to `cout`. You could also implement it by exposing a getter on `info` and having the the overload function use it, then friend is not required. – Paul Rooney Sep 25 '20 at 05:24
  • I saw where is the problem. I am studying C++ and at point I am I saw that an object can be used as a member of a class where it was not declared (an object of one class can be a member of another class - this is composition). The problem was at cout << funcionarioNome << funcionarioCPF; as Paul said but i didnt want to put a class as friend of another one. What I did was create a function print in trabalhador.cpp and another in info.cpp. In print from trabalhador I used funcionarioNome and funcionarioCPF to call print in info and print infoDado. – ROR Sep 25 '20 at 06:10
0
//new constructor in trabalhador.cpp
trabalhador::trabalhador (const info &infoNome, const info &infoCPF)
:funcionarioNome (infoNome),
funcionarioCPF (infoCPF)
{
    cout << "\nDados do funcionario: \n";
    print();
}

//function created in trabalhador.cpp
void trabalhador::print() const
{
    funcionarioNome.print();
    funcionarioCPF.print();
}

//function created in info.h
void print () const;

//function created in info.cpp
void info::print () const
{
    cout << infoDado << "\n";
}
ROR
  • 1
  • 2