2 errors in my code

This is a discussion on 2 errors in my code within the C++ Programming forums, part of the General Programming Boards category; i got a project due tomorrow dealing with inheritence and i seem to have hit a brick wall. the errors ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    2 errors in my code

    i got a project due tomorrow dealing with inheritence and i seem to have hit a brick wall. the errors are:

    c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Cube.h(4) : error C2504: 'Rectangle' : base class undefined
    .\Cube.cpp(5) : error C2511: 'Cube::Cube(void)' : overloaded member function not found in 'Cube'
    c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Cube.h(3) : see declaration of 'Cube'

    rectangle is a declared class

    if needed here it is:
    Code:
    class Rectangle
    {
    public:
    	Rectangle(double=0.0,double=0.0);
    	double getWidth();
    	double getLength();
    	double getArea();
    private:
    	double width;
    	double length;
    };
    the cube class is here:
    Code:
    class Cube : public Rectangle
    {
    
    	public:
    		Cube(double=0.0);
    		double getHeight();
    		double getVolume();
    	private:
    		double height;
    		double volume;
    };
    and the cpp where the 2nd error occurs
    Code:
    #include "Cube.h"
    
    
    Cube::Cube(double height=0.0)
    {
    
    }
    
    double Cube::getHeight()
    {
    	return height;
    }//height
    
    double Cube::getVolume()
    {
    	double volume;
    	volume=height*height*height;
    	return volume;
    }//getvolume
    if anyone can help would really appreciate it thanks in advance

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    By the looks of it, your *full* Cube code looks something like this:

    Code:
    #ifndef _CUBE_H_
    #define _CUBE_H_
    class Cube : public Rectangle
    {
    
    	public:
    		Cube(double=0.0);
    		double getHeight();
    		double getVolume();
    	private:
    		double height;
    		double volume;
    };
    #endif
    Which means that the cube class doesn't know about the rectangle class, as it hasn't included the rectangle class's header file.

    So, add this:

    Code:
    #include "Rectangle.h"
    That's, of course, under the assumption that you haven't done that already.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    i tried that in the cube.h and it caused 11 more errors... the class i was given by my instructor i thought it was right but...i dono im out of ideas

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    How big is your code? If it's not overly huge, just paste the whole files. It'll be much easier to help you out.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    its not huge. ill post the remaining files
    rectangle.cpp:
    Code:
    #include "Rectangle.h"
    
    Rectangle::Rectangle(double length, double width)
    {
    	 length=0.0;
    	 width=0.0;
    }
    
    double Rectangle::getWidth()
    {
    	return width;
    }
    
    double Rectangle::getLength()
    {
    	return length;
    }
    
    double Rectangle::getArea()
    {
    	double area;
    	area=length*width;
    	return area;
    }
    main file ( the part commented out was my first attempt)
    Code:
    #include "Rectangle.h"
    #include "Cube.h"
    #include <iostream>
    //#include <fstream>
    #include <iomanip>
    #include <conio.h>
    
    using namespace std;
    /*
    void main()
    {
    	cout<<"Welcome to the rectangle program which will ask you for a length and a width"<<endl;
    	cout<<" it will then compute the area "<<endl;
    	//cout<<"Please enter the length: "<<endl;
    	//cin>>length; undeclared var.
    	Rectangle r(4,5);
    	r.getArea();
    	cout<<endl<<r.getLength()<<endl;
    }//main
    */
    void main()
    {
    	int selection=0;
    	double length=0;
    	double width=0;
    	double height=0;
    		cout<<"Basic Geometric Calculator Program"<<endl;
    		cout<<"1) Calculate Rectangle Statistics. "<<endl;
    		cout<<"2) Calculate Cube Statistics."<<endl;
    		while (selection != 1 || 2 )
    		{
    			cout<<"Please enter the number for the function you wish to execute. "<<endl;
    			cout<<"Function #: ";
    			cin>>selection;
    			if (selection==1)
    {
    			cout<<"\nPlease enter the lenght of the rectangle: ";
    			cin>>length;
    			cout<<"\nPlease enter the width of the rectangle: ";
    			cin>>width;
    			Rectangle r(length,width);
    			cout<<"\n\nRectangle Statistics: "<<endl;
    			cout<<"\nLength: "<<r.getLength();
    			cout<<"\nWidth: "<<r.getWidth();
    			cout<<"\nArea: "<<r.getArea();
    }
    			else if (selection==2)
    {
    			cout<<"\nPlease enter the side length of the cube: ";
    			cin>>height;
    			Cube c(height);
    			cout<<"\n\nCube Statistics: "<<endl;
    			cout<<"\nLength: "<<c.getHeight();
                                               cout<<"\nWidth: "<<c.getHeight();	
    			cout<<"\nHeight: "<<c.getHeight();
    			cout<<"\nVolume: "<<c.getVolume();
    }
    			else
    				cout<<"\nInvalid Selection. Please re-enter selection."<<endl;
    	}
    }//main

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    First off, fix this:
    Code:
    #include "Cube.h"
    
    
    Cube::Cube(double height=0.0)
    {
    
    }
    Change it to:
    Code:
    #include "Cube.h"
    
    
    Cube::Cube(double height)
    {
    
    }
    Second off, make sure your header files have #ifndef/#define/#endif statements surrounding the class as I did in the above code for Cube. This must be in Rectangle.h *and* Cube.h, and for that matter, any header file that you make which contains a class in it from this point on.
    Then, include Rectangle.h from Cube.h and you won't have any issues.
    Last edited by jverkoey; 05-03-2006 at 10:08 PM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    nice that fixed error #2 but it still doesnt recogonize the base class

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Check my edit.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    checked it but still didnt fix it

  10. #10
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Cube.h(4) : error C2504: 'Rectangle' : base class undefined

    This could only be caused if the Cube header isn't including Rectangle.h, which clearly it's not. So, adding that should fix it.

    Then, if this causes more problems, it is because of the fact that you aren't using #ifndef/#define/#endif correctly. Just a quick rundown:

    Say, for myClass.h
    Code:
    #ifndef _MYCLASS_H_
    #define _MYCLASS_H_
    
    // class definition
    
    #endif // _MYCLASS_H_
    That is how all of your class headers should look.

    Then, because your Cube.cpp file includes Cube.h, which includes Rectangle.h, we have this:

    Rectangle.h > Rectangle.cpp
    vvvvvvvv
    Cube.h > Cube.cpp
    vvvvvvvv
    main.cpp

    So, if main.cpp includes Cube.h, it automatically knows about Rectangle.h and your compiler grins with happiness.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    i normally code that into all my headers but this was given out of the assignment sheet so i didnt and i coded those in look:
    Code:
    #ifndef CUBE_H
    #define CUBE_H
    class Cube : public Rectangle
    {
    
    	public:
    		Cube(double=0.0);
    		double getHeight();
    		double getVolume();
    	private:
    		double height;
    		double volume;
    };
    #endif

    Code:
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    class Rectangle
    {
    public:
    	Rectangle(double=0.0,double=0.0);
    	double getWidth();
    	double getLength();
    	double getArea();
    private:
    	double width;
    	double length;
    };
    #endif
    yet still i get:

    c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Cube.h(5) : error C2504: 'Rectangle' : base class undefined

    my friends and i are stumped... it should be working...

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should include the Rectangle header.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    tried it again after adding the ifndef and it compiles but.

    the values enter by the user are not making it to the cpp of rectangle/cube. its producing garbage as the length and witdth and height.

    in addition entering the wrong char sends it into an endless loop.

    any ideas about that? -_-

    P.S thanks so much for the quick help guys
    Last edited by johnnyg; 05-03-2006 at 10:36 PM.

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    while (selection != 1 || 2 )
    while (selection != 1 || selection != 2)

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    made the change but its still an endless loop /sigh

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  3. RAM/Swap Memory Code Errors
    By ghe1 in forum Linux Programming
    Replies: 2
    Last Post: 04-01-2002, 06:37 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21