-
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
-
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.
-
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
-
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.
-
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
-
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.
-
nice that fixed error #2 but it still doesnt recogonize the base class
-
-
checked it but still didnt fix it :(
-
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.
-
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...
-
You should include the Rectangle header.
-
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
-
Code:
while (selection != 1 || 2 )
while (selection != 1 || selection != 2)
-
made the change but its still an endless loop /sigh
-
I have a few comments! :)
- The Rectangle class needs a destructor, since you wrote a constructor.
- You should use your getter functions to compose the equation in your area function.
- The Cube class inherits the height variable, you dont need to make another private member for the Cube class. The Cube class can also simply use Rectangle's implementation, since nothing is different about it. Save yourself the typing.
Most importantly, default parameters belong in the implementation of the constructor:
Code:
Rectangle::Rectangle(double l=0., double w=0.) : length(l), width(w)
{ }
And of course the loop will be endless until the user types something that isn't 1 or 2. Type 0, for instance, and the loop breaks.
-
forget the loop any idea why the functions wont take the numbers and they are producing garbage?
-
No matter what the user passes into your Rectangle constructor, you ignore it and use zeros, for one thing.
Use the constructor I built for you in my last post. They don't have the same variable names as yours does, but that can be easily changed. I tested my constructor before posting it and it works fine.
-
your constructor gives me 2 errors:
.\Rectangle.cpp(4) : error C2572: 'Rectangle::Rectangle' : redefinition of default parameter : parameter 2
c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Rectangle.h(8) : see declaration of 'Rectangle::Rectangle'
.\Rectangle.cpp(4) : error C2572: 'Rectangle::Rectangle' : redefinition of default parameter : parameter 1
c:\documents and settings\johnnyg\my documents\visual studio 2005\projects\project 8\project 8\Rectangle.h(8) : see declaration of 'Rectangle::Rectangle'
thats the way i had it at the start but had to remove those due to the errors it was causing... seemed to be unavoidable
-
I will do this for you again. I told you my parameter names didn't match yours; you would need to change them--and that's what those errors mean.
This will definitely work.
Code:
Rectangle::Rectangle (double l=0., double w=0.) : length(l), width(w) { }
-
they give me the same two errors
sorry -_-
-
Sorry, I went to sleep. What exactly is the problem again as it seems we've gone off on a tangent.
-
well i have to leave to turn it in now but the problem was the var's arnt being set by the user input and are just returning garbage.