开发者

C++ friend function declaration between two seperate files

I've been instructed to create开发者_如何学C two Classes: Customer and Barber, Barber should have a function : cutHair() that can change the value of private member hairLength in Customer.

Customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "barber.h"
#include <iostream>
#include <string>
using namespace std;  

class Customer{
    public:
        friend void Barber::cutHair(Customer &someGuy);
        Customer(string name, double hairLength);
        string getName();
        double getHair();
        void setHair(double newHair);
    private:
        string name;
        double hairLength;
};
#endif

Barber.h

#ifndef BARBER_H
#define BARBER_H
#include <iostream>
#include <string>
#include "customer.h"
using namespace std;
class Customer;
class Barber{
    public:
        Barber(string barberName);
        void cutHair(Customer &someGuy);
    private:
        string name;
        double takings;
};
#endif

barber.cpp EDIT: I changed the implementation of cutHair() to take advantage of the friend declaration instead of accessing the private members of class Customer through it's public accessor methods

#include "barber.h"
#include <string>

Barber::Barber(string barberName){
        name = barberName;
        takings = 0;
}
void Barber::cutHair(Customer &someGuy){
        takings += 18;
        someGuy.hairLength = someGuy.hairLength * 80 / 100; 
}

customer.cpp

#include "customer.h"
#include <string>

Customer::Customer(string customerName, double custHairLength){
        name = customerName;
        hairLength = custHairLength;
}
double Customer::getHair(){
        return hairLength;
}
void Customer::setHair(double newLength){
        hairLength = newLength;
}

when attemping to build i get the error message

customer.h(10): error C2653: 'Barber' : is not a class or namespace name

been doing research and canceling out issues one after another for a few days now. hope someone can come to my rescue :)


You have cyclic dependency here. You include barber.h from customer.h and customer.h from barber.h.

In barber.h use

class Customer;

instead of

#include "customer.h"


In Barber.h you are forward declaring Customer (class Customer;) AND including #include Customer.h". Try removing the include. Of course you will have to add the include in your Barber.cpp


When you are doing a forward declaration such as class Customer in Barber.h, you are telling the compiler if you see the name Customer ignore it for now at declaration as long as you don't need to know the size of the object. However in your case when you are declaring the CutHair function you are using the actual Customer object in the argument. If you change the code in a way to have the pointer to Customer as the argument, and get rid of the inclusion dependencies, your code will compile.

I would also suggest that in your case you wouldn't need a friend function if you had a setHair function defined, unless you have a good reason not to have that function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