Problem with struct in function

hello guys,
i need some help with my code

in main i have:

int main(){
.
.
struct{
int id;
string nazov;
int rok;
string zaner;
string povod;
int trvanie;
string herci;
}filmy[17];

naplnStrukturu(filmy[],i,line);
.
.
}

function is:

void naplnStrukturu(struct filmy[],int i,string line){
filmy[i].id=idFilmu(line);
}

error : expected unqualified-id befor '[' token

struct outside of main dousn't help
any ideas?
thanks
If you want your structure to be visible outside of main() you'll need to declare it in the global scope. But be sure you leave the variable declaration inside main(). And don't forget to name that structure.

Also when posting code please use code tags.
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
37
38
#include <iostream>
#include <string>

using namespace std;

struct thing {
    int id;
    string nazov;
    int rok;
    string zaner;
    string povod;
    int trvanie;
    string herci;
};

void naplnStrukturu(thing filmy[], int i, string line);

int main()
{
    thing filmy[17];

    int i = 0;
    string line;
    
    naplnStrukturu(filmy, i, line);

    return 0;
}

int idFilmu(string line)
{
    return 0;
}

void naplnStrukturu(thing filmy[], int i, string line)
{
    filmy[i].id = idFilmu(line);
}
i did what you said and it still don't work
functin:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void naplnDatabazu(struct actDatabaza herci[], int pocetRiadkovAct,const char *Vstup){
ifstream vstup(Vstup);
string line;
i=0;
while(getline(vstup, line)&&i<pocetRiadkovAct)
{
    herci[i].id = idHerca(line);
    herci[i].meno = menoHerca(line);
    herci[i].priezvisko = priezviskoHerca(line);
    herci[i].narodenie = narodenieHerca(line);
    herci[i].miestoNar = miestoNarHerca(line);
    i++;
}
}

int idHerca(string line){
    string id = line.substr(0, 2);
    const char *znak = id.c_str();
    int result;
    sscanf ( znak, "%d", &result );
    return result;
}


i got this in header file:
void naplnDatabazu(struct actDatabaza herci[], int pocetRiadkovAct,const char *inputFile);

outside main:
like you said:
1
2
3
4
5
6
7
 struct actDatabaza{
        int id;
        string meno;
        string priezvisko;
        string narodenie;
        string miestoNar;
};


in main :
1
2
actDatabaza herci[rowInActors];
naplnDatabazu(herci, rowInActors, nazovActVstup.c_str());



i get errors:
invalid use of incomplete type 'struct actDatabaza'
forward declaration of 'struct actDatabaza'

Last edited on
It's difficult to give a complete answer from only part of the code. However, this message:
'actDatabaza' was not declared in this scope

presumably comes from your header file:
void naplnDatabazu(actDatabaza herci[], int pocetRiadkovAct,const char *inputFile);

The answer is to move the struct into the header file.
1
2
3
4
5
6
7
8
9
10
11
12
// ---- header file ----
#include <string>

struct actDatabaza {
    int id;
    std::string meno;
    std::string priezvisko;
    std::string narodenie;
    std::string miestoNar;
};

void naplnDatabazu(actDatabaza herci[], int pocetRiadkovAct,const char *inputFile);


By the way, it is not good to put "using namespace std;" inside header files, so instead I used the qualifier std:: where needed.
it works thanks
actually what is your program?i headache reading your program ==
i need read data from imput file and put them in the struct. i did it in main but i decide that it will be nicer to do it in function and there is the problem... but no more i already did it and it works fine.
sorry for my english i'm just begginer.
Topic archived. No new replies allowed.