Thread: Inline functions and inheritance

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Inline functions and inheritance

    Hi guys,
    I am having some issues getting my code to compile that I am working on that deals with inline functions and inheritance. The code is supposed to create a squareShape class that inherits the attributes of the rectShape class. I made the main code simply attempt to instantiate a squareShape object with particular properties (xpos, ypos, length of each side, and fill color) assigned to it.
    I'll include all of the code that I am using and below I'll tell what I've tried to do to resolve this (to no avail):

    The first header file is d_rectsh.h, which is created/provided by the author of the book:

    Code:
    #ifndef RECTANGLESHAPE_CLASS
    #define RECTANGLESHAPE_CLASS
    
    #include "d_shape.h"
    
    // declaration of rectShape class with base class shape
    class rectShape: public shape
    {
       public:
          rectShape(double x = 0.0, double y = 0.0,
                    double len = 0.0, double wid = 0.0,
                    shapeColor c = darkgray);
    			// constructor. has parameters for the base point,
    			// length, width and color
    
          double getLength() const;
          double getWidth() const;
          void setSides(double len, double wid);
    	      // retrieve or set rectangle dimensions
    
          virtual void draw();
    	      // draw the rectangle
    
       private:
          double length, width;
    			// rectangle dimensions
    
    };
    
    rectShape::rectShape(double x, double y, double len, double wid,
                         shapeColor c):
                shape(x,y,c), length(len), width(wid)
    {}
    
    double rectShape::getLength() const
    {
       return length;
    }
    
    double rectShape::getWidth() const
    {
       return width;
    }
    
    void rectShape::setSides(double len, double wid)
    {
       length = len;
       width = wid;
    }
    
    void rectShape::draw()
    {
       EZDCOLORVAL old_color;
       
       old_color = ezdSetColor(color.convertToEzdColor());
    
       // execute primitive function and draw the rectangle
       shape_handle = ezdDrawRectangle(baseX, baseY,
                                       baseX+length,baseY+width);
    
       ezdSetColor(old_color);
    }
    
    #endif   // RECTANGLESHAPE_CLASS
    The next part is the sqshape.h file that I created that is supposed to declare the inherited squareShape class and use inline functions within the class:

    Code:
    #include <iostream>
    #include "d_rectsh.h"
    
    
    using namespace std;
    
    class squareShape: public rectShape
    {
    public:
    	squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
    	rectShape(xpos,ypos,c), side(s)
        
    	double getSide() const
    	{
    		return s;
    	}
    	void setSide(double s)
    	{
    		s=side;
    	}
    		
    private:
    	double s;
    };
    Finally, I have my main code that is simply attempt to instantiate the squareShape object with certain attributes. I know that I'll also need to do the draw function to make anything appear onscreen, but I'll deal with that once I get this figured out:

    Code:
    #include <iostream>
    #include "sqshape.h"
    
    int main ()
    {
    	squareShape sq(4.0,4.0,3.0,red);
    	
    	return 0;
    }
    Here are the error messages that I get (I am using Visual C++ 6.0):

    --------------------Configuration: ex_13_27 - Win32 Debug--------------------
    Compiling...
    ex_13_27.cpp
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(13) : error C2612: trailing '=' illegal in base/member initializer list
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(13) : error C2664: '__thiscall rectShape::rectShape(double,double,double,double,c lass shapeColor)' : cannot convert parameter 3 from 'class shapeColor' to 'double'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(13) : error C2614: 'squareShape' : illegal member initialization: 'side' is not a base or member
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(14) : error C2270: 'getSide' : modifiers not allowed on nonmember functions
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(14) : error C2601: 'getSide' : local function definitions are illegal
    C:\Program Files\Microsoft Visual Studio\VC98\ex_13_27\ex_13_27.cpp(10) : fatal error C1004: unexpected end of file found

    Here is what I have tried:

    I tried what is seen currently in the sqshape.h file and received the error messages above. I have also tried implementing the inline functions by preceeding them with "inline" before the function name, but that did not work. I've also tried using "inline" along with the class name (squareShape), but that did not work.

    I've looked for examples all over the web and the 3 or 4 other C++ books I have in my possession, but can't find anything that seems to solve my issue. Anything that someone can suggest is greatly appreciated.

    Thanks for your time, as always!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    public:
    	squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
    	rectShape(xpos,ypos,c), side(s);
        
    	double getSide() const
    Try that.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Hi Quzah,
    I should have mentioned that I had tried that before as well (adding the semicolon to the end of my constructor) and when I do that I receive this error message:
    Code:
    c:\program files\microsoft visual studio\vc98\ex_13_27\sqshape.h(10) : error C2969: syntax error : ';' : expected member function definition to end with '}'
    Any idea why this might occur? It seems as sound as any other class code that I've written in the past, I just haven't dealt with inheritance or inline functions often.

    Thanks again!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you need to actually make your constructor construct something. Not just be a prototype. If it isn't going to do anything, don't put one in. Otherwise, make it do something:
    Code:
    class Foo {
    public:
        Foo(int y) { x = y }
    
    private:
        int x;
    };
    Do like you did with the constructor for rectShape.
    Code:
    rectShape::rectShape(double x, double y, double len, double wid,
                         shapeColor c):
                shape(x,y,c), length(len), width(wid)
    {}
    See the error message helps you:
    expected member function definition to end with '}'
    It's saying: "Hey, you're making a function here, where's the function body?"

    So either just add {} to it, or make it outside the function like you did for rectShape.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 08:47 AM
  2. [B]Overloading Functions In Inheritance[/B]
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2005, 11:50 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and virtual functions
    By Cryptomega in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2003, 02:59 PM
  5. inheritance & virtual functions
    By xagiber in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 12:10 PM