problem in classes

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

problem in classes

Post by omar shaaban »

i have a small problem in a code like this:

Code: Select all


int main()
{

   class obj{
public:
  int id;
void shout(){
    for(int i =0;i<50;i++){
 if(par[i].id>1){cout<<"shout";}

    }}
       };
obj par[50];
    for(int i =0;i<50;i++){
par[i].id=1;
par[i].shout();
}
    cout << " hello"<< endl;
    return 0;
}
so the problem here is that par[50] was defined after obj class (it must) so when i write a function (shout) in the class which deals with par .error occurs tells me i haven't defined par.
i know there are 2 solutions one where i just define the function shout in the class then define the function after i have defined par
:

Code: Select all

int main()
{

   class obj{
public:
  int id;
void shout();

    }
       };
obj par[50];
void obj::shout(){
    for(int i =0;i<50;i++){
 if(par[i].id>1){cout<<"shout";}

    }}
    for(int i =0;i<50;i++){
par[i].id=1;
par[i].shout();
}
    cout << " hello"<< endl;
    return 0;
}
and another solution which changes function from shout() to shout(obj a ) then i use a as par but i dont want that i want function to be void shout
so is there any other solution??
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

your code is hard to read becasue of the layout....
anyhow...
why are you defining a class inside the main function?

Code: Select all



// define the class definition
// each instance will have a variable called id
// and a function called shout()
// when the shout() function is called, it will print out the id of the instance
class obj
{ 
public: 
      obj::obj(int ID) 
      {
           id = ID
      }

      int id; 

      void shout()
       {
           printf("Hello from object with an id of %d", id);
       }
};
 
int main() 
{ 

   // instantiate the object array
    obj par[50]; 

    // run through the array and assign a value to each instances id member
    for(int i =0;i<50;i++)
     { 
          par[i].id=i;      // set the value of this instances id to whatever 'i' is
          par[i].shout(); // call the shout() function of this instance 
     }
 
    cout << " hello"<< endl; 
    return 0; 
} 
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

yeah thanks for arranging the code but u changed the function!! :P
shout prints the id of all par(objects). not its own.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

sorry, cant help you then because I dont understand what it is that you are trying to do :(

you define a class called obj that has a variable called id and a function called shout(). then you instantiate an array of 50 of these obj's, set each one's id variable to 1, and then call the shout() function of the obj class 1 time for each of the 50 instances, but when the shout function is called, you want it to print out the id value of all 50 obj instances?

sorry, i really dont know for sure what you are asking............
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

you could make the array a static member of the obj class?

Code: Select all

class obj
{
     static obj par[50];

     void shout()
      {
           for (int x=0; x<50; x++)
               printf("%d", par[x].id);
       }
      
}


void main()
{
    for (int x=0; x<50; x++)
    {    
         obj::par[x].id = 1;
         par[x].shout(); 
     }
}

or something like that.........
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

yup thanks i think static will work
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Sounds to me like you really want to have 2 classes. One for the object and one class managing the objects. If that's the case - don't force it into a single class. You will have that pattern all the time - objects which are kept in an array which is in another class that has the function of managing the objects. So call if ObjManager for example.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply