[C++] operateur<<() aide SVP
Résolu
progfann
Messages postés
373
Statut
Membre
-
Char Snipeur Messages postés 10112 Statut Contributeur -
Char Snipeur Messages postés 10112 Statut Contributeur -
Bonjour,
j'ai passé des heurs et des heurs pour résoudre ce problème des opérateur. voila l'erreur:
livre.h:
livre.cpp:
main:
j'ai passé des heurs et des heurs pour résoudre ce problème des opérateur. voila l'erreur:
In file included from main.cpp ISO C++ forbids declaration of `ostream' with no type `ostream' is neither function nor member function; cannot be declared friend expected `;' before '&' token In function `int main(int, char**)': no match for 'operator<<' in 'std::cout << a' std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] note std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
livre.h:
class livre{
private:
int annee;
char *titre;
char *auteur;
public:
livre(int a=0,char*t="",char*au="");
livre(const livre &);
~livre();
void ecrire();
void saisir();
friend ostream& operator<<(ostream&,const livre&);
};
livre.cpp:
#include <iostream>
#include <cstdlib>
#include "livre.h"
using namespace std;
livre::livre(int a,char*t,char*au)
{
annee=a;
titre=new char[strlen(t)+1];
strcpy(titre,t);
auteur=new char[strlen(au)+1];
strcpy(auteur,au);
}
livre::livre(const livre & a)
{
annee=a.annee;
titre=new char[strlen(a.titre)+1];
strcpy(titre,a.titre);
auteur=new char[strlen(a.auteur)+1];
strcpy(auteur,a.auteur);
}
livre::~livre()
{if(titre)
delete []titre;
if(auteur)
delete []auteur;
}
void livre::saisir()
{cout<<"entrer l annee :";
cin>>annee;
cout<<"entrer le titre :";
cin>>titre;
cout<<"entrer l'auteur :";
cin>>auteur;
}
void livre::ecrire()
{cout<<"l'annee est:"<<annee<<endl;
cout<<"le titre est:"<<titre<<endl;
cout<<"l'auteur est:"<<auteur<<endl;
}
ostream& operator<<(ostream &oo,const livre &l){
oo<<"annee : "<<l.annee<<endl;
oo<<"titre : "<<l.titre<<endl;
oo<<"auteur : "<<l.auteur<<endl;
return oo;
}
main:
#include <iostream>
#include <cstdlib>
#include "livre.h"
using namespace std;
int main(int argc, char *argv[])
{livre a;
a.saisir();
cout<<a;
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
A voir également:
- [C++] operateur<<() aide SVP
- Opérateur de réseau mobile - Guide
- 0668 quel opérateur ✓ - Forum Mobile
- 0758 quel opérateur - Forum Opérateurs & Réseaux mobiles
- 0466 quel opérateur - Forum Mobile
- 0785 quel operateur ✓ - Forum Mobile