Re: Newbie problem
On 03/27/2015 03:15 PM, Louis Krupp wrote:
On Thu, 26 Mar 2015 09:53:43 -0700 (PDT), Doug Mika
<dougmmika@gmail.com> wrote:
Hi, I have defined a class called TimeType.cc and DateType.cc.
>> Inside my DateType.cc class I use the TimeType object and inside
my TimeType.cc class I use the DateType object. For some reason
>> my compiler complains with the following message:
In file included from DateType.h:11:0,
from DateType.cc:8:
TimeType.h:21:26: error: 'DateType' has not been declared
I've attached the files below: (ANY help is appreciated)
*****DateType.h*******
#ifndef DATETYPE_H
#define DATETYPE_H
#include "TimeType.h"
class DateType {
public:
DateType();
DateType(const DateType& orig);
virtual ~DateType();
void setDate(TimeType time);
private:
int day;
int month;
int year;
};
#endif /* DATETYPE_H */
*******TimeType.h*********
#ifndef TIMETYPE_H
#define TIMETYPE_H
#include "DateType.h"
class TimeType{
public:
void Set(int hours, int minutes, int seconds);
void Increment();
void Write() const;
bool Equal(TimeType otherTime) const;
bool LessThan(TimeType otherTime) const;
void setTimeFromDate(DateType date);
private:
int hrs;
int mins;
int secs;
};
#endif /* TIMETYPE_H */
1. As a general rule, post the smallest piece of code that reproduces
the problem. Your situation can be demonstrated by something like
this:
===
class a
{
void f(b& x);
};
class b
{
void f(a& x);
};
===
Nothing you can do with header files changes the fact that 'b' hasn't
been declared when it's used in class 'a'. The solution is a forward
declaration:
===
class b;
class a
{
...
};
===
This is not exactly the same situation the OP has: He isn't using
pointers or references in order to use a forward declaration to solve
his problem.
2. For your own sanity, as well as for everyone else who reads your
code, don't include user header files within other user header files.
This is a style issue; it's more work up front, but I think it makes
the code easier to read and maintain.
Regards
--
Cholo Lennon
Bs.As.
ARG