Thread: Polymorphism Help

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    17
    The error is due to the semi-colon after forest(). It is declared in the header file, but adding the semi-colon is redeclaring it in the implementation.

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    17
    Ok, I'm nearly finished. I'm trying to create the forest using the array of tree pointers.

    Code:
    //-------------------------------------------------------------------------
    //Default Forest Constructor
    //-------------------------------------------------------------------------
    
    forest::forest()
    {
      
      for(int i=0; i<21; i++){
           for(int j=0; j<21; j++){
    
               int x=i&#37;2;
               int y=j%2;
    
    	     if ( i == 0 || i == 20 || j == 0 || j == 20 ) { 
                 
                   grid[i][j] = new tree;
                 }
                 else if( x==0 || y==0 ){
    
                   grid[i][j] = new pine;
                 }
                 else{
    
                   grid[i][j] = new oak;
                 }
            }
       }
    }
    Here is the compiler error:
    Code:
    forest.cpp: In constructor ‘forest::forest()’:
    forest.cpp:30: error: expected type-specifier before ‘pine’
    forest.cpp:30: error: cannot convert ‘int*’ to ‘tree*’ in assignment
    forest.cpp:30: error: expected `;' before ‘pine’
    forest.cpp:34: error: expected type-specifier before ‘oak’
    forest.cpp:34: error: cannot convert ‘int*’ to ‘tree*’ in assignment
    forest.cpp:34: error: expected `;' before ‘oak’

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you #include Oak.h and Pine.h in the forest.cpp file?

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    17
    Nope, I forgot to include them.

    Now I have the following error:
    Code:
    /home/ryan/Desktop/C++_Lab/Final_Project/Part K/forest.cpp:32: undefined reference to `pine::pine()'
    /home/ryan/Desktop/C++_Lab/Final_Project/Part K/forest.cpp:36: undefined reference to `oak::oak()'
    /home/ryan/Desktop/C++_Lab/Final_Project/Part K/forest.cpp:32: undefined reference to `pine::pine()'
    /home/ryan/Desktop/C++_Lab/Final_Project/Part K/forest.cpp:36: undefined reference to `oak::oak()'

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you add Pine.cpp and Oak.cpp to your project (or makefile or command line or whatever you are using to build)?

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I'm stunned that one of the first things they taught in the course was not how to compile, link, and run a program.

  7. #22
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by medievalelks View Post
    I'm stunned that one of the first things they taught in the course was not how to compile, link, and run a program.
    Sounds like the course isn't the greatest in the world. It's supposed to be an Intro to C++ course, but they're trying to teach polymorphism and inheritance without teaching the STL properly (by the look of it - they're just bringing the entire STL into the global namespace) or any basic C/C++ fundimentals (includes, compiling, linking)...

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    17
    Nope, I also left them out of the make.

    Program runs fine now.

    I'm finished!

    Thanks everyone for the help!
    Ryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM