Multiple classes in .h?

I'm doing an assignment here... Up until now, I've only seen examples with 1 header for each class. But in this assignment, there are multiple classes in the provided header!

Others.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef OTHERS_H
#define OTHERS_H

class Date{
...
};

class Time{
...
};

#endif 


So I decided to create a cpp file for each class.

Date.cpp
1
2
3
#include "Others.h"

Date::Date(){...}


Time.cpp
1
2
3
#include "Others.h"

Time::Time(){...}


main.cpp

1
2
3
4
5
6
#include "Others.h"
using namespace std;
int main()
{...
return 0;
}


But I get an error when I run my program at the line where there is --#include "Others.h"-- from the Date.cpp and Time.cpp.

I don't know if the error is because of my code, or because of Eclipe (which is giving a headache).

I didn't bother putting all the code since I believe it's irrelevant, but I will if it's needed. Thank you for the help!

Edit: Fixed typos.

Last edited on
closed account (E0p9LyTq)
Class declarations end with a ;

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef OTHERS_H
#define OTHERS_H

class Date{
...
};

class Time{
...
};

#endif  
There should be no problem putting multiple classes in a single header. From what you shown, it looks okay.

Note that in others.h, lines 6 and 10 need a terminating ;
I'm assuming that was just a typo posting the code.

But I get an error when I run my program at the line where there is --#include "Others.h"-- from the Date.cpp and Time.cpp.

Please post the exact text of the error.
1
2
3
makefile:44 : recipe for "TP1_v1" target failed
collect2: error: ld returned 1 exit status
make: *** [TP1_v1] Error1

That's the error when I build the project. When I comment the --#include "Others.h"-- line from the Date.cpp and Time.cpp, I can build and run the program. But that also means I can't define the methods...
Last edited on
Hello hoboCPP,

What IDE / compiler are you using?

Andy
Hello Andy.
I'm using Mars Eclipse. It's been a very horrible experience so far, but I'm obligated to use it for my class.
hoboCPP,

I know in Visual Studio I have to include stdafx.h* in every .cpp file for the compile to work.

It is the first I have heard of Mars Eclipse, but just thinking that there might be a header file or two that might need to be included in the Date.cpp and Time.cpp files.

Just a thought,

Andy

* stdafx.h is specific to Visual Stusio
Last edited on
Topic archived. No new replies allowed.