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.
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.
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
usingnamespace 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=newchar[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.
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.