First time using structs

Pages: 12
This is the code


vul_struct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

void vul_struct(TEAM& temp, int x)
{

    cout<<"Naam ploeg "<<x+1<<": ";
    getline (cin, temp.naam);

}


vul_struct.h
1
2
3
4
5
6
#ifndef VUL_STRUCT_H_INCLUDED
#define VUL_STRUCT_H_INCLUDED

void vul_struct (TEAM& temp, int x);

#endif // VUL_STRUCT_H_INCLUDED 


And this is the error:
1
2
3
4
5
6
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Papa Poulefase\vul_struct.cpp|8|error: variable or field 'vul_struct' declared void|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Papa Poulefase\vul_struct.cpp|8|error: 'TEAM' was not declared in this scope|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Papa Poulefase\vul_struct.cpp|8|error: 'temp' was not declared in this scope|
C:\Documents and Settings\XAN DIET VINIE\My Documents\XANDER\Programmeren\Papa Poulefase\vul_struct.cpp|8|error: expected primary-expression before 'int'|
||=== Build finished: 4 errors, 0 warnings ===|


Anyone sees why?

thanks!
Last edited on
Where do you declare TEAM? The compiler has no idea what it is.
I declare it in main...

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>
#include <vector>
#include <string>

#include "vul_struct.h"

using namespace std;

struct TEAM{string naam; int score;};

vector<TEAM> teams;

TEAM temp;

int x = 0;

int main()
{

    for(; x != 4; ++x)
    {
        vul_struct(temp, x);
        teams.push_back(temp);
    }

}
Unfortunately that doesn't help the compiler when it is compiling vul_struct.cpp. You need to put that declaration of TEAM into its own header file and include it in vul_struct.cpp and in main.cpp
Wow...what?
I ahve to make a header file to declarde TEAM?

EDIT: I got it working. But why do I only have to do that with structures?
Last edited on
The rule is that the compiler has to see the declaration of the struct before it compiles something that uses it. That means that if several different .cpp files use the struct then they each need to #include its declaration so the compiler knows what's going on.
It's NOT only with structures. It's because you defined what TEAM was after the compiler had seen it used in the other files. Declaring it beforehand would fix it.
hmm ok thanks guys!
but why don't i have to do that with ints and doubles etc?
Because ints and doubles are already defined in the entire program, they are fundamental C++ data types.
Last edited on
Ok, and structures aren't then...
Now I come to think of it, if i use it in one .cpp file, I wouldn't need to make a separate header for it?
And, why would you take several .cpps ? Just a question, what is the good thing about several cpp?
Classes, structures, and unions are all fundamental data structures. BUT, the data types you declare with them only exist AFTER you declare them, and not everywhere. This is the same for variables and just about everything.
Last edited on
but why don't I need to include int x; tehn?
Because you already defined it...
I defined it after TEAM...
Yes, so?
So I didn't define it before vul_struct read it?
I don't see the differnce with TEAM...
vul_struct has x defined in the parameter list. It is a completely different x than the one declared in main.
You define int x before you use it. If you tried to use your int x in vul_struct.cpp then you would have to declare it just like you have to declare TEAM.

You would do it like this:
 
extern int x;
But I do use it...?
vul_struct has x defined in the parameter list. It is a completely different x than the one declared in main.

Do you not understand what I said? If not I can explain.
Last edited on
Pages: 12