.hpp file without .cpp

Hello,

I'm about to create a priority queue using a heap to it. So, to do so I will fill the MaxHeap array with objects from a struct called Elements, that means that I'll have an Element array. To organize the project I crated a file called element.hpp that contains the following code:

1
2
3
4
5
6
7
8
9
10
11
12

#ifndef ELEMENT
#define ELEMENT

typedef struct {
	unsigned int priority;
	char data;

} Element;

#endif


As this is a struct type, I don't need neither a constructor nor any methods (in this case). So, should I leave it like that without a .cpp file, or just declare the struct inside other file? Thanks!


Last edited on
First thing to note that is there is literally no difference between a "struct" and a "class" in C++, except that a struct's member are public by default, and a class's members are private by default.

With that out of the way, I see nothing wrong with your code. Yes, structs/classes are usually declared in the header file you wish to use them. You don't need a .cpp file if it serves no purpose.

Note that you don't need to do the whole "typedef struct {} name_of_struct;" thing in C++. That is just a C relic.

You can just do:
1
2
3
4
5
6
7
8
9
#ifndef ELEMENT_HPP
#define ELEMENT_HPP

struct Element {
	unsigned int priority;
	char data;
};

#endif 

Last edited on
Thanks so much! Got it now
Topic archived. No new replies allowed.