Thread: Class Help

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    17

    Class Help

    I'm currently working on my forest class for my forest fire simulation, but I've run into trouble.
    In one particular spot, it wants to say grid is not declared.

    Here is a working implementation followed by the driver:
    Code:
    int forest::nextStatus(int i, int j) const
    {
      double rannum, z;
      rannum = static_cast<double>(rand())/RAND_MAX;
       z=(grid[i][j].getProbCatch()/grid[i][j].getWetness());
       
      if( (grid[i][j].getStatus()==live) && (rannum<z) && (grid[i+1][j].getStatus()==burning))  
      {
        return burning;
      }
      
      else if( (grid[i][j].getStatus()==live) && (rannum<z) && (grid[i-1][j].getStatus()==burning))
      {
        return burning;
      }
    
      else if( (grid[i][j].getStatus()==live) && (rannum<z) && (grid[i][j+1].getStatus()==burning))
      {
        return burning;
      }
    
      else if( (grid[i][j].getStatus()==live) && (rannum<z) && (grid[i][j-1].getStatus()==burning))
      {
        return burning;
      }
    
      else if( grid[i][j].getStatus()==burning )
      {
        return dead;
      }
    
      else if( grid[i][j].getStatus()==dead )
      {
        return dead;
      }
    
      else
      
    return live;
    }
    Code:
    void forest::applyNextStatus()
    {
      tree next_grid[FW][FH];
       int i, j, stat;
       
      for(int i=0; i<21; i++){
         for(int j=0; j<21; j++){
         
             next_grid[i][j]=grid[i][j];
         }
      }
    
    
       for(int i=1; i<20; i++){
          for(int j=1; j<20; j++){
    
              stat=nextStatus(i, j);
              next_grid[i][j].setStatus(stat);
          }
       }
    
      for(int i=0; i<21; i++){
         for(int j=0; j<21; j++){
    
              grid[i][j]=next_grid[i][j];
         }
      }
    }
    Driver:

    Code:
    b.applyNextStatus();
    However for this set, it is saying grid is not declared.
    Code:
    int regrowth(double growProb)
    {
        double rannum;
        rannum=static_cast<double>(rand())/RAND_MAX;
    
       for(int i=1; i<20; i++){
           for(int j=1; j<20; j++){
               if( (grid[i][j].getStatus()==dead) && (rannum<growProb)){
    
                  return live;
               }
               else{
    
                   return dead;
               }
           }
       }
    }
    Driver:
    Code:
    b.regrowth(rprob);

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    21
    Where do you define grid? (ps; is regrowth supposed to be a member of forest?)

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    17
    Here is the header file for the forest class:
    Code:
    #ifndef FOREST_H
    #define FOREST_H
    
    //Forest class declaration
    
    #include"tree.h"
    #include<iostream>
    using namespace std;
    
    #define FW 21
    #define FH 21
    
    class forest
    {
       private:
         tree grid[FW][FH];
    
       public:
         forest(); //default constructor
         void setGrid(int i, int j, double prob, int stat, double wet, int burn);
         int nextStatus(int i, int j) const;
         void applyNextStatus();
         bool isBurning();
         void lightning(double lightProb);
         int regrowth(double growProb);
    
       friend ostream& operator<<(ostream& out, forest&  b);
    };
    #endif //FOREST_H

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're missing something in the definition of regrowth (see scarecrow's ps for a hint).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    21
    Was the class specifier lost in translation, or is your code actually this way (sans forest:: ):
    Code:
    int forest::regrowth(double growProb)
    {
        double rannum;
        rannum=static_cast<double>(rand())/RAND_MAX;
    
       for(int i=1; i<20; i++){
           for(int j=1; j<20; j++){
               if( (grid[i][j].getStatus()==dead) && (rannum<growProb)){
    
                  return live;
               }
               else{
    
                   return dead;
               }
           }
       }
    }

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    17
    Hah, there I go again making stupid simple mistakes.

    I forgot to put forest in front.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    21
    Shouldn't you have received an undefined ref. to regrowth??

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    17
    Ok, it compiles, but it doesn't run anymore. Comment out the call to regrowth and the program runs fine.

    Here is the whole code for the driver loop:

    Code:
    while(b.isBurning()==true){
    
      tt=clock()+steppause*CLOCKS_PER_SEC;
      while(clock()<tt){}
      
      b.applyNextStatus();
      b.regrowth(rprob);
      cout<<b<<endl;
      
      
    }

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    21
    Can you post the actual console output?
    Also, define "doesn't run anymore"...

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    17
    Found my error. The problem lies in the function regrowth.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Shouldn't you have received an undefined ref. to regrowth??
    No, because the compiler thought it was a regular non-member function.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In other words, the compiler couldn't find the variable.
    If you tell the compiler that a variable exists but it doesn't (extern declarations, for example), THEN you will get a linking error. In other words, lie to the compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM