0
votes

My program returns and error et I don't know why . How I can resolve this ?

Error:

error: cannot bind 'std::ostream {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&'| error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = Personne]'|

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct Personne { int age; int nb_ami; };

int main()
{
    std::cout << "Entrer le nom du fichier: " << std::endl;
    std::string fileUser{};
    std::cin >> fileUser;
    std::ofstream(fileUser.c_str());
    std::cout << "Quel age avez-vous ?" << std::endl;
    std::vector<Personne> tab(1);
    std::cin >> tab[0].age;
    for (unsigned int i{}; i < tab.size(); i++){
        std::cout << tab[i] << std::endl;
    }
    return 0;
}
2
It would really help to give the line number, where this error occurred.dornhege
Shouldn't it be Entrez le nom du fichier?cangrejo

2 Answers

2
votes

cout does not know how to display a Personne. You must either put the contents, i.e. age, nb_ami directly to the output or provide an overload for operator<< that takes an ostream and a Personne.

1
votes

Terrible error message. The reason is this line:

std::cout << tab[i] << std::endl;

You haven’t told the compiler how to stream an object of type Personne to the output. You need to define an appropriate operator<< for your type.