Error when extending a template abstract class - c++

I have this code for a Node based Queue implementation and I'm supposed to extends an abstract class called QueueInterface.
template<typename T>
struct QueueInterface {
public:
virtual ~QueueInterface(){};
virtual bool isEmpty() const = 0;
virtual void enqueue(const T value) = 0;
virtual void dequeue() throw(PreconditionViolationException) = 0;
virtual T peekFront() const throw(PreconditionViolationException) = 0;
};
template<typename T>
struct Queue : QueueInterface {
Queue();
~Queue();
bool isEmpty() const;
void enqueue(const T value);
void dequeue() throw(PreconditionViolationException);
T peekFront() const throw(PreconditionViolationException);
private:
Node<T>* front;
Node<T>* back;
};
I keep getting a expected class name before '{' token error even though I included the QueueInterface header file. Why is this happening?

QueueInterface is not a class. You can inherit from something that is not a struct or a class. This thing is what is called a templated class. You can recognize templates with the template<...> just before the templated class. You must specify a type so the compiler can create a class of that type.
In your case, you are trying to create a struct that is also a template. By looking at the overrides of the methods of your base classes, I guess you are trying to do this:
template<typename T>
struct Queue : QueueInterface<T> {
// notice that there ---^--- you are sending the required parameter
// defaulted members are good.
Queue() = default;
// override too.
bool isEmpty() const override;
void enqueue(const T value) override;
void dequeue() throw(PreconditionViolationException) override;
T peekFront() const throw(PreconditionViolationException) override;
private:
Node<T>* front;
Node<T>* back;
};

Related

return type of virtual function 'method' is not covariant with the return type of the function it overrides c++

BreadthSearchableContainer.hpp
template <typename Data>
class BreadthSearchableContainer{
public:
virtual ~BreadthSearchableContainer() = default;
BreadthSearchableContainer& operator=(const BreadthSearchableContainer&) = delete;
BreadthSearchableContainer& operator=(BreadthSearchableContainer&&) noexcept = delete;
bool operator==(const BreadthSearchableContainer&) const noexcept = delete;
bool operator!=(const BreadthSearchableContainer&) const noexcept = delete;
using typename SearchableContainer<Data>::MapFunctor;
virtual void MapBreadth(MapFunctor, void*) = 0;
using typename SearchableContainer<Data>::FoldFunctor;
virtual void FoldBreadth(FoldFunctor, const void*, void*) const = 0;
};
binarytree.hpp
template <typename Data>
class BinaryTree : virtual public BreadthSearchableContainer<Data>{
public:
struct Node {
public:
friend class BinaryTree<Data>;
virtual Node& LeftChild() = 0;
};
};
binarytreelnk.hpp
template <typename Data>
class BinaryTreeLnk : public BinaryTree<Data>{
public:
struct NodeLnk : public BinaryTree<Data>::Node{
protected:
Data Elements;
struct NodeLnk* left_child;
struct NodeLnk* right_child;
public:
friend class BinaryTreeLnk<Data>;
NodeLnk& LeftChild() override;
};
protected:
struct NodeLnk* Nodelnk_root;
};
binarytreelnk.cpp
template<typename Data>
typename BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::LeftChild(){
if(left_child == nullptr){
throw std::out_of_range("Figlio sinisto non esistente");
}
else{
return Node(left_child);
}
}
MAIN
void main(){
BinaryTreeLnk<int> binary;
}
ERROR
binarytreelnk.hpp:58:14: error: return type of virtual function 'LeftChild' is not covariant
with the return type of the function it overrides ('lasd::BinaryTreeLnk<int>::NodeLnk &' is not derived from
'lasd::BinaryTree<int>::Node &')
NodeLnk& LeftChild() override;
~~~~~~~~ ^
binarytree.hpp:55:19: note: overridden virtual function is here
virtual Node& LeftChild() = 0;
I tell you right now that the project I’m doing is really huge and I don’t know if I was able to compress the necessary code to understand the error. However, this error could also concern compiler problems since to other colleagues of mine such a thing does not come out even with the code perfectly equal to mine. I have avoided putting includes as I know for certain that they have been inserted perfectly. If you can tell me why this mistake I would be very grateful. thanks in advance

cannot declare field ‘Executive::history’ to be of abstract type

