I don’t know what is the problem here… Maybe someone can help me, please.
I want to inherit my new class MyDictionary from template abstract class dictionary.
I have exactly this code:
Dictionary.h
#ifndef UNTITLED_CPP_DICTIONARY_H
#define UNTITLED_CPP_DICTIONARY_H
template<class Key, class Value>
class dictionary {
public:
virtual ~dictionary() = default;
virtual const Value &get(const Key &key) const = 0;
virtual void set(const Key &key, const Value &value) = 0;
virtual bool is_set(const Key &key) const = 0;
};
template<class Key>
class not_found_exception : public std::exception {
public:
virtual const Key &get_key() const noexcept = 0;
};
#endif //UNTITLED_CPP_DICTIONARY_H
MyDictionary.h
#ifndef UNTITLED_CPP_MYDICTIONARY_H
#define UNTITLED_CPP_MYDICTIONARY_H
#include "dictionary.h"
template<class Key, class Value>
class MyDictionary : public dictionary {
public:
const Value &get(const Key &key) const;
void set(const Key &key, const Value &value);
bool is_set(const Key &key) const;
~MyDictionary() = default;
};
#endif //UNTITLED_CPP_MYDICTIONARY_H
I am using CLion 2021.2 and the compiler MinGW 5.4, that saying like this:
====================[ Build | untitled_cpp | Debug ]============================
"C:\Program Files\JetBrains\CLion 2021.2\bin\cmake\win\bin\cmake.exe" --build D:\Projects\untitled_cpp\cmake-build-debug --target untitled_cpp -- -j 6
Scanning dependencies of target untitled_cpp
[ 33%] Building CXX object CMakeFiles/untitled_cpp.dir/MyDictionary.cpp.obj
In file included from D:\Projects\untitled_cpp\MyDictionary.cpp:1:
D:\Projects\untitled_cpp\MyDictionary.h:7:40: error: expected class-name before '{' token
7 | class MyDictionary : public dictionary {
| ^
mingw32-make.exe[3]: *** [CMakeFiles/untitled_cpp.dir/MyDictionary.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/untitled_cpp.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/untitled_cpp.dir/rule] Error 2
mingw32-make.exe: *** [untitled_cpp] Error 2
CMakeFiles\untitled_cpp.dir\build.make:83: recipe for target 'CMakeFiles/untitled_cpp.dir/MyDictionary.cpp.obj' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/untitled_cpp.dir/all' failed
CMakeFiles\Makefile2:88: recipe for target 'CMakeFiles/untitled_cpp.dir/rule' failed
Makefile:123: recipe for target 'untitled_cpp' failed
There is no helpful information about the problem. And I don’t know what to do with this.
>Solution :
dictionary is a class template, and therefore when you inherit from it you have to specify the template arguments.
In your case it seems like you would like to inherit dictionary with the same template arguments used for the derived class.
Therefore change:
class MyDictionary : public dictionary {
To:
//------------------------------------vvvvvvvvvvvv
class MyDictionary : public dictionary<Key, Value> {