Even if list<D> and FindD are A's protected members, B can't call FindD on C's object.
Printable View
Even if list<D> and FindD are A's protected members, B can't call FindD on C's object.
Consider this:
Now class B may call protected function from class A.Code:...
class A
{
friend class B;
...
But actually, there is too much useless code you provide us, that's why we are just guessing what you expect to be an answer to your question.
This is the code. i tried to keep it simple as I can. The error appear in "add_line_to_bus_station" function
Code:#include <iostream>
#include <string>
#include <list>
using namespace std;
class Bus_Line;
class Address
{
public:
Address(const string street,int HouseNum): m_street(street),m_HouseNum(HouseNum){}
int get_house_num(){return m_HouseNum;}
string get_street(){return m_street;}
friend bool operator== (const Address& a1,const Address& a2);
private:
string m_street;
int m_HouseNum;
};
class Bus_Station
{
public :
Bus_Station(const Address& address): m_address(address){}
void add_line(int bus_sine);
const Address& get_address()
{
return m_address;
}
private:
list<int> m_lines;
Address m_address;
};
class Container
{
public:
Container(string name): m_name(name){}
//void add_street(string str);
//bool add_address(string street_name,int num);
//bool add_bus_station(const Address& add);
protected:
string m_name;
list<Bus_Station> m_bus_stations;
list<Address> m_addresses;
Bus_Station* find_bus_station(const Address& add);
};
class City : public Container
{
public:
City(string name): Container(name) {}
void add_line_to_bus_station(Container*c,const Address& add,int line_number); //this function uses "find_bus_station "
private:
list<Bus_Line> m_lines;
};
class Country : public Container
{
public:
Country(string name);
//City& add_city(string city_name);
//City* find_city(string city_name);
private:
list<City> m_cities;
};
class Bus_Line
{
public :
Bus_Line(int line_code);
int GetLineCode() {return m_line_code;}
private:
int m_line_code;
};
inline Bus_Station* Container::find_bus_station(const Address& add)
{
if(m_bus_stations.empty()) {return 0;}
list<Bus_Station>::iterator it = m_bus_stations.begin();
while (it != m_bus_stations.end())
{
const Address& temp =(*it).get_address();
if( temp == add)
{
return &(*it);
}
it++;
}
return 0;
}
void City::add_line_to_bus_station(Container*c,const Address& add,int line_number)
{
Bus_Station* A= (*c).find_bus_station(add);
if(A==0){return;}
(*A).add_line(line_number);
}
void Bus_Station::add_line(int Bus_Line)
{
m_lines.push_back(Bus_Line);
}
bool operator== (const Address& a1,const Address& a2)
{
if( a1.m_street == a2.m_street && (a1.m_HouseNum == a2.m_HouseNum))
{return true;}
return false;
}
Any answer? you asked me to post my code
I think you need to take a big step backwards and try to learn more about inheritance. You have a class Container. In order for City to derive properly from Container, a City would have to be a Container, and that doesn't seem likely.
Also, since in your hierarchy a City is a Container, why are you passing a Container pointer to a City function? None of what you're doing makes a whole lot of sense conceptually.
City should be a Container. I know that the name doesn't explain the meaning, but a container is an object which contains Street(s), Address(es) and Bus_Station(s). It should be mentioned that some of the bus stations are located out of town.
The paramater of "add_line_to_bus_station" is of type Container because it can be either City or Country.
I'm still unclear why you're passing a pointer to a Container to City::add_line_to_bus_station. Since city is a Container, why not operate on the City object directly? You won't run into the protected issue if you do, and at least on the surface it seems to make more sense.
Because Country is also a Container, so the parameter might also be a Country
Some suggestions were already given but you just couldn't notice them because
A) your mind is bent on thinking that you're right, B) you're too busy justifying the questionable logic you asked us to review, C) and at the same time trying to find fault with the possible solutions given by the members of this forum.
Sometimes thinking things backwards helps.
What I mean by this is that you set aside your efforts to understand the conceptual term polymorphism for now. Instead, try to write a virtual function used in an inheritance hierarchy. I think with this approach, you can focus your synergy on more syntactical side of the language, and solidify your understanding towards the semantic side from it.
So the next sensible thing for me to do is to ask you if you know how to write a virtual function using Foo as a name of the base class and Bar as a name of the derived class with a virtual function named Print that takes no parameter.
Can you show, in code, that you could do this?
I think you're right, thanks. However, the most beneficial solution I heard so far is to make Container a friend of City, but I prefer a better solution because as far as I know (and tell if it doesn't true), I should avoid using this way.
Regarding to this, you're right, and the last code I've postet doesn't use polymorphism - no virtual function and no Print function without parameters.
I don't know exactly what you're trying to do but I don't see City and Country being derived from Container. It seems you only want one Container object that contains the list of bus stations and addresses. By deriving from it, every City and Country object also contains the copies of the lists, which I can't see that you want.
No, City and also Country have to hold lists of adresses and bus stations. Concerning cities, it's clear why they should hold those lists. But some of the bus stations are located out of town, so it is necessary to manage a list of all those bus stations.
However, only city should hold a list of bus lines.
p.s. GCDEF, I'm appreciate your patience for my troubles :)