I have read some other post about this error and have seen the popular problem is there are pure virtual methods in the objects class but I don't have any that I can see.
The error message is executive.h:13:44: error: invalid new-expression of abstract class type ‘List<std::__cxx11::basic_string<char> >’
This is the executive.h
private:
string command="";
List<string>* history = new List<string>();
int currentPosition=0;
public:
Executive(string filename);
~Executive();
void navigateTo(string url);
void forward();
void back();
string current() const;
void copyCurrentHistory(List<string>& destination);
void printHistory();
The List.h
template <class T>
class List: public ListInterface<T>{
private:
int length;
Node<T>* head= nullptr;
public:
List();
~List();
bool isEmpty() const;
int getLength() const;
void insert(int newPosition, const T& newEntry) ;
void remove(int position) ;
void clear();
T getEntry(int position);
void setEntry(int position, const T& newEntry);
Node<T>* getNode(int postion);
And my the class where the pure virtual methods are listInterface.h
template<class T>
class ListInterface
{
public:
virtual ~ListInterface() {}
virtual bool isEmpty() const = 0;
virtual int getLength() const = 0;
virtual void insert(int newPosition, const T& newEntry) = 0;
virtual void remove(int position) = 0;
virtual void clear() = 0;
virtual T getEntry(int position) const = 0;
virtual void setEntry(int position, const T& newEntry) = 0;
I am also getting a note from my compiler saying this but I don't make since because it says its the line where my class name is.
list.h:11:7: note: because the following virtual functions are pure within ‘List<std::__cxx11::basic_string<char> >’:
class List: public ListInterface<T>{
T getEntry(int position); is not an implementation for virtual T getEntry(int position) const = 0;. Add const to method's signature.

'PolishStack' is not a class template, virtual function ghost error

I'm having some problems implementing a class based on a abstract parent class. It's saying PolishStack is an abstract class, even though all virtual functions are coded:
In file included from braincalc.cpp:10:
./polstack.h:15:7: error: explicit specialization of non-template class 'PolishStack'
class PolishStack<T> : public AbstractStack<T> {
^ ~~~
braincalc.cpp:13:21: error: variable type 'PolishStack<char>' is an abstract class
PolishStack <char> stk;
^
./abstractstack.h:53:16: note: unimplemented pure virtual method 'isEmpty' in
'PolishStack'
virtual bool isEmpty() const = 0;
Here's my class header:
#ifndef POLSTACK_H
#define POLSTACK_H
#include <iostream>
using namespace std;
#include "abstractstack.h"
template <typename T>
class PolishStack<T> : public AbstractStack<T> {
T* data;
int mMax;
int mTop;
public:
PolishStack();
bool isEmpty();
const T& top() const throw (Oops);
void push(const T& x);
void pop();
void clear();
//my funcs:
void printStack();
~PolishStack();
};
#endif
I don't want to give all my code away due to other students cheating, so I'll post the function that the error is complaining about:
#include "polstack.h"
//...
template <typename T>
bool PolishStack<T>::isEmpty() {
if(mTop == 0)
return true;
return false;
}
//...
As others have stated it should be:
template<typename T>
class PolishStack : public AbstractStack<T>
./abstractstack.h:53:16: note: unimplemented pure virtual method 'isEmpty' in
'PolishStack'
virtual bool isEmpty() const = 0;
You're missing the const:
template<typename T>
bool PolishStack<T>::isEmpty() const
// ^^^^^
{
if(mTop == 0)
return true;
return false;
}
Note: You should use the override keyword to be informed when you try to override a function using a different signature (i.e., you're introducing a new function overload instead of overriding the virtual one).
template<typename T>
class PolishStack : public AbstractStack<T>
{
public:
...
bool isEmpty() const override;
...
};
It's hard to tell without all the code, but one thing I noticed is that this:
class PolishStack<T> : public AbstractStack<T> {
should be just:
class PolishStack : public AbstractStack<T> {
That'll fix the first error for sure and potentially (but maybe not) the second.
Try changing to
template <typename T>
class PolishStack : public AbstractStack<T>
As a side note: Exception specifiers throw (Oops) are deprecated.

How to inherit and implement a pure virtual method with the abstract class as a parameter?

I have an abstract class Node which contains a pure virtual method stub matches, requiring another instance of a Node (i.e. instance of something that subclasses Node) as a parameter.
class Node; // forward declaration
class Node {
public:
Node() : parentNode(this) {}
virtual ~Node() {}
Node* parentNode;
virtual bool matches(const Node& node) const = 0;
};
How can I implement matches in a subclass such that the parameter can be of the subclasses type as opposed to Node?
E.g. I want something like the following to register as the implemented version of the contract from Node, so that I can access NodeImpl specific properties as part of the function which I would otherwise be unable to do:
class NodeImpl : public Node {
private:
int foo;
...
};
...
bool NodeImpl::matches(const NodeImpl& n) const {
return this->foo == n.foo;
}
(I did have a try using templates to achieve this sort of effect, but I wasn't sure that I was doing it quite right. I found myself propagating the templates all over my code and encountering a myriad errors as such, and was hoping to get an idea of what the right method for this exactly is before I waste yet more time on what might well be also the wrong way of doing things.)
What I tried was:
template <class T>
class Node;
template <class T>
class Node {
public:
Node() : parentNode(this) {}
virtual ~Node() {}
Node* parentNode;
virtual bool matches(const T& node) const = 0;
};
So that I could call matches generically in a template function like so:
template <class T>
void pathComp(Node<T>& currNode, Node<T>& rootNode) {
Node<T> *node = &currNode;
while (node->matches(rootNode)) {
...
}
}
I couldn't quite get this method to work, plus I didn't like how I seemingly had to have class NodeImpl : public Node<NodeImpl> as my inheritance, something about that didn't seem quite right. Any advice as to whether I was on the right lines or not would be great!
You can't really do that in general, because it wouldn't be type-safe. For example:
struct Node { virtual bool matches(const Node &) const = 0; }
struct NodeA : Node { virtual bool matches(const NodeA &) const; };
struct NodeB : Node { virtual bool matches(const NodeB &) const; };
NodeA a; // compiler doesn't allow, but if it did...
NodeB b;
Node &c = a;
c.matches(b); // oops!
The way you are talking about implementing it, there would be an assumption that b was the same type as a, but there is no way for the compiler to verify that assumption in general, so it isn't going to allow it.
However, if you are using two nodes of the same type, you can always have the matches() function just not be virtual:
struct Node { }
struct NodeA : Node { bool matches(const NodeA &) const; };
NodeA a1;
NodeA a2;
a1.matches(a2); // fine
You should honor the superclass' contract signature. Then if you need
to access sub-class properties, just cast to the sub-class, as needed.

Accessing template friend function of class in main.

I am having a hard time finding a simple solution to this. I am implementing an expression tree using the following classes, I declare a friend function of class Tree. My problem comes when I try to get it started in main.
template<class object> class Tree;
template<class object> Tree<object>::expTree(Tree<object> *&T); // ERROR
template<class object>
struct Node
{
object info;
Node *next;
Node<object>():info(0), next(NULL) {}
Node<object>(const object &element, Node *n = NULL):
info(element), next(n){}
};
template<class object>
class Stack
{
public:
Stack();
~Stack();
void makestackempty();
bool stackEmpty() const;
void push(object &item);
void pop (object &item);
void printStack() const;
private:
Node<object> *top;
};
template<class object>
struct TreeNode
{
object info;
TreeNode *right;
TreeNode *left;
TreeNode()
{}
};
template<class object>
class Tree
{
private:
TreeNode<object> *root;
public:
Tree();
~Tree();
Tree(const Tree<object> &rhs); // copy
void operator=(const Tree<object> &rhs);
void copyconst(TreeNode<object> *orig, TreeNode<object> *&rhs);
void makeEmpty(TreeNode<object> *&tree);
bool isEmpty()const;
friend Tree<object> expTree(Tree<object> *&T){
buildTree(T.root);
};
void buildTree(TreeNode<object> *&tree);
void printTree(TreeNode<object> *&tree)const;
};
In main, I get "error: expTree was not declared in this scope."
I also get, " error: expected constructor, destructor, or type conversion before â;â token"
on the second line of this code..
Anyone have any pointers?
template<class object> Tree<object>::expTree(Tree<object> *&T); // ERROR
//^^^^^^^^^^^^^^ cause of the error
This function template is actually a free function, and will be a friend of the class Tree. So it should be written as:
template<class object> expTree(Tree<object> *&T);
That is, remove Tree<object>:: from the declaration. I just realized that it is missing the return type, and I suppose you mean Tree<object> to be the return type (and Tree<object>:: is a typo in your code). If so, then write this:
template<class object> Tree<object> expTree(Tree<object> *&T);
I would like to comment on your style of naming the template parameters and arguments. It is customary to use T, U, V etc for template parameters, not for function argument. So it feels good when you write the declaration as:
template<class T> Tree<T> expTree(Tree<T> *&object);
Well I just swapped the names.