Thread: inheritance problems

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    inheritance problems

    I have to use provided parent classes to create a class called inventory. When i try to compile I get

    error C2352: 'Lumber::getlumber' : illegal call of non-static member function

    Im really ranking my brain on this one. I am not allowed to make any changes to the parents. How can i call a memberfunction without having to be static?
    Code:
    The provided class definitions
    //  program_id       LUMBER.H
    //  written_by       Hi Lo Jo
    //  company          The Woods of Colorado Springs, CO
    //
    //  description      This header file contains the class definitions 
    //                   needed for the program: STORELUMBER.CPP. 
    //                   The method definitions are in the file LUMBER.CPP
    //
    
    #ifndef LUMBER
    #define LUMBER
    
    const short LEN = 40;       // maximum length of strings 
    
    class Type                // type of lumber
    {
       private:
          char dimensions[LEN];
          char grade[LEN];
       public:
          Type();                     // constructor (no args)
    	
          Type(char di[], char gr[]);  // constructor (two args)
    	
          void gettype();              // get type from user
    
          void showtype();             // display type
    
          char * show_grade();         // displays the grade
    
          char * show_dimensions();    // displays the dimensions
    };
    
    class Distance                    // English Distance class
    {
       private:
          short feet;
          double inches;
       public:
          Distance();                  // constructor (no args)
    	
          Distance(short ft, double in);  // constructor (two args)
    
          void getdist();              // get length from user
    
          int show_feet();             // shows the feet
    
          double show_inches();        // shows the inches
    
          void showdist();             // display distance	
    };
    
    class Lumber : public Type, public Distance
    {
       private:
          short quantity;                      // number of pieces
          double price;                       // price of each piece
       public:
          Lumber(); // constructor (no args)  
    						 // constructor (6 args)
          Lumber( char di[], char gr[],      // args for Type
    	      short ft, double in,          // args for Distance
    		  short qu, double prc ); 
    
          void getlumber();                    // gets from the keyboard the lumber values
    	
          void showlumber();                   // displays on the screen the values
    
          int show_quantity();                 // diplays on the screen the quantity
    
          double show_price();                 // diplays on the screen the  price
    };
    
    
    #endif
    
    
    }


    Code:
    The provided class method definitions, i cut out a lot to save some space
    //  program_id       LUMBER.CPP
    //  written_by       Hi Lo Jo
    //  company          The Woods of Colorado Springs, CO
    //
    //  description      This file contains the class method definitions 
    //                   needed for the program: STORELUMBER.CPP. 
    //                   The class definitions are in the file LUMBER.H
    
    
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    #include"lumber.h"
    
    
    // type of lumber function definitions
    
    Type::Type()                      // constructor (no args)
    { 
        strcpy(dimensions, "N/A"); strcpy(grade, "N/A"); 
    }
    
    Type::Type(char di[], char gr[])  // constructor (two args)
    { 
        strcpy(dimensions, di); strcpy(grade, gr); 
    }
         
    void Type::gettype()              // get type from user
    {
        cout << "   Enter nominal dimensions (2x4 etc.): ";
        cin >> dimensions;
        cout << "   Enter grade (rough, const, etc.): ";
        cin >> grade;
    }
    
    void Type::showtype()             // display type
    {
        cout << "\n   Dimensions: " << dimensions;
        cout << "\n   Grade: " << grade;
    }
    
    char * Type::show_grade()
    {
        return grade;
    }
    
    char * Type::show_dimensions()
    {
        return dimensions;
    }
    
    // Distance class function definitions
    
    Distance::Distance()                  // constructor (no args)
    { 
        feet = 0; inches = 0.0; 
    }
    
    Distance::Distance(short ft, double in)  // constructor (two args)
    {
        feet = ft; inches = in; 
    }
    
    void Distance::getdist()              // get length from user
    {
        cout << "   Enter feet: ";  cin >> feet;
        cout << "   Enter inches: ";  cin >> inches;
    }
    
    void Distance::showdist()             // display distance
    { 
        cout  << feet << "\'-" << inches << '\"'; 
    }
    
    int Distance::show_feet()
    {
        return feet;
    }
    
    double Distance::show_inches()
    {
        return inches;
    }
    
    // Lumber class function definitions
    
    Lumber::Lumber() : Type(), Distance()      // constructor (no args)
    { 
        quantity = 0; price = 0.0; 
    }
    					 // constructor (6 args)
    Lumber::Lumber( char di[], char gr[],      // args for Type
    	      short ft, double in,          // args for Distance
    	      short qu, double prc ) :      // our own args
    	      Type(di, gr),              // call Type constructor
    	      Distance(ft, in)           // call Distance constr
    {
        quantity = qu; price = prc;     // use our own args
    }
    
    void Lumber::getlumber()
    {
        Type::gettype();
        Distance::getdist();
        cout << "   Enter quantity: "; cin >> quantity;
        cout << "   Enter price per piece: "; cin >> price;
    }

    Code:
    class i wrote that uses the parents lumber and date, i did not attach date
    //newLumber.h
    #include "lumber.h"
    #include "date.cpp"
    
    #ifndef INVENTORY
    #define INVENTORY
    
    //Derived class from parents lumber and date
    class inventory : public LUMBER DATE
    {
    private:
    	long inventoryNumber;
    
    protected:
    
    
    
    public:
    	inventory();
    	void	enterSku();
    };
    
    #endif
    Code:
    Class member functions
    //newLumber.cpp
    #include <iostream>
    #include "newLumber.h"
    
    using namespace std;
    
    
     //member function that will create a new sku
    void  inventory::enterSku() 
    { 
    	cout << "What is the SKU number?" << endl;
    	cin >> inventoryNumber;
    	Lumber::getlumber();
    }
    Last edited by generic83; 04-10-2005 at 11:48 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    So... you are getting an error with the date class and that is the one thing you don't show us?

    Oh... you should not include source files. If you really have a multi-file project, the accepted way to generate the program is to compile all the source files into object files and then link those object files into an executable. How you do that depends on if you are using an IDE of some sort or a command line tool.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Quote Originally Posted by hk_mp5kpdw
    So... you are getting an error with the date class and that is the one thing you don't show us?

    Oh... you should not include source files. If you really have a multi-file project, the accepted way to generate the program is to compile all the source files into object files and then link those object files into an executable. How you do that depends on if you are using an IDE of some sort or a command line tool.
    hah, oops, it doesn't matter which function i call, i still get the same error. I was just goofing around and forgot to change it looks like. Ill worry about including the source files later heh

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How about:
    Code:
    Lumber mylumber;
    mylumber.getlumber();
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    That works.. But i still have some questions. What we did there was create another class of the type lumber right? So wouldn't i have a class inventory that contains lumber, type, distance, and date AND another class of lumber than contains type and distance as well?


    What i want to be able to do is create an array[100] of the class inventory and be able to add items.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah my bad, I see what you are doing now. It seems like doing it that way should work (although It's been awhile since I used inheritance much). I don't think you need to disambiguate the call to getlumber, try just:
    Code:
    void  inventory::enterSku() 
    { 
    	cout << "What is the SKU number?" << endl;
    	cin >> inventoryNumber;
    	getlumber();
    }
    I hope this line is just a typo:
    Code:
    class inventory : public LUMBER DATE
    //should be
    class inventory: public Lumber,public Date
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  3. Class inheritance problems
    By Carolina in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2006, 04:30 AM
  4. Inheritance problems
    By wheels165 in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 04:49 PM
  5. Inheritance problems
    By alkis_y3k in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2003, 06:00 PM