KSetBase.h base class , an interface
#ifndef HW6_GTUSETBASE_H
#define HW6_GTUSETBASE_H
class KSet;
#include <cstddef>
namespace TU {
template<typename T>
class KSetBase {
public:
virtual bool empty() const = 0;
virtual size_t size() const = 0; //int is not okay since unsigned also could be in.
//comparisons would be not working if that was case
//detailed
//https://stackoverflow.com/questions/1181079/stringsize-type-instead-of-int
virtual size_t max_size() const = 0;
virtual void insert(T first, T second) = 0; //cift dondurmesi gerekli
virtual void erase(T deleter) = 0;
virtual void clear() = 0;
virtual T find(T deneme) = 0;
virtual size_t count(T testle) = 0;
virtual T begin() = 0;
virtual T end() = 0;
protected:
~GTUSetBase() {
//do nothing
}
};
}
#endif //HW6_GTUSETBASE_H
KSet.h (derived class)
#include "KSetBase.h"
#ifndef HW6_GTUSET_H
#define HW6_GTUSET_H
#include <memory>
using namespace std;
namespace TU {
template<typename T>
class KSet : public KSetBase {
public:
bool empty() const;
size_t size() const;
size_t max_size() const;
void insert(T first);
void erase(T deleter);
void clear();
T find(T deneme);
size_t count(T testle);
T begin();
T end();
protected:
int hmany = 0;
shared_ptr<T> set_harmony;
};
}
#endif //HW6_GTUSET_H
I cant figure out what part of my code is wrong. I basically made a basic inheritance from interface class, which i implemented all functions in KSet.cpp. In theory nothing looks wrong but it gives 2 errors.
both in main.cpp and KSet.cpp
class KSet : public KSetBase {
Error line is this.
I checked #ifndef and #defines, i thought about using forward declaration but i cant seems to work that out.I am really stuck with this problem. I searched whole web about it , in the end there's always a big error on syntax or something that is visible , but i can't figure out on mine. It's very short and simple code.
KSetBase is a template class, if you inherit from it you must specify a template argument:
template<typename T>
class KSet : public KSetBase<T>
In addition, it seems you have a typo in the base class destructor.
Related
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
I'm coding my own Tree data structure.
In my parent() function, I want to throw no_parent exception that I created. All functions are implemented in Tree.cpp file.
Tree.hpp
#ifndef Tree_hpp
#define Tree_hpp
#include <stdio.h>
#include <vector>
using namespace std;
template<typename T>
class Tree{
class Node;
class no_parent : public std::exception {
virtual const char* what() const _NOEXCEPT
{
return "no_parent";
}
};
protected:
Node* rootNode;
Node* currentNode;
int _size;
public:
Tree();
void addChild(T* childElem);
void removeChild(int index) throw (out_of_range);
bool empty();
int size();
Tree& root();
bool isRoot();
bool isExternal();
Tree& parent() throw(no_parent);
Tree& child(int index) throw(out_of_range);
int getChildrenNum();
int getChildrenNum(Node* node);
};
#endif /* Tree_hpp */
But when I implement no_parent in Tree.cpp file in this way
template<typename T>
class Tree<T>::no_parent : public std::exception {
virtual const char* what() const _NOEXCEPT
{
return "no_parent";
}
};
I get error message from parent() function "Incomplete type 'Tree::no_parent' is not allowed in exception specification".
How can I implement the no_parent outside of Tree.hpp file??
You cannot implement methods depending on template parameters in a source file which is never included where an instance of the class is needed. You either have to include the source file as well or implement your methods in the header right away. See this question for details.
I hope your business need you to have nested class and a forward declaration in class Tree. I do not care them much.
But your code works fine with "noexcept" keyword from C++11.
http://en.cppreference.com/w/cpp/language/noexcept
I guess the "_NOEXCEPT" does not exist any more. May be in very specific compilers to maintain backward compatibility.
I did few changes to your code and tested, works fine. please find details below.
Removed "stdio" and replaced "iostream", to deal with only C++ (at least the question tag says)
Commented few functions, which are lack of variable declaration.
Here is the code. try it.
#include <iostream>
using namespace std;
template<typename T>
class Tree{
class Node;
class no_parent : public std::exception {
//Added "noexcept" key word here.
virtual const char* what() const noexcept
{
return "no_parent";
}
};
protected:
Node* rootNode;
Node* currentNode;
int _size;
public:
Tree();
void addChild(T* childElem);
// void removeChild(int index) throw (out_of_range);
bool empty();
int size();
Tree& root();
bool isRoot();
bool isExternal();
Tree& parent() throw(no_parent);
//Tree& child(int index) throw(out_of_range);
int getChildrenNum();
int getChildrenNum(Node* node);
};
int main()
{
return 0;
}
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.
I'm very new to C++ and I'm not quite sure how to override methods from base class.
I apologize for not translating the code to English prior to posting, but I don't think it would make any difference.
A summary of what I'd like to do is create an abstract base class for linked lists, then derive singly/doubly/multiply linked/circular lists from it etc.
Here is the code:
#ifndef __LISTA_H__
#define __LISTA_H__
class Elem;
class JulElem;
class Lista // Abstract list class
{
public:
Lista();
~Lista();
virtual void dohvatiGlavu() = 0;
virtual void dodajNaKraj(int vred) = 0;
virtual void dodajIza(Elem* elem, int vr) = 0;
virtual void obrisiIza(Elem* elem) = 0;
virtual bool prazna() = 0;
virtual int velicina() = 0;
protected:
class Elem
{
protected:
int vred;
};
virtual Elem* noviElem();
private:
Elem* glava;
};
class Jul : Lista // Singly linked list
{
public:
void dohvatiGlavu();
void dodajNaKraj(int vred);
void dodajIza(Elem* elem, int vr);
void obrisiIza(Elem* elem);
bool prazna();
int velicina();
private:
class JulElem : Elem
{
};
JulElem* noviElem();
};
This is the error I get:
error C2555: 'Jul::noviElem': overriding virtual function return type differs and is not covariant from 'Lista::noviElem'
I've mostly seen people use structs for list elements but I'm not sure how that would fit my case scenario because different types of lists require different types of structs.
Thanks
The problem is that the inheritance is private, so JulElem* is not covariant with Elem*. Use public inheritance:
class JulElem : public Elem
^^^^^^
The return type of the function can not be overrided. you should do like this:
in the base, define the function as:
virtual void noviElem(Elem** result);
The parameter "result" served as an output paramter
It seems like you need to use templates.
Here is an example :
template<class T>
T Add(T n1, T n2)
{
T result;
result = n1 + n2;
return result;
}
As a requirement of an assignment for a data structures class, I have to get the following class hierarchy to work: http://www.brpreiss.com/books/opus4/
The source code is also provided, and right now I am simply trying to get stuff to compile. This has required re-organizing class definitions into their respective header files, moving template implementation into .inc files, and finishing bits of code which were not implemented. I have been making progress, but I am stuck on the following error (compiled with VC++):
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Iterator::~Iterator(void)" (??1Iterator##UAE#XZ) referenced in function "public: virtual __thiscall NullIterator::~NullIterator(void)" (??1NullIterator##UAE#XZ)
I have tried all of the usual solutions (eliminating redundant include statements, cleaning the project and recompiling, etc.) and am not sure where to go. As was previously discussed, the consensus here seems to be that this code-base is really poorly designed. Nonetheless, the requirement of this assignment is to get this code working, and I am into deep to scrap it all together and start from scratch. If it
Here is the iterator class definition in iterator.h
#ifndef ITERATOR_H
#define ITERATOR_H
#include "object.h"
class Iterator
{
public:
virtual ~Iterator ();
virtual void Reset () = 0;
virtual bool IsDone () const = 0;
virtual Object& operator * () const = 0;
virtual void operator ++ () = 0;
};
class NullIterator : public Iterator
{
public:
NullIterator () {}
void Reset () {}
bool IsDone () const { return true; }
Object& operator * () const { return NullObject::Instance(); }
void operator ++ () {}
};
#endif
These are all of the other header files which are associated with iterator:
#ifndef CONTAINER_H
#define CONTAINER_H
#include "object.h"
#include "visitor.h"
#include "iterator.h"
#include "ownership.h"
class Container : public virtual Object, public virtual Ownership
{
protected:
unsigned int count;
Container () : count(0) {}
public:
virtual unsigned int Count () const { return count; }
virtual bool IsEmpty () const { return Count () == 0; }
virtual bool IsFull () const { return false; }
//virtual HashValue Hash () const;
virtual void Put (ostream&) const;
virtual Iterator& NewIterator () const { return *new NullIterator (); }
virtual void Purge () = 0;
virtual void Accept (Visitor&) const = 0;
};
#endif
Stack and Queue also inherit from Container, but it appears only stack makes use of Iterator:
#ifndef STACK_H
#define STACK_H
#include "linkList.h"
#include "container.h"
class Stack : public virtual Container
{
public:
virtual Object& Top () const = 0;
virtual void Push (Object&) = 0;
virtual Object& Pop () = 0;
};
class StackAsLinkedList : public Stack
{
LinkedList<Object*> list;
class Iter;
public:
StackAsLinkedList () : list() {}
~StackAsLinkedList() { Purge(); }
//
// Push, Pop and Top
//
void Push(Object& object);
Object& Pop() override;
Object& Top() const override;
int CompareTo(Object const& obj) const;
//
// purge elements from, and accept elements onto, the list
//
void Purge();
void Accept (Visitor&) const;
friend class Iter;
};
class StackAsLinkedList::Iter : public Iterator
{
StackAsLinkedList const& stack;
ListElement<Object*> const* position;
public:
Iter (StackAsLinkedList const& _stack) : stack(_stack) { Reset(); }
//
// determine whether iterator is pointing at null
//
bool IsDone() const { return position == 0; }
//
// overloaded dereference and increment operator
//
Object& operator*() const;
void operator++();
void Reset() { position = stack.list.Head(); }
};
#endif
If anyone has some insight, it would be much appreciated. I have been trying to solve this one error for the last few hours and haven't made any progress!
The error is a linking error which tells you that you have not provided a definition for the destructor:
virtual ~Iterator();
Which is being called through:
NullIterator::~NullIterator(void)
because NullIterator derives from Iterator class.
Solution is You should provide the definition for the Base class destructor.