writing operator

the question is:modify the writing operator so it can read information about the route

you can use getteri and or friend functions

the display will be: departure

destination

traincode

the operator can read a string with spaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class TrainRoute
{
private:
   string departure;
   string destination;
   char* trainCode;

public:
   TrainRoute(string departure, string destination, const char* trainCode)
   {
       this->departure = departure;
       this->destination = destination;
       this->trainCode = new char[strlen(trainCode) + 1];
       strcpy_s(this->trainCode, strlen(trainCode) + 1, trainCode);
   }

ostream& operator<<(ostream& o, TrainRoute t)
{
   return o;
}


this is what I did but it doens't work

1
2
3
4
5
6
7
8
ostream& operator<<(ostream& o, TrainRoute t)
{
  

   o <<t.departure << "\n" << t.destination << "\n" << t.trainCode;

   return o;
}
Last edited on
You have to explain/show how exactly "it does not work".
I get the following error: C++ member (declared at line 10) is inaccessible

line ten of my code is
private:
10 string departure;
11 string destination;
12 char* trainCode;
Last edited on
So it seems you have the same situation like here:

http://www.cplusplus.com/forum/beginner/274830/#msg1186118

Again not reproduceable...

It appears that you have two functions:

A member function that does nothing
A free function that tries to access private members of a unrelated class.

To solve it you should write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TrainRoute
{
private:
   string departure;
   string destination;
   char* trainCode;

public:
   TrainRoute(string departure, string destination, const char* trainCode)
   {
       this->departure = departure;
       this->destination = destination;
       this->trainCode = new char[strlen(trainCode) + 1];
       strcpy_s(this->trainCode, strlen(trainCode) + 1, trainCode);
   }

friend ostream& operator<<(ostream& o, TrainRoute t);


I would further suggest that you (before you without any comment mark this thread as solved) make sure that you understand the solution...
@laura11111 you either haven't read or not understood my previous posts.

In class TrainRoute:

 
friend ostream& operator<<(ostream& o, const TrainRoute& t);


and then define the free function:

1
2
3
4
ostream& operator<<(ostream& o, const TrainRoute& t)
{
    // Put the code here to display TrainRoute class data
}


Also, although you can, it's not good practice to have a class function with parameter names the same as class member variables.

1
2
3
4
5
TrainRoute(const string& dep, const string& dest, const char* code) : departure(dep), destination(dest)
{
	trainCode = new char[strlen(code) + 1];
	strcpy_s(trainCode, strlen(code) + 1, code);
}


Last edited on
Furthermore, there are two ways to implement a member function:
1. Inside the class definition
1
2
3
4
5
6
7
8
class Example { // definition of Example starts here

  void member()
  {
    // implementation of function 'member'
  }

}; // definition of Example ends here 

2. Outside of class definition
1
2
3
4
5
6
7
8
9
10
class Example { // definition of Example starts here

  void member(); // declaration of function

}; // definition of Example ends here

void Example::member()
{
  // implementation of function 'member'
}


The incomplete fragments of code that you have posted do not show whether you even attempt to do the right thing.


you can use getteri and or friend functions

error: C++ member is inaccessible

The members that you try to access are private to the class. Only members of class can access them.
A "getteri" is a member function.
The operator<< cannot be a member function. It is a stand-alone function. It can call public getteri member functions of the class.

You must have been explained what a "friend function" is. A stand-alone function that the class does declare within its definition to be a friend. Friend has access to private members.

1
2
3
4
5
6
7
8
9
10
class Example { // definition of Example starts here

  friend void nonmember(); // declaration of friend

}; // definition of Example ends here

void nonmember()
{
  // implementation of function 'nonmember'
}

thank you all
I rewrite the code as per your advices and now it works
Topic archived. No new replies allowed.