base class undefined, class type redefinition and lots more
Dec 8, 2009 at 3:43am UTC
I am practising inheritance for my computer science class but I'm getting a lot of errors and I have a feeling it's one of those errors where just one small change gets rid of all of them. So anyways I'm just going to post all my files and I'd appreciate any help at all.
AngularDistance.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
using namespace std;
class AngularDistance
{
protected :
int degrees;
int seconds;
int minutes;
public :
AngularDistance();
AngularDistance(int ,int ,int );
AngularDistance(const AngularDistance&);
friend ostream& operator <<(ostream&, AngularDistance&);
AngularDistance& operator =(const AngularDistance&);
};
AngularDistance.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <iostream>
#include "AngularDistance.h"
using namespace std;
AngularDistance::AngularDistance()
{
AngularDistance::degrees = 0;
AngularDistance::minutes = 0;
AngularDistance::seconds = 0;
}
AngularDistance::AngularDistance(int deg, int min, int sec)
{
AngularDistance::degrees = deg;
AngularDistance::seconds = sec;
AngularDistance::minutes = min;
}
AngularDistance::AngularDistance(const AngularDistance& src)
{
AngularDistance::degrees = src.degrees;
AngularDistance::minutes = src.minutes;
AngularDistance::seconds = src.seconds;
}
ostream& operator <<(ostream& os, AngularDistance& src)
{
os << "Degrees: " << src.degrees << endl;
os << "Minutes: " << src.minutes << endl;
os << "Seconds: " << src.seconds << "\n\n\n" ;
return os;
}
AngularDistance& AngularDistance::operator =(const AngularDistance& src)
{
AngularDistance::degrees = src.degrees;
AngularDistance::minutes = src.minutes;
AngularDistance::seconds = src.seconds;
return *this ;
}
DirectedAngle.h(derived from the previous class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include "AngularDistance.h"
using namespace std;
class DirectedAngle: public AngularDistance
{
private :
char direction;
public :
DirectedAngle();
DirectedAngle(int ,int ,int ,char );
DirectedAngle(const DirectedAngle&);
int AngleTotal();
friend ostream& operator <<(ostream&, DirectedAngle&);
DirectedAngle& operator =(const DirectedAngle&);
};
DirectedAngle.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include "AngularDistance.h"
#include "DirectedAngle.h"
using namespace std;
DirectedAngle::DirectedAngle()
{
DirectedAngle::degrees = 0;
DirectedAngle::minutes = 0;
DirectedAngle::seconds = 0;
DirectedAngle::direction = "" ;
}
DirectedAngle::DirectedAngle(int deg, int min, int sec, char dir)
{
DirectedAngle::degrees = deg;
DirectedAngle::minutes = min;
DirectedAngle::seconds = sec;
DirectedAngle::direction = dir;
}
DirectedAngle::DirectedAngle(const DirectedAngle& da)
{
DirectedAngle::degrees = da.degrees;
DirectedAngle::seconds = da.seconds;
DirectedAngle::minutes = da.minutes;
DirectedAngle::direction = da.direction;
}
int DirectedAngle::AngleTotal()
{
return DirectedAngle::degrees + (DirectedAngle::minutes/60) + (DirectedAngle::seconds/3600);
}
ostream& operator <<(ostream& os, DirectedAngle& src)
{
os << "Degrees: " << src.degrees << endl;
os << "Minutes: " << src.minutes << endl;
os << "Seconds: " << src.seconds << endl;
os << "Direction: " << src.direction << endl;
os << "Total Angle: " << src.AngleTotal() << "\n\n" ;
return os;
}
DirectedAngle& DirectedAngle::operator =(const DirectedAngle& src)
{
DirectedAngle::degrees = src.degrees;
DirectedAngle::minutes = src.minutes;
DirectedAngle::seconds = src.seconds;
DirectedAngle::direction = src.direction;
return *this ;
}
GlobalPath.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include "DirectedAngle.h"
#include "AngularDistance.h"
using namespace std;
class GlobalPath
{
private :
int numLocations;
DirectedAngle* longitudes;
DirectedAngle* latitudes;
public :
GlobalPath();
GlobalPath(int , DirectedAngle*, DirectedAngle*);
GlobalPath(const GlobalPath&);
~GlobalPath();
void setLongitudes(int , DirectedAngle*);
void setLatitudes(int , DirectedAngle*);
friend ostream& operator <<(ostream&, GlobalPath&);
GlobalPath& operator =(const GlobalPath&);
};
GlobalPath.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include "DirectedAngle.h"
#include "AngularDistance.h"
#include "GlobalPath.h"
GlobalPath::GlobalPath()
{
GlobalPath::numLocations = 0;
GlobalPath::longitudes = NULL;
GlobalPath::latitudes = NULL;
}
GlobalPath::GlobalPath(int numLoc, DirectedAngle* lats, DirectedAngle* longs)
{
GlobalPath::numLocations = numLoc;
GlobalPath::setLatitudes(numLoc, lats);
GlobalPath::setLongitudes(numLoc, longs);
}
GlobalPath::GlobalPath(const GlobalPath& src)
{
GlobalPath::numLocations = src.numLocations;
GlobalPath::longitudes = new DirectedAngle()[src.numLocations];
GlobalPath::latitudes = new DirectedAngle()[src.numLocations];
GlobalPath::setLatitudes(src.numLocations, src.latitudes);
GlobalPath::setLongitudes(src.numLocations, src.longitudes);
}
GlobalPath::~GlobalPath()
{
delete [] GlobalPath::longitudes;
delete [] GlobalPath::latitudes;
}
void GlobalPath::setLatitudes(int num, DirectedAngle* lats)
{
delete [] GlobalPath::latitudes;
GlobalPath::latitudes = new DirectedAngle()[num];
for (int i = 0; i < num; i++)
{
GlobalPath::latitudes[i].degrees = lats[i].degrees;
GlobalPath::latitudes[i].minutes = lats[i].minutes;
GlobalPath::latitudes[i].seconds = lats[i].seconds;
GlobalPath::latitudes[i].direction = lats[i].direction;
}
}
void GlobalPath::setLongitudes(int num, DirectedAngle* longs)
{
delete [] GlobalPath::longitudes;
GlobalPath::longitudes = new DirectedAngle()[num];
for (int i = 0; i < num; i++)
{
GlobalPath::longitudes[i].degrees = longs[i].degrees;
GlobalPath::longitudes[i].minutes = longs[i].minutes;
GlobalPath::longitudes[i].seconds = longs[i].seconds;
GlobalPath::longitudes[i].direction = longs[i].direction;
}
}
ostream& operator <<(ostream& os, GlobalPath& src)
{
os << "Number of locations: " << src.numLocations << endl;
os << "Latitudinal coordinates: " << endl;
for (int i = 0; i < src.numLocations; i++)
{
os << src.latitudes[i];
}
os << "\n\n\nLongitudinal coordinates: " << endl;
for (int i = 0; i < src.numLocations; i++)
{
os << src.longitudes[i];
}
return os;
}
The "Base class undefined error" is coming from line 6 of directedangle.h. Class type refinition is coming from the same line. Any thoughts?
Dec 8, 2009 at 4:32am UTC
Two things:
1. You need header guards on your headers. That will likely prevent the redefinition error.
2. Never put using directives or declarations in header files, especially at the global namespace level. Your users may not want their global namespace polluted. Get into that habit now before it is too late.
Dec 8, 2009 at 4:45am UTC
what are header guards. could you elaborate please?
Dec 8, 2009 at 5:00am UTC
Dec 8, 2009 at 5:11am UTC
even with those changes I still get the errors. I already went through and took out the "#includes" where they weren't needed.
Dec 8, 2009 at 5:39am UTC
Are you sure you put header guards in place, in addition to stripping the unnecessary #includes?
Header guard example:
1 2 3 4 5 6
#ifndef ANGULARDISTANCE_H
#define ANGULARDISTANCE_H
... Your header file contents go here ...
#endif
Topic archived. No new replies allowed.