0

I have an array full with objects of a class I created. I want to access members of each of them but I don't know how.



#include <iostream>

using namespace std;

class TRAIN{
    int Train_Number;
    string Train_Name;
    int Arrival_Hr;
    int Arrival_Min;
    TRAIN(int Hr1,int Mn1, int Hr2, int Mn2)
    {
        Arrival_Hr = Hr2-Hr1;
        Arrival_Min = Mn2-Mn1;
        cout<<Arrival_Hr<<"Hr"<<Arrival_Min<<"min is the difference";
    }
};
int main(){
    TRAIN *ptr[10];
    ptr[0] = new TRAIN(2,30,4,40);
    ptr[1] = new TRAIN(1,20,5,30);
    ptr[0].Train_Number = 100;
    ptr[0].Train_Name = "Jansadabti";
    cout<<ptr[0].Train_Number;
    cout<<ptr[0].Train_Name;
    return 0;
}
    

This is the error message I am getting:

main.cpp: In function ‘int main()’:
main.cpp:27:33: error: ‘TRAIN::TRAIN(int, int, int, int)’ is private within this context
     ptr[0] = new TRAIN(2,30,4,40);
                                 ^
main.cpp:18:5: note: declared private here
     TRAIN(int Hr1,int Mn1, int Hr2, int Mn2)
     ^~~~~
main.cpp:28:33: error: ‘TRAIN::TRAIN(int, int, int, int)’ is private within this context
     ptr[1] = new TRAIN(1,20,5,30);
                                 ^
main.cpp:18:5: note: declared private here
     TRAIN(int Hr1,int Mn1, int Hr2, int Mn2)
     ^~~~~
main.cpp:29:12: error: request for member ‘Train_Number’ in ‘ptr[0]’, which is of pointer type ‘TRAIN*’ (maybe you meant to use ‘->’ ?)
     ptr[0].Train_Number = 100;
            ^~~~~~~~~~~~
main.cpp:30:12: error: request for member ‘Train_Name’ in ‘ptr[0]’, which is of pointer type ‘TRAIN*’ (maybe you meant to use ‘->’ ?)
     ptr[0].Train_Name = "Jansadabti";
            ^~~~~~~~~~
main.cpp:31:18: error: request for member ‘Train_Number’ in ‘ptr[0]’, which is of pointer type ‘TRAIN*’ (maybe you meant to use ‘->’ ?)
     cout<<ptr[0].Train_Number;
                  ^~~~~~~~~~~~
main.cpp:32:18: error: request for member ‘Train_Name’ in ‘ptr[0]’, which is of pointer type ‘TRAIN*’ (maybe you meant to use ‘->’ ?)
     cout<<ptr[0].Train_Name;
                  ^~~~~~~~~~
Quimby
  • 17,735
  • 4
  • 35
  • 55
Cosmos396
  • 13
  • 3
  • The message says it quite clearly: `maybe you meant to use ‘->’ `. So, use `ptr[0]->Train_Number` instead of the dot. Also do you have a particular reason for not writing just `TRAIN trains[10];`? – Quimby Jan 09 '21 at 14:40
  • As the compiler message says. For pointers you need to do `ptr[0]->Train_Number = 100;` etc.. – Hitobat Jan 09 '21 at 14:41
  • 1
    All the class members are private: in the main you're trying to call them, but you actually can't. – Matteo Galletta Jan 09 '21 at 14:43
  • @Quimby Yes , this is a question for my exam, it says " use pointer to array of objects for different trains" – Cosmos396 Jan 09 '21 at 14:59
  • @MidhunPaul Okay, that's fine, would not expect badly written code at an exam. – Quimby Jan 09 '21 at 15:33
  • replace `class` with `struct` or add `public:` just after the class opening brace. Still, you defined an array of pointers, not a pointer to an array. Moreover, in exams etc. if they write "different trains", they often mean inheritance and virtual functions. – zkoza Jan 09 '21 at 15:51
  • @zkoza , So how do i define a pointer to array of objects? The question is " Write a program to implement pointer to object in a class TRAIN. Train_Number , Train_Name, Arrival_Hr ,Arrival_Min, TimeDiff() are members in class. Member function TimeDiff() is used to find the time difference between Arrival Time (Arrival_Hr and Arrival_Min) and Reached Time ( New given Hr and Min for late arrival) and display the calculated time difference of the specified train. Use pointer to array of objects for different trains. – Cosmos396 Jan 10 '21 at 07:27
  • https://stackoverflow.com/questions/859634/c-pointer-to-array-array-of-pointers-disambiguation – zkoza Jan 10 '21 at 12:27

1 Answers1

1
#include <iostream>

using namespace std;

class TRAIN{
    public: int Train_Number;
    public: string Train_Name;
    int Arrival_Hr;
    int Arrival_Min;
    TRAIN(int Hr1,int Mn1, int Hr2, int Mn2) {
        Arrival_Hr = Hr2-Hr1;
        Arrival_Min = Mn2-Mn1;
        cout<<Arrival_Hr<<"Hr"<<Arrival_Min<<"min is the difference";
    }
};
int main(){
    TRAIN* ptr[10];
    ptr[0] = new TRAIN(2,30,4,40);
    ptr[1] = new TRAIN(1,20,5,30);
    ptr[0]->Train_Number = 100;
    ptr[0]->Train_Name = "Jansadabti";
    cout<<ptr[0]->Train_Number;
    cout<<ptr[0]->Train_Name;
    return 0;
}

you need to make public variables if you need to access them from outside. and also you need -> instead of . when you use pointers.