help with a class error

i am writing a program to reorder events in the correct based on a date that the user provides, i am getting an error in buildtime.


Main.cpp||In function 'int main()':|
Main.cpp|9|error: aggregate 'Stuff event1' has incomplete type and cannot be defined|
Main.cpp|10|error: aggregate 'Stuff event2' has incomplete type and cannot be defined|
Main.cpp|9|warning: unused variable 'event1' [-Wunused-variable]|
Main.cpp|10|warning: unused variable 'event2' [-Wunused-variable]|
||=== Build finished: 2 errors, 2 warnings ===|

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

void initialize20Events();
class Stuff;

int main()
{
    initialize20Events();
    Stuff event1;
    Stuff event2;
    return 0;
}

void initialize20Events()
{


};

class Stuff
{
    public:
    int date;
    char eventName1[50];
    void initializeEvents(int);
};
Define the class a bit more.

1
2
3
4
5
6
class Stuff
{
public:
   Stuff() {} // constructor
   ~Stuff() {} // destructor
};


This skeleton should get rid of the error.
did that but am still getting the same error

Forward declaration of a class allows you to refer to the class type, but not actually use it (or any of its members). You could have a pointer to one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

void initialize20Events();
class Stuff;

int main()
{
    initialize20Events();
    Stuff* event1;
    Stuff* event2;
    return 0;
}

void initialize20Events()
{


};

class Stuff
{
    public:
    int date;
    char eventName1[50];
    void initializeEvents(int);
};


or you could move the definition ahead of the usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void initialize20Events();

class Stuff
{
    public:
    int date;
    char eventName1[50];
    void initializeEvents(int);
};

int main()
{
    initialize20Events();
    Stuff event1;
    Stuff event2;
    return 0;
}

void initialize20Events()
{


};

thx for the help, i didnt think it mattered the order
All the compiler can leave until later is function calls, which get filled in by the linker as necessary. It needs to know everything else about an object when it has to use it, and compilers read the code once only from top to bottom (or, if they do multi-pass, they still act as if they only read it once).
Topic archived. No new replies allowed.