fonction amie d'une classe
bonjour tout le monde .
j'ai un souci a utiliser les fonction amies .
je possède de deux classes livre et stylo . et je veux pourvoir accéder aux attribut prives de la classe livre depuis une méthode membre de la classe stylo .
Stylo.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#ifndef STYLO
#define STYLO
#include "livre.h"
class Stylo{
public:
Stylo(int p=0):prix(p){}
void affiche();
void afficheLivre(Livre l);
private :
int prix;
};
#endif |
Stylo.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include<iostream>
#include "stylo.h"
using namespace std;
void Stylo::affiche(){
cout <<"prix: "<<this->prix<<endl ;
}
void Stylo::afficheLivre(Livre l){
cout <<l.nom <<endl;
} |
livre.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef LIVRE
#define LIVRE
#include "stylo.h"
class Livre{
public:
Livre(int p=0,std::string n="new livre");
void affiche();
friend void Stylo::afficheLivre(Livre l);
private :
std::string nom;
int prix;
};
#endif |
livre.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include<iostream>
#include "livre.h"
using namespace std;
Livre::Livre(int prix,string nom){
this->prix=prix;
this->nom=nom;
}
void Livre::affiche(){
cout << "nom: " + this->nom + " prix: "<<this->prix<<endl ;
} |
le problème c'est que lorsque j'inclus livre.h dans le fichier .h de la classe stylo (puisque j'utilise un objet LivreafficheLivre(Livre l)) et de la même façon
lorsque j'inclus stylo.h dans le fichier .h de la classe livre il m'affiche des erreurs :
D:\c++SMI6\fctAmieClass\stylo.h|10|error: `Livre' has not been declared|
D:\c++SMI6\fctAmieClass\stylo.h|10|error: ISO C++ forbids declaration of `l' with no type|
D:\c++SMI6\fctAmieClass\livre.h|11|error: prototype for `void Stylo::afficheLivre(Livre)' does not match any in class `Stylo'|
D:\c++SMI6\fctAmieClass\stylo.h|10|error: candidate is: void Stylo::afficheLivre(int)|
||=== Build finished: 4 errors, 0 warnings ===|
merci de m'aider