0

I really can't find anything related to my problem on the internet (probably because i can't explain the problem), so if someone can help me I would seriously appriciate it. I'm just beginning to learn C++, so forgive my crude title - The problem is as follows:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Plaza {
public:

    int duljina;
    double X;
    double Y;
    Predmet *niz[50];

};

class Predmet {
public:

    string opis;
    int kolicina;

};

int main() {

    int i, N, M, x;

    cout << "Unesite broj plaža (N):";
    cin >> N;

    Plaza* P = new Plaza[N];
    

    for (i = 1; i <= N; i++) {
        cout << "Unesite velicinu i lokaciju za" << i << ". plazu:";
        cout << endl;
        cin >> P[i].duljina;
        cin >> P[i].X;
        cin >> P[i].Y;
    };

    cout << "Unesite broj predmeta (M): ";
    cin >> M;


    for (i = 1; i <= M; i++) {
        cout << "Unesite redni broj plaze kojoj pripada" << i << ". predmet: ";
        cin >> x;
        cout << "Unesite kolicinu i opis predmeta: ";
        cin >> P[0].niz[0].opis;
        .......
    }
    

}

Plaza is the main class and the Predmet is the child class. I have an array of classes Plaza which in itself has an array of classes Predmet.

My question is: how the hell do I get to the variables in the Predmet class which is in a list which is in a class that is in an array?

Plaza* temp = new Plaza[X];

temp[0].niz[0].opis; // "Expression must have class type error"

I kid you not, I'm stuck on this problem for 5 hours now... Please help

Thx

Sadlyfe
  • 3
  • 2
  • Terminology note: a subclass is a class that derives from another class. That's not what you are doing here. `Predmet ` is just another class that `Plaza ` *contains*. – user4581301 May 20 '21 at 00:41
  • 1
    Recommend describing what goes wrong. If you get a compiler error or warning, add the full text of it to the question as text (as in don't take a nd post a picture of it). – user4581301 May 20 '21 at 00:43
  • Ill edit it out. Thanks! – Sadlyfe May 20 '21 at 00:44
  • Only thing I see that could possibly be wrong in the above code is nothing ever points `temp[0].niz` at a valid `Predmet` (or array of `Predmet`s). Without at least one valid object to point at, `niz[0]` cannot be safely used. Consider manufacturing a [mre] so we can all see exactly what you do. Often simply making the MRE shakes loose the problem and the solution. – user4581301 May 20 '21 at 00:48
  • Seems fine to me, but I had to invent the code you left out, so your issue may be different. https://ideone.com/1Ezi1K – Retired Ninja May 20 '21 at 00:49
  • I put my whole code as requested – Sadlyfe May 20 '21 at 00:51
  • 1
    That edit changed the declaration of `niz`, so now we can see the problem. `niz[0]` is a pointer to an object, not an object or reference to one. Use `niz[0]->opis`. – 1201ProgramAlarm May 20 '21 at 00:53
  • 1
    As surmised, nothing ever makes any `Predmets`s to point at. If `Plaza` knows how many it will need, it should allocate them in it's constructor. Mind you, if it knows you may be able to use a regular array instead of a pointer. Another good alternative is `std::vector` because it will do all of the heavy lifting for memory management and correct copying. – user4581301 May 20 '21 at 00:54
  • @1201ProgramAlarm Thats it! Thanks a lot! – Sadlyfe May 20 '21 at 00:56
  • Side note: `Predmet` needs to be declared before you can hold a reference to it and it must be defined before you can fully use it. I recommend moving its definition above the definition of `Plaza ` – user4581301 May 20 '21 at 00:56
  • Here's some reading to help you write simpler code, especially if you have to use raw pointers to dynamic allocations: [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [The Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three) – user4581301 May 20 '21 at 01:01
  • I'd upvote everyone's answers and choose an answer, but I don't have voter privledge... – Sadlyfe May 20 '21 at 01:01
  • Don't worry about it. These are all comments. They're worth zero Internet points and could be deleted at any time. An answer will show up below, and if one does and it answers your question, you can mark it as The Correct Answer with a big green checkmark. – user4581301 May 20 '21 at 01:03

0 Answers0