I am new to C++. Could you pls help me get rid of the errors:
error C2259: 'MinHeap' : cannot instantiate abstract class
IntelliSense: return type is not identical to nor covariant with return type "const int &" of overridden virtual function function
template <class T> class DataStructure {
public:
virtual ~DataStructure () {}
virtual bool IsEmpty () const = 0;
virtual void Push(const T&) = 0;
virtual const T& Top() const = 0;
virtual void Pop () = 0;
};
class MinHeap : public DataStructure<int>
{
private:
std::vector<int> A;
public:
bool IsEmpty() const
{
..
}
int Top() const
{
..
}
void Push(int item)
{
...
}
void Pop()
{
..
}
};
The problem is with const T& Top() vs. int Top(). The latter is different from the former, and thus not an override. Instead it hides the base class function. You need to return exactly the same as in the base class version: const int& Top() const.
The same problem exists for Push(), BTW.
try
class MinHeap : public DataStructure<int>
{
private:
std::vector<int> A;
public:
bool IsEmpty() const
{
..
}
const int& Top() const
{
..
}
void Push(const int& item)
{
...
}
void Pop()
{
..
}
};
Note that it is using const int& instead of int for Top and Push
Related
Can someone , for the love of god please help me. I've been stuck on this for hours
This is the "father" .h file , which i am NOTallowed to alter at all
//------------ Declarations for List traits used in Test1 in main.cpp
template <typename T>
class ListTraits
{
public:
virtual unsigned int size() = 0;
virtual ListTraits& insert(const T& item) = 0;
virtual void print() = 0;
};
//------------ Declarations for List traits used in Test2 in main.cpp
template <typename T>
class ListTraitsExtended
{
public:
virtual const T* getCurrentElement() const = 0;
virtual void advance() = 0;
virtual void rewind() = 0;
};
And this is the "child" .h file ,
#include "ListTraits.h"
#include <array>
template <typename T>
class List : public ListTraits<T>
{
protected:
std::array<int, 7> data;
public:
unsigned int size() override{
return 0;
}
void print() override {
}
List & insert(const T& item) override {
return this;
}
};
Where I always get this error:
'return' cannot convert from List * to List &
If I do
return *this;
I still get errors...
What exactly am I doing wrong?I cant wrap my head around it
List& List::insert(const T &item) is declared to return a reference to List. this is a pointer to List. To return a reference to List, return *this:
List & insert(const T& item) override {
return *this;
}
Edit: I had copied the base template for the statisticscompiler class from another solution, and it seems I somehow had ended up editing the original one while including the new one (that hadn't been edited yet) which is what led to the errors. So a tale of caution against copy pasting code into another file with the same name.
I will just post the header files because I think that's what matters here, let me know if otherwise and I can post the cpp. I have one base class that collects statistics from a process, with two derived classes and then a class that let's me use several statistics classes at once
Statistics.h
#pragma once
#define STATISTIC_H
#include <vector>
#include "Arrays.h"
class Statistics
{
public:
Statistics() {}
virtual void DumpOnePath(MJArray results) = 0;
virtual std::vector<std::vector<double>> GetResultsSoFar() const = 0;
virtual Statistics* clone() const = 0;
virtual ~Statistics() {}
private:
};
class StatisticsMean : public Statistics
{
public:
StatisticsMean();
virtual void DumpOnePath(MJArray results) ;
virtual std::vector<std::vector<double>> GetResultsSoFar() const ;
virtual Statistics* clone() const;
private:
MJArray RunningSums;
unsigned long PathsDone;
};
class StatisticsQuantiles : public Statistics
{
public:
StatisticsQuantiles(double p_lower_, double p_upper_);
virtual void DumpOnePath(MJArray results);
virtual std::vector<std::vector<double>> GetResultsSoFar() const;
virtual Statistics* clone() const;
private:
std::vector<MJArray> RunningResults;
unsigned long PathsDone;
double p_lower;
double p_upper;
};
StatisticsCompiler.h
#pragma once
#define STATISTICS_COMPILER_H
#include "statistics.h"
#include "wrapper.h"
class StatisticsCompiler : public Statistics
{
public:
StatisticsCompiler(const std::vector <Wrapper<Statistics>>& Inner_);
virtual Statistics* clone() const;
virtual void DumpOnePath(MJArray results);
virtual std::vector<std::vector<double>> GetResultsSoFar() const;
private:
std::vector <Wrapper<Statistics>> Inner;
};
And in my main class I'm trying to do this:
StatisticsMean meanGatherer;
StatisticsQuantiles quantileGatherer(p_lower, p_upper);
vector<Wrapper<Statistics>> gathererArray{ meanGatherer, quantileGatherer};
StatisticsCompiler meanAndQuantileGatherer(gathererArray);
Which gives an error no the last line complaining that "No instance of constructor matches the argument list. Argument types are:
(std::vector<Wrapper<Statistics>, std::allocator<Wrapper<Statistics>>>)."
But isn't that exactly what I've defined the constructor to accept? at least the first part, I don't know what
std::allocator<Wrapper<Statistics>>
means.
Wrapper.h in case needed. It does the memory handling
#pragma once
#define WRAPPER_H
template< class T>
class Wrapper
{
public:
Wrapper()
{
DataPtr = 0;
}
Wrapper(const T& inner)
{
DataPtr = inner.clone();
}
~Wrapper()
{
if (DataPtr != 0)
delete DataPtr;
}
Wrapper(const Wrapper<T>& original)
{
if (original.DataPtr != 0)
DataPtr = original.DataPtr->clone();
else
DataPtr = 0;
}
Wrapper& operator=(const Wrapper<T>& original)
{
if (this != &original)
{
if (DataPtr != 0) {
delete DataPtr;
}
DataPtr = (original.DataPtr != 0) ? original.DataPtr->clone() : 0;
}
return *this;
}
T& operator*()
{
return *DataPtr;
}
const T& operator*() const
{
return *DataPtr;
}
const T* const operator->() const
{
return DataPtr;
}
T* operator->()
{
return DataPtr;
}
private:
T* DataPtr;
};
I've been assigned the following template:
#include <map>
template <typename T>
class Catalog {
struct Item {
//..
};
std::map<int, Item*> items;
public:
Catalog(void);
Catalog(const Catalog&);
~Catalog(void);
bool IsEmpty(void) const;
int Size() const;
void Add(T*);
T* Remove(T*);
T* Find(T*);
typedef void (T::*pFunc) (const T&);
void Inspection(pFunc) const;
};
Next, there is an abstract Product class and three subclasses:
class Product {
protected:
unsigned int _id;
string _name;
public:
Product(const int& id, const string& name) : _id(id), _name(name) {};
virtual void Action(const Product& p) = 0;
virtual int hashCode() {
return _id*100;
};
unsigned int getId(void) const {return _id;};
string getName(void) const {return _name;};
};
class ProductA : public Product {
public:
ProductA(const int& id, const string& name) : Product(id, name) {};
virtual void Action(const Product& p) {
cout << "ahoj" << endl;
};
};
Finally, class ProductsCatalog that handles a Catalog instance:
class ProductsCatalog {
Catalog<Product> catalog;
public:
//..
void CatalogInspection(void) const {
catalog.Inspection(&Product::Action);
}
};
What I have trouble with is the Inspection method:
template <typename T> void Catalog<T>::Inspection(pFunc p) const {
for (std::map<int, Item*>::const_iterator it=items.begin(); it!=items.end(); ++it) {
it->second->Product->*p(*(it->second->Product));
}
};
I am getting the following error:
error C2064: term does not evaluate to a function taking 1 arguments
I've tried everything I could think of, without success. The following works as intended, but is obviously not abstract enough:
it->second->Product->Action(*it->second->Product);
Did you try
(it->second->Product->*p)(*(it->second->Product));
for calling the method?
The thread Calling C++ class methods via a function pointer seems to be related.
I have a template class, called Cell, here the definition:
template <class T>
class OneCell
{
.....
}
I have a cast operator from Cell to T, here
virtual operator const T() const
{
.....
}
Now i have derived class, called DCell, here
template <class T>
class DCell : public Cell<T>
{
.....
}
I need to override the Cell's cast operator (insert a little if), but after I need to call the Cell's cast operator. In other methods it's should be something like
virtual operator const T() const
{
if (...)
{
return Cell<T>::operator const T;
}
else throw ...
}
but i got a compiler error
error: argument of type 'const int (Cell::)()const' does not match 'const int'
What can I do?
Thank you, and sorry about my poor English.
You are missing parentheses, so the compiler thought you were trying to return the member function, not call it.
return Cell<T>::operator const T();
You're not actually calling the operator:
return Cell<T>::operator const T();
Full code:
template <class T>
class OneCell
{
public:
virtual operator const T() const
{
return T();
}
};
template <class T>
class DCell : public OneCell<T>
{
public:
virtual operator const T() const
{
cout << "operator called";
return OneCell<T>::operator const T();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
DCell<int> x;
int y = (int)x;
}
Consider this code with the implementations of Cell and DCell:
#include <iostream>
#include <exception>
template<class T>
class Cell
{
protected:
T cnt;
public:
Cell(const T& cnt = T()) : cnt(cnt){}
virtual operator const T() const { return cnt; }
};
bool test_bool = true;
template<class T>
class DCell : public Cell<T>
{
public:
DCell(const T& cnt = T()) : Cell<T>(cnt){}
virtual operator const T() const
{
if(test_bool)
{
return Cell<T>::operator const T(); // Here you had Cell<T>::operator const T;
} else {
throw std::exception();
}
}
};
int main()
{
DCell<int> cell(5);
std::cout << static_cast<int>(cell) << "\n"; // prints 5 (and a new line)
return 0;
}
Don't make the operator virtual. Instead, delegate to a protected virtual helper function.
template <class T>
class Cell
{
public:
operator const T() const { return cvt_T(); }
protected:
virtual const T cvt_T() const;
};
template <class T>
class DCell : public Cell<T>
{
const T cvt_T() const
{
if (...)
{
return Cell<T>::cvt_T();
}
else throw ...
}
};
This and other good practices can be learned from GotW, here is the section on virtual architecture.
Trying to initialize a class called StackAsLinkedList, which should be a derived class of the abstract class Stack (testing code which is available here: http://www.brpreiss.com/books/opus4/).
However, I get an error trying to instantiate this code in main():
StackAsLinkedList stack;
error C2259: 'StackAsLinkedList' : cannot instantiate abstract class
I am confused about this, because I thought StackAsLinkedList is defined as a derived class of Stack:
#ifndef STACK_H
#define STACK_H
#include "object.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();
Object& Top() 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++() const;
void Reset() { position = stack.list.Head(); }
};
#endif
The implementation:
#include "stack.h"
void StackAsLinkedList::Purge()
{
if ( IsOwner() )
{
ListElement<Object*> const* ptr;
for(ptr = list.Head(); ptr != 0; ptr = ptr->Next() )
delete ptr->Datum();
list.Purge();
count = 0;
}
}
void StackAsLinkedList::Push(Object& object)
{
list.Prepend(&object);
++count;
}
Object& StackAsLinkedList::Pop()
{
if(count == 0)
throw domain_error ("stack is empty");
Object& result = *list.First();
list.Extract(&result);
--count;
return result;
}
Object& StackAsLinkedList::Top() const
{
if(count == 0)
throw domain_error ("stack is empty");
return *list.First();
}
void StackAsLinkedList::Accept(Visitor& visitor) const
{
ListElement<Object*> const* ptr;
for(ptr = list.Head(); ptr != 0 && !visitor.IsDone(); ptr = ptr->Next())
visitor.Visit(*ptr->Datum());
}
class Container:
#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
EDIT: It appears that the compiler says that the CompareTo() method in Object is not implemented in any of the derived classes. However, this functionality is implemented in the derived class of Object called "Wrapper":
#ifndef WRAPPER_H
#define WRAPPER_H
#include "object.h"
template <class T>
class Wrapper : public Object
{
protected:
T datum;
int CompareTo (Object const&) const;
public:
Wrapper ();
Wrapper (T const&);
Wrapper& operator = (T const&);
operator T const& () const;
//HashValue Hash () const;
void Put (ostream&) const;
};
//
// typedefs for for Wrappers representing different primitive
// data types
//
typedef Wrapper <int> Int;
typedef Wrapper <char> Char;
typedef Wrapper <double> Double;
typedef Wrapper <std::string> String;
#include "wrapper.inc"
#endif
But Stack doesn't inherit from Wrapper - so I am guessing this means another CompareTo method needs to be implemented for Stack? Not sure how the original author got this to work (scratches head).
Since you've now explained you're trying to fix it, I suggest:
First step is to get it compiling, which you can do by adding a CompareTo(Object&) const member to StackAsLinkedList. You can use either dynamic_cast or the Visitor machinery to find out whether the object compared to is another collection.
Next, get rid of reference parameters in any case where the object will be stored by the callee and used after the function returns. And eradicate reference return types, where ownership is being transferred. You can either use pointers, or change the collection to pass-by-value (but don't pass-by-value if the collection should be polymorphic). You'd get:
class Stack : public virtual Container
{
public:
virtual Object& Top () const = 0; // short-term access to object, no ownership transfer, reference is ok here.
virtual void Push (Object*) = 0; // pointer kept, ownership transfer, use pointer
virtual Object* Pop () = 0; // ownership transfer (caller must delete), use pointer
};
Then, you should do something about the brokenness in the Visitor implementation. Right now, Accept always calls Visit(Object&) regardless of the dynamic type. You'd need to call a virtual Accept function on each individual member, in order to let the Visitor perform correctly on polymorphic collections.
We're well on the way to scrapping the design by this point.