0

I am making an Observer in CPP but I have some problems to make it. I only put two files because the same errors appears in the others. I dispatch my file like this: Event.hh with the declaration of prototypes etc. And Event.cpp with the functions.

Here Event.hh:

#ifndef EVENT_HH
#define EVENT_HH

template <typename Object>

class Event {
public:
    Event(Object* obj);
    ~Event();
    Object* getInfo();
private:
    Object* _obj;
};

#endif //EVENT_HH

Here the Event.cpp:

#include "Event.hh"



Event::Event(Object* obj) {
    this->_obj = obj;
}

Event::~Event() {
    delete this->_obj;
}

Event::getInfo() {
    return this->_obj;
}

I don't really understand how to use the template and how it works, but I was following a tutorial the only difference is : the developer use one file Event.h with everything in it. I show you the error I have with the compilation:

    Event.cpp:3:1: error: invalid use of template-name ‘Event’ without an argument list
 Event::Event(Object* obj) {
 ^~~~~
Event.cpp:3:1: note: class template argument deduction is only available with -std=c++1z or -std=gnu++1z
In file included from Event.cpp:1:0:
Event.hh:6:7: note: ‘template<class Object> class Event’ declared here
 class Event {
       ^~~~~
Event.cpp:7:1: error: invalid use of template-name ‘Event’ without an argument list
 Event::~Event() {
 ^~~~~
Event.cpp:7:1: note: class template argument deduction is only available with -std=c++1z or -std=gnu++1z
In file included from Event.cpp:1:0:
Event.hh:6:7: note: ‘template<class Object> class Event’ declared here
 class Event {
       ^~~~~
Event.cpp:11:1: error: invalid use of template-name ‘Event’ without an argument list
 Event::getInfo() {
 ^~~~~
Event.cpp:11:1: note: class template argument deduction is only available with -std=c++1z or -std=gnu++1z
In file included from Event.cpp:1:0:
Event.hh:6:7: note: ‘template<class Object> class Event’ declared here
 class Event {
       ^~~~~

If you could explain to me what is wrong in my files, I'll appreciate it. Thank you. I have already looked for the errors that I have but I found nothing conclusive.

Kravennagen
  • 77
  • 10
  • 1
    Possible duplicate of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Algirdas Preidžius Oct 30 '18 at 13:36
  • I was making test but this is not my problem. Thank you for your help. I updated my info. – Kravennagen Oct 30 '18 at 14:01

1 Answers1

2

To define a member function of a template, you must declare that you're defining a member function of a template. This template declaration will include the names of the template arguments so that they can be used within the definition. Like this:

template <typename Object>

You've already sucessfully managed to use the template declaration in your definition of the constructor, but have failed to use the template declaration in the definition of other member functions.

Also, your definition of getInfo lacks a return type. Also, Event has no member named getInfo (you've accidentally added an underscore in the declaration).


Furhtermore, do realize that template functions (and member functions of template classes) must be defined in the translation units where they are instantiated. So, if you define the member functions in Event.cpp, then that is the only translation unit where you can instantiate the template.

This is not very good thing for re-usability of the template. It is usually better to define the member functions in a header file.


Lastly, it is a very bad idea to have bare pointers to owned memory.

If you make a copy of a Event instance either accidentally or on purpose, your destructor will attempt to delete the memory twice resulting in undefined behaviour.

It is not obvious to the user of the class that the argument will be deleted. It is very easy to accidentally misuse your class by constructing it with a pointer that musn't be deleted.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thank you for your answer. If I understand well I need only one declaration of "template " in my header file so Event.hh. The return type is Object for getInfo(), no ? (And yes I accidentally put an underscore thank you). What I do not understand is how to use my template in my Event.cpp file. I am checking for my pointers. – Kravennagen Oct 30 '18 at 13:55