dividing the program into smaller parts.

The cyferka has a value of 11. I wish she was 7 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <conio.h>
#include "skladyN.hpp"

using namespace std;

int main()
{
    int cyferka = 11;


    liczba();

    cout << cyferka << endl;


    getch();
    return( 0 );
}



1
2
3
4
5
6
7
8
9
10
#include <iostream>
#ifndef skladyN
#define skladyN



void liczba();

#endif



1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
#include "skladyN.hpp"


int cyferka;

void liczba()
{
    cyferka = 7;
}
Last edited on
When you have run this, remove the 'int' in the line
int cyferka = 11;
and the fairy will grant you your wish.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int cyferka;

void liczba()
{
    cyferka = 7;
}

int main()
{
    int cyferka = 11;          
    liczba();
    cout << cyferka << endl;
}
If you're using multiple transaction units (multiple .cpp files), then keep int cyferka; in one file, but either put it as extern int cyferka; in the other .cpp file.
Yeeah :)


extern is a big player ;) Now I have 7 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include "skladyN.hpp"

using namespace std;

int main()
{
    extern int cyferka;


    liczba();

    cout << cyferka << endl;


    getch();
    return( 0 );
}
Last edited on
I didn't say delete all of "int cyferka" - I said change the version IN MAIN to just cyferka. [EDIT: OP has changed their post; makes this comment largely redundant]

Your error is that you had too many different variables all called "cyferka". (Just like Polish seems to have an awful lot of words translating to "number").

Let me do it for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int cyferka;

void liczba()
{
    cyferka = 7;
}

int main()
{
    cyferka = 11;   // NOT     int cyferka = 11
    liczba();
    cout << cyferka << endl;
}
Last edited on
I have a program with structures. There are a lot of assignments in the main function. I want to define these structures and throw the value assignments to the second file. But in the main function, I just want to load these values with the new function. This will simplify the main function, which is big now because of the array assignments of structures.

This first example was simple because I wanted to understand how to split files into several. However, in your example, I would have to make a few hundred lines of code before the main function to set values to variables in functions where the liczba function defines. I just want to throw the definition of this function out.

I write something like this in main funstion, but something is not working here :

extern struct Player zwodnik[21];

extern struct Team druzyna[8];
Last edited on
Why do you need global variables in the first place? Just pass what you need to functions.

"something is not working here" provides no information to us.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// .cpp - main

#include <iostream>
#include <conio.h>
#include "skladyN.hpp"

using namespace std;

int main()
{
    extern Team druzyna;

    void ustawPola();


 cout<<druzyna.x<<endl;
 cout<<druzyna.y<<endl;


    getch();
    return( 0 );
}


1
2
3
4
5
6
7
8
9
10
11
12
// .cpp 2

#include "skladyN.hpp"


Team druzyna;

void ustawPola()
{
    druzyna.x = 5;
    druzyna.y = 10;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// .hpp 

#ifndef skladyN
#define skladyN


struct Team
{
 int x;
 int y;
};

void ustawPola();

#endif 


console effect:
0
0

You never run the version of ustawPola() that you have in your second .cpp file ... because you declared another local version of it within main().

In int main():
Change
void ustawPola();
to just
ustawPola();

But I concur with @Ganado: use the argument list of procedures to pass variables if possible, rather than global variables. If you have a lot of them you can always bundle them up in a struct.
I know nothing about argument list of procedures.

I'am close to understanding it but I am getting a message:
"error: expected unqualified-id before ')' token"

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
27
28
29
// main .cpp

#include <iostream>
#include <string.h>
#include <windows.h>
#include <ctime>
#include <fstream>
#include "sklady.hpp"



using namespace std;

int main()
{

    extern Player zawodnik[21];
    extern Team druzyna[8];

    sklady();


    cout<<druzyna[3].zawodnik[17].name<<endl;



    getchar();
    return 0;
}


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
27
28
29
30
31
32
33
34
35
// .hpp

#ifndef sklady
#define sklady

using namespace std;


    struct Player
    {
    string name;
    char formation;
    short int number;
    ...
    ...
    ...

    };

    struct Team
    {
    string name;
    struct Player zawodnik[21];
    short int teamPld;
    ...
    ...
    ...

    };


    void sklady();


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// .cpp 2 file

#include "sklady.hpp"



    Player zawodnik[21];
    Team druzyna[8];



    void sklady()
    {
    druzyna[0].name = "Manchester"; 
    druzyna[0].reputation = 11;
    druzyna[0].physio = 2;

    druzyna[0].zawodnik[0].name = "F. Barthez";
    druzyna[0].zawodnik[0].formation = 'g';
   ...
   ...
   ...
   ...
   }
Last edited on
Your compiler should tell in which file and (near) which line that error occurred. We need to see your actual code - at least in that vicinity (NOT just that line).

Your error message is one which often arises as a "knock-on" effect on earlier lines - so we need to see the line ascribed the error message and a few before it.

I presume that my last post solved your previous problem. You are now introducing new ones.

Yes, he solved the problem. now i have a new 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
27
28
29
// main .cpp

#include <iostream>
#include <string.h>
#include <windows.h>
#include <ctime>
#include <fstream>
#include "sklady.hpp"



using namespace std;

int main()
{

    extern Player zawodnik[2];
    extern Team druzyna[2];

    sklady();


    cout<<druzyna[1].zawodnik[0].name<<endl;



    getchar();
    return 0;
}


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
27
28
// .hpp

#ifndef sklady
#define sklady

using namespace std;


    struct Player
    {
    string name;
    char formation;
    short int number;
    bool morale;
    };

    struct Team
    {
    string name;
    struct Player zawodnik[2];
    short int teamPld;
    };


    void sklady();


#endif 


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
27
28
29
30
31
32
33
34
35
36
// .cpp 2

#include "sklady.hpp"


    Player zawodnik[2];
    Team druzyna[2];


    void sklady()
    {
    druzyna[0].name = "Manchester";

    druzyna[0].zawodnik[0].name = "F. Barthez";
    druzyna[0].zawodnik[0].formation = 'g';
    druzyna[0].zawodnik[0].number = 1;
    druzyna[0].zawodnik[0].morale = true;

    druzyna[0].zawodnik[1].name = "G. Neville";
    druzyna[0].zawodnik[1].formation = 'd';
    druzyna[0].zawodnik[1].number = 2;
    druzyna[0].zawodnik[1].morale = true;


    druzyna[1].name = "Arsenal";

    druzyna[1].zawodnik[0].name = "R. Wright";
    druzyna[1].zawodnik[0].formation = 'g';
    druzyna[1].zawodnik[0].number = 1;
    druzyna[1].zawodnik[0].morale = true;

    druzyna[1].zawodnik[1].name = "O. Luzhny";
    druzyna[1].zawodnik[1].formation = 'd';
    druzyna[1].zawodnik[1].number = 2;
    druzyna[1].zawodnik[1].morale = true;
    }


...|23|error: expected unqualified-id before ')' token|
...||In function 'int main()':|
...|18|error: expected primary-expression before ')' token|
...|warning: unused variable 'zawodnik' [-Wunused-variable]|

I am not sure if there should be a variable in the Team structure zawodnik[2]; in the file hpp since it is declared in the second file cpp. As is the case with the structure Team.

The problem here is clearly that one structure is contained within the other and it doesn't suit him.
Last edited on
Topic archived. No new replies allowed.