Vector

Hi, how to use public data of type vector out of class?
This code shows creating and using a vector, outside a class.

1
2
3
4
5
6
int main()
{
  vector<int> numbers;
  numbers.push_back(3);
  cout << numbers[0];
}


Is that what you want?
I declared data vector type in one class and it is public, but when i use it in another class, compiler returns message that it is not declared in this scope.
How can we help without seeing code? Show us the code that reproduces the error message.
Last edited on
insideclass.vectorname[itemindex]
where insideclass is the instance of class with vector inside the other class.

if you inherited it, then just vectorname[index] is good enough.
Last edited on
Did you create an instance of the class containing the vector?


1
2
3
4
5
6
7
8
9
10
11
12
class vectorInside
{
  public:
    vector<int> the_vector;
};

int main()
{
  vectorInside anObject;
  anObject.the_vector.push_back(3);
  cout << anObject.the_vector[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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
using namespace std;
class Osoba{
protected:
    char* ime;
    char* prezime;
    int godina_rodjenja;
public:
    Osoba(char* niska){
        char f[]=" ";
    ime=strtok(niska,f);
    prezime=strtok(NULL,f);
    godina_rodjenja=atoi(strtok(NULL,f));
    }
    char* vrati_ime(){
    return ime;
    }
    char* vrati_prezime(){
    return prezime;
    }
    int vrati_godinu_rodjenja(){
    return godina_rodjenja;
    }
};
class Glumac:public Osoba{
private:
    int broj_uloga;
public:
    Glumac(char* niska):Osoba(niska){
    broj_uloga=0;
    }
    void uvecaj_broj_uloga(){
    broj_uloga++;
    }
};
class Reziser:public Osoba{
private:
    int broj_rezija;
public:
    Reziser(char* niska):Osoba(niska){
    broj_rezija=0;
    }
     void uvecaj_broj_rezija(){
    broj_rezija++;
    }
};
class Film{
public:
    char* naziv;
    char* zanr;
    int godina_snimanja;
    int vrijeme_trajanja;
    Reziser* reziser;
    vector<Glumac>glumaci_filma;
    Film(char* naziv_,char* zanr_,int godina_snimanja_=0,int vrijeme_trajanja_=0){
    naziv=naziv_;
    zanr=zanr_;
    godina_snimanja=godina_snimanja_;
    vrijeme_trajanja=vrijeme_trajanja_;
    }
};
class Kolekcija{
public:
    vector<Glumac>glumci;
    vector<Reziser>reziseri;
    vector<Film>filmovi;
    void novi_film(char* naziv_,char* zanr_,int godina_snimanja_=0,int vrijeme_trajanja_=0){
    Film f(naziv_,zanr_,godina_snimanja_,vrijeme_trajanja_);
    filmovi.push_back(f);
    }
    void novi_glumac(char* niska){
    Glumac g(niska);
    glumci.push_back(g);
    }
    void novi_reziser(char* niska){
    Reziser r(niska);
    reziseri.push_back(r);
    }
};
void dodaj_glumca(char* niska){
    int brojac=0;
Glumac g(niska);
for(int i=0;i<glumci.size();i++){
    if(!(g.vrati_ime()==glumci[i].vrati_ime() && g.vrati_prezime()==glumci[i].vrati_prezime()))
    brojac++;
}
if(glumci.size()==0)
    glumci.push_back(g);
else if(brojac==glumci.size())
    glumci.push_back(g);
else(brojac!=glumci.size())
    glumci_filma.push_back(g);
}
int main(){
    string s1="Met Dejmon 1970";
    char*c1=new char[s1.length()+1];
    strcpy(c1,s1.c_str());
    Glumac g1(c1);
    cout<<g1.vrati_ime()<<endl
    <<g1.vrati_prezime()<<endl
    <<g1.vrati_godinu_rodjenja()<<endl;
return 0;
}


Line 86 and line 90: error: 'glumci' was not declared in this scope.
Last edited on
You do realize that dodaj_glumca() is not part of any class and glumci was declared in some class, right?

well, it was not.
glumci, the only one I saw, is inside Kolekcija. But I did not look too hard.

here we have a function. where, inside this scope, is glumci? Its not there.

void dodaj_glumca(char* niska)
{
int brojac=0;
Glumac g(niska);
for(int i=0;i<glumci.size();i++) //where should it look??
...
what glumci are you thinking you refer to?
remember, a class is a type, and the variables in it do not exist until you create a variable OF the type. You cannot do this:
class foo
{ public:
int x;
};
x = 10; //there is no x.
you have to do this:
foo z;
z.x = 10; //ok there is an x now.
Thanks jonnin, I understand I can not do that, my mistake.
Last edited on
I don't see where you even make a call to use that bad function, so just take it out and the program works.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
using namespace std;
class Osoba{
protected:
    char* ime;
    char* prezime;
    int godina_rodjenja;
public:
    Osoba(char* niska){
        char f[]=" ";
    ime=strtok(niska,f);
    prezime=strtok(NULL,f);
    godina_rodjenja=atoi(strtok(NULL,f));
    }
    char* vrati_ime(){
    return ime;
    }
    char* vrati_prezime(){
    return prezime;
    }
    int vrati_godinu_rodjenja(){
    return godina_rodjenja;
    }
};
class Glumac:public Osoba{
private:
    int broj_uloga;
public:
    Glumac(char* niska):Osoba(niska){
    broj_uloga=0;
    }
    void uvecaj_broj_uloga(){
    broj_uloga++;
    }
};
class Reziser:public Osoba{
private:
    int broj_rezija;
public:
    Reziser(char* niska):Osoba(niska){
    broj_rezija=0;
    }
     void uvecaj_broj_rezija(){
    broj_rezija++;
    }
};
class Film{
public:
    char* naziv;
    char* zanr;
    int godina_snimanja;
    int vrijeme_trajanja;
    Reziser* reziser;
    vector<Glumac>glumaci_filma;
    Film(char* naziv_,char* zanr_,int godina_snimanja_=0,int vrijeme_trajanja_=0){
    naziv=naziv_;
    zanr=zanr_;
    godina_snimanja=godina_snimanja_;
    vrijeme_trajanja=vrijeme_trajanja_;
    }
};
class Kolekcija{
public:
    vector<Glumac>glumci;
    vector<Reziser>reziseri;
    vector<Film>filmovi;
    void novi_film(char* naziv_,char* zanr_,int godina_snimanja_=0,int vrijeme_trajanja_=0){
    Film f(naziv_,zanr_,godina_snimanja_,vrijeme_trajanja_);
    filmovi.push_back(f);
    }
    void novi_glumac(char* niska){
    Glumac g(niska);
    glumci.push_back(g);
    }
    void novi_reziser(char* niska){
    Reziser r(niska);
    reziseri.push_back(r);
    }
};
/*
void dodaj_glumca(char* niska){
    int brojac=0;
Glumac g(niska);
for(int i=0;i<glumci.size();i++){
    if(!(g.vrati_ime()==glumci[i].vrati_ime() && g.vrati_prezime()==glumci[i].vrati_prezime()))
    brojac++;
}
if(glumci.size()==0)
    glumci.push_back(g);
else if(brojac==glumci.size())
    glumci.push_back(g);
else(brojac!=glumci.size())
    glumci_filma.push_back(g);
}*/

int main(){
    string s1="Met Dejmon 1970";
    char*c1=new char[s1.length()+1];
    strcpy(c1,s1.c_str());
    Glumac g1(c1);
    cout<<g1.vrati_ime()<<endl
    <<g1.vrati_prezime()<<endl
    <<g1.vrati_godinu_rodjenja()<<endl;
return 0;
}


But just a note about that else statement on line 95... that is not going to work buddy. An else does not have a conditional check, it is meant to be what will happen when the if's aren't true.
Last edited on
nice spot. worse, it will probably compile and run something like that. here is a simple example explained.

if(x == 0) //is x zero?
foo(); //if so, this happens
else (x <3) //if not, the program will now execute the statement x<3, evaluate that to zero or 1 (true or false) and throw away this unused value. this kind of thing may be optimized out entirely since it has no effect.
cout << "whaaa?"; //this happens no matter what x is, because its not in the if or the else.
if and else need {} blocks to execute more than 1 statement.

the worst bugs are the ones that compile and run. This code would probably drive you a bit nuts to fix if you did not turn up your warnings and fix what it said.
Last edited on
Thanks, I noticed my program does not work like it was imagined. I think I fixed my program, but I will write again if i have problems.
Topic archived. No new replies allowed.