-2

I have problem with my program. I was looking for answer, but i did't find solution. I created struct and class:

struct Data
{
    Client* k;
    string msg;
};
class Client 
{
public:
    string name;
    int _id;
}

Then i created table of Data.

Data tab[100];

And when i try get name from some fields in the table, like this:

tab[i].k->name;

I get errors:

C2227 left of "->name" must point to class/struct/union/generic type
C2027 use of undefined type 'Client'
Kamil L
  • 1
  • 1
  • Recommended read: https://stackoverflow.com/questions/46991224/are-there-any-valid-use-cases-to-use-new-and-delete-raw-pointers-or-c-style-arr. – user0042 Dec 09 '17 at 18:13
  • Well, the error is quite self-explanatory: `use of undefined type 'Client'`. Which means first define type `Client`, then type `Dane`. Or use a forward declaration since you're dealing with a `Client*` – Jack Dec 09 '17 at 18:14
  • I agree, but i defined type Client, before type Data. And i still have this error. – Kamil L Dec 09 '17 at 18:18

1 Answers1

1

You got a ';' missing after the Client class declaration.

Either way, I slightly modified your code (Client before Data, add missing ';') and got it compiling and running fine:

#include <iostream>

using namespace std;

class Client
{
public:
    string name;
    int _id;
};
struct Data
{
    Client* k;
    string msg;
};

int main()
{
    Data tab[100];

    for (int i = 0; i < 100; i++)
    {
        tab[i].k = new Client();
        tab[i].k->name = "name" + to_string(i);
        cout << tab[i].k->name << endl;
    }
}
J. In
  • 415
  • 5
  • 10