I have a function declared in my base class and specified as virtual, I am trying to re-declare it in a derived class but I'm getting a multiple declaration error.
Anyone know if I'm missing something here?
class Field {
public:
virtual void display() = 0;
virtual int edit() = 0;
virtual bool editable() const = 0;
virtual void *data() = 0;
virtual Field *clone() const = 0;
};
class LField : public Field {
int rowNum;
int colNum;
int width;
char *val;
bool canEdit;
int index;
public:
virtual void *data() { return val; }
};
class IVField : public LField {
void (*ptrFunc)(void *);
bool (*ptrValid)(int &);
int *data;
public:
void* data() {
return data;
}
};
class DVField : public LField {
int decimalsToDisplay;
double *data;
void (*ptrFunc)(void *);
bool (*ptrValid)(double&);
public:
void* data() {
return data;
}
};
You have a function named data and a member variable named data in the same class. That's not allowed. Pick a different name for your member variable.
You also appear to be re-declaring many member variables. That's probably not what you want to do. If you want to declare them in the base class and use them in descendants, then they should probably have protected visibility. The default visibility for classes, when you haven't specified any other, is private, which means, for example, that descendants of IVField (such as DVField) cannot access the ptrFunc variable you declared in IVField. The ptrFun variable you declared in DVField has absolutely no relation to the one declared in the parent class. Make the IVField one protected, and then descendants don't need to duplicate the declaration for themselves.
You're also going to have to implement all those other abstract methods before the compiler will allow you to instantiate any of those classes.
Related
Im trying to define an interface (abstract class) which will "automatically" register any instance created to a global map, where the key is an uint8_t and the value is a pointer to the interface class.
All classes that will implement this interface already have a method to retrieve a unique id with a getId() method. I've tried the following approach, but it get warningsn when I use the (then) abstract method getId() in the c'tor and d'tor of the interface, which I can understand. But I get an error when I try to create an instance of LightZoneImpl because it has no implementation of getId().
What am I doing wrong here?
Note: This is a simplified example of the real thing, lots of other classes etc involved in the real thing.
class ILightZone; // forward
typedef std::map<uint8_t, ILightZone*> LightZoneMap;
extern LightZoneMap lightZoneMap;
/**
* #brief Interface defining a Lightzone operating node, automatically (de-)registered in the lightZoneMap
*
*/
class ILightZone {
public:
ILightZone() {
lightZoneMap[getId()] = this; // <== warning: pure virtual function called from c'tor
}
virtual ~ILightZone() {
lightZoneMap.erase(getId()); // <== warning: pure virtual function called from d'tor
}
virtual const uint8_t getId() const = 0;
virtual void setLightOn() = 0;
virtual void setLightOff() = 0;
virtual bool isLightOn() = 0;
virtual void setIntensity(const uint8_t percentage) = 0;
virtual uint8_t getIntensity() = 0;
};
class BaseNode {
public:
BaseNode(uint8_t nodeId) : nodeId(nodeId) {};
virtual ~BaseNode() {};
virtual const uint8_t getid() const { return nodeId; };
private:
uint8_t nodeId;
};
class LightZoneImpl : public ILightZone, public BaseNode {
public:
LightZoneImpl() {};
virtual ~LightZoneImpl() {};
using BaseNode::getId;
void setLightOn() override { /* implementation */};
void setLightOff() override { /* implementation */};
bool isLightOn() override { return false; };
void setIntensity(const uint8_t percentage) override { /* implementation */ };
uint8_t getIntensity() override { return 0; };
}
LightZoneImpl zone{12}; // <= error: cannot declare variable 'zone' to be of abstract type LightZoneImpl
Note2: The example below is modified to show the solution suggested below
class ILightZone; // forward
typedef std::map<uint8_t, ILightZone*> LightZoneMap;
extern LightZoneMap lightZoneMap;
/**
* #brief Interface defining a Lightzone operating node, automatically (de-)registered in the lightZoneMap
*
*/
class ILightZone {
public:
ILightZone(uint8_t nodeId) : nodeId(nodeId) {
lightZoneMap[nodeId] = this;
}
virtual ~ILightZone() {
lightZoneMap.erase(nodeId);
}
const uint8_t getId() const { return nodeId; };
virtual void setLightOn() = 0;
virtual void setLightOff() = 0;
virtual bool isLightOn() = 0;
virtual void setIntensity(const uint8_t percentage) = 0;
virtual uint8_t getIntensity() = 0;
private:
uint8_t nodeId;
};
class BaseNode {
public:
BaseNode(uint8_t nodeId) : nodeId(nodeId) {};
virtual ~BaseNode() {};
virtual const uint8_t getid() const { return nodeId; };
private:
uint8_t nodeId;
};
class LightZoneImpl : public ILightZone, public BaseNode {
public:
LightZoneImpl(uint8_t nodeId) : ILightZone(nodeId), BaseNode(nodeId) {};
virtual ~LightZoneImpl() {};
using BaseNode::getId;
void setLightOn() override { /* implementation */};
void setLightOff() override { /* implementation */};
bool isLightOn() override { return false; };
void setIntensity(const uint8_t percentage) override { /* implementation */ };
uint8_t getIntensity() override { return 0; };
}
LightZoneImpl zone{12}; // <= error: cannot declare variable 'zone' to be of abstract type LightZoneImpl
Virtual dispatch doesn't start using the derived-class function overrides until construction completes: at the time you hoped getId() would use the derived-class override, only part of the abstract base class had been constructed - the derived object didn't exist to have its functions called.
You can have derived classes or a factory function provide the id and operate on the map.
Elaboration/example as requested by Bascy...
You can think of the objects involved here as being a LightZoneImpl object in which an ILightZone base class object is embedded. To construct the LightZoneImpl, the base class must be constructed first... and while that's happening the derived-class object doesn't exist or have the invariants (guarantees about state) that the derived-class constructor sets up, so it's premature to call any derived class overrides of the virtual functions. For that reason, the C++ Standard says the base class virtual function implementations should continue to be called, but if they're unavailable because the function is pure virtual your program will terminate.
To work around this, you can do what Mooing Duck suggests in his comment, and have the derived class specify an id that the base class saves. That's probably best. You could also have a factory function that creates light zones, letting the derived-class constructor pass it down to the base class for storage/use:
std::unique_ptr<LightZoneImpl> lz_factory() {
static int id_ = 0;
if (id_ > 255)
throw std::runtime_error("too many lightzones");
return std::make_unique<LightZoneImpl>(id_++);
}
You'd then want to make the light zone constructors private and make the factory a friend.
I created a base class with one protected method returning the id of the object. I want only derived classes to be able to query this id on other derived classes, but hide it from classes outside the inheritance hierarchy.
class Identifiable {
public:
virtual ~Identifiable() = default;
protected:
virtual auto getId() const noexcept -> unsigned = 0;
};
class ObjectA: public Identifiable {
protected:
auto getId() const noexcept -> unsigned override { return 0; }
};
class SpecificObjectA: public ObjectA {
protected:
using ObjectA::getId;
};
class ObjectB: Identifiable {
public:
explicit ObjectB(const SpecificObjectA& objectA) {
objectA.getId(); // error C2248: 'SpecificObjectA::getId': cannot access protected member declared in class 'SpecificObjectA'
}
protected:
auto getId() const noexcept -> unsigned override { return 0; }
};
Is there any way to make it work other than adding a next method?
auto getId(const Identifiable& identifiable) const noexcept -> unsigned {
return getId();
}
No, that's not possible. Otherwise, it would let you use virtual protected members from any class by creating another class derived from the base class in question.
You cannot do as intended, but you might try this:
class Identifiable
{
public:
virtual ~Identifiable() = default;
protected:
virtual unsigned getId() const noexcept = 0;
static unsigned int getId(Identifiable const& other)
{
return other.getId();
}
};
Now you can do:
explicit ObjectB(const SpecificObjectA& objectA)
{
Identifiable::getId(objectA);
}
I personally wouldn't use trailing return type unless you really need it, otherwise it just reduces readability.
I'm trying to create an abstract base class that I intend the derive classes to implement a vector<int> member. My problem is if I try to do this:
class BaseClass
{
public:
virtual int GetCount() = 0;
virtual vector<int> ChildData;
}
class CID1 : public BaseClass
{
public:
int GetCount()
{
return 3;
}
//and so on.....
}
I get
'virtual' not permitted on data declarations
'virtual' is not allowed.
What I'm trying to do is:
make my child classes override the vector because they will contain different number of the vector elements
To be able to loop through an array of BaseClass* containing addresses of declared child classes, and the second loop for the data of the vectors.
Am I doing it the right way? Thanks in advance!
Note: I have no prior experience in coding in C++.
Am I doing it the right way?
No.
As the compiler lets you know, you can have virtual member functions but not virtual member variables.
Change the member variables to member functions and make sure you implement them properly in the derived classes.
class BaseClass
{
public:
virtual int GetCount() = 0;
virtual vector<int>& GetChildData() = 0;
}
class CID1 : public BaseClass
{
public:
// virtual keyword is not necessary here
// but helps with understanding code.
virtual int GetCount()
{
return 3;
}
virtual vector<int>& GetChildData()
{
return data;
}
private:
vector<int> data;
}
Consider the following code:
#include <stdio.h>
#include <iostream>
/// Header-file
class Base {
public:
virtual void do_something() const =0;
int GetAttrib () const {return constattribute_;};
static const int constattribute_;
};
typedef Base* Derived_Ptr; //<< adress derived classes by their base-class ptr; so no templates for Base
class DerivedA : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedA class instances and their derivatives
void do_something() const {};
};
class DerivedB : public Base {
// static const int constattribute_; //<< change this static attribute for all DerivedB class instances and their derivatives
void do_something() const {};
};
/// CC-file
using namespace std;
const int Base::constattribute_(0);
const int DerivedA::constattribute_(1); //<<error: no such variable 'constattribute' in class DerivedA
const int DerivedB::constattribute_(2); //<<error: no such variable 'constattribute' in class DerivedB
int main(void) {
Derived_Ptr derivedA = new DerivedA();
Derived_Ptr derivedB = new DerivedB();
cout << derivedA->GetAttrib() << derivedB->GetAttrib() <<endl;
return 0;
};
The intend being that i have some abstract interface (Base) which defines also a variable, which should be present for all derived classes, and is retrievable. All flavours of subclasses should be forced to/able to redefine their specific value for this variable, at best during class declaration (the values are known at the time the class is declared after all).
I want to achieve code, not altering the main()-program so that the output is '12' and not as of now (uncommenting current lines in the code) '00' (Doing so shadows the fields from base class).
I tried to look into the matter, and there are different paths for solutions, many of which however go contrary to my intuition:
1. Some follow the CRTP pattern, which is however impossible if I want to address my subclasses by their base-ptr in main.
2. Other solutions require to virtualize the 'GetAttrib()' function for every derived instance., which is cumbersome, and action of modifying the attribute is masked within a function definition.
3. A third possibility is to remove the static pattern and have the 'constattribute_' field as a regular member, which however forces me to drag it through all constructors as a parameter.
I am quite sure that there must be some smarter way to do this. Any hints are appreciated.
Using CRTP may get you what you want, assuming you don't have to access GetAttr() through Base* and can leave without constattribute_ in Base itself. Just follow the rule that every programming problem can be solved by entering another level of indirection, which I did below:
class Base {
public:
virtual void do_something() const = 0;
virtual ~Base() // should define it as you are using Base*
{
}
};
typedef Base* Derived_Ptr;
template<class T>
class BaseConstAttr : public Base
{
public:
int GetAttrib () const
{
return(constattribute_);
};
static const int constattribute_;
};
class DerivedA : public BaseConstAttr<DerivedA>
{
public:
void do_something() const
{
};
};
class DerivedB : public BaseConstAttr<DerivedB>
{
public:
void do_something() const
{
};
};
template<> const int BaseConstAttr<DerivedA>::constattribute_(1);
template<> const int BaseConstAttr<DerivedB>::constattribute_(2);
If you need GettAttr from top to bottom of the inheritance tree you can modify the above code a bit, but this will cost you making GetAttr virtual (but still one implementation only):
class Base {
public:
virtual void do_something() const = 0;
virtual int GetAttrib () const = 0;
virtual ~Base() // should define it as you are using Base*
{
}
};
typedef Base* Derived_Ptr;
template<class T>
class BaseConstAttr : public Base
{
public:
int GetAttrib () const
{
return(constattribute_);
};
static const int constattribute_;
};
class DerivedA : public BaseConstAttr<DerivedA>
{
public:
void do_something() const
{
};
};
class DerivedB : public BaseConstAttr<DerivedB>
{
public:
void do_something() const
{
};
};
template<> const int BaseConstAttr<DerivedA>::constattribute_(1);
template<> const int BaseConstAttr<DerivedB>::constattribute_(2);
Please note that I don't know how well (or bad) it will behave with deep inheritance tree (ie. when inheriting from DerivedA and/or DerivedB). In this case I would probably remove BaseConstAttr from inheritance tree right below Base and would try to inject it between most derived class and its predecessor or use multiple inheritance.
What you are requesting requires virtual dispatch somewhere, because you don't know the type of the object you are dealing with until runtime. The purpose of virtual dispatch is to solve exactly the problem you are facing.
The simplest solution is what you have given as number 2: make GetAttrib() virtual, and implement it on each derived class where you introduce a shadowing constattribute_.
static variable in base class is single instance hence it will be reflected same in derived class.
You can make same static member variable in derived class with specific different value you want. Now make getter member function of static variable in Base class as virtual and overload it in derived class which returns is static instance value.
I have update your code to work it, please check ..
#include <iostream>
using namespace std;
class Base {
public:
static const int constattribute_;
virtual void do_something() const =0;
virtual int GetAttrib () const {return constattribute_;};
};
typedef Base* Derived_Ptr; //<< adress derived classes by their base-class ptr; so no templates for Base
class DerivedA : public Base {
static const int constattribute_; //<< change this static attribute for all DerivedA class instances and their derivatives
void do_something() const {};
int GetAttrib () const {return constattribute_;};
};
class DerivedB : public Base {
static const int constattribute_; //<< change this static attribute for all DerivedB class instances and their derivatives
void do_something() const {};
int GetAttrib () const {return constattribute_;};
};
const int Base::constattribute_(0);
const int DerivedA::constattribute_(1); //<<error: no such variable 'constattribute' in class DerivedA
const int DerivedB::constattribute_(2); //<<error: no such variable 'constattribute' in class DerivedB
int main(void) {
Derived_Ptr derivedA = new DerivedA();
Derived_Ptr derivedB = new DerivedB();
cout << derivedA->GetAttrib() << derivedB->GetAttrib() <<endl;
return 0;
};
You should get desired output.
Note : Remember all member variables and func in derived class are private.
I want to expose only the CreateSort() for the client. it was to create an object for the implementation of the sort class i.e imSort then return it to the client.but the compiler says that it cannot create an object of an abstract class eventhough all the functions have been defined in the derived class.
/////sort.h
class __declspec(dllexport) Sort {
public:
virtual int* BSort() const=0;
virtual void getdata() const=0;
};
extern "C" Sort *CreateSort();
/////imSort.h
#include "Sort.h"
class imSort : public Sort{
private:
int i,j,num;
int temp;
int *a;
public:
imSort();
int* BSort();
void getdata();
}
/////imSort.cpp
#include <iostream>
#include "imSort.h"
Sort *CreateSort()
{
return new imSort(); /* object of abstract class type "imSort" is not allowed: */
}
imSort::imSort()
{
i=j=num=0;
*a=0;
}
void imSort::getdata()
{
std::cout<<"\nEnter the number of elements..";
std::cin>>num;
for(i=0;i<num;i++)
{
std::cin>>*a;
*(a++);
}
}
int* imSort::BSort()
{
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
{
if(*(a+i)<*(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
return a;
}
Your base class has:
virtual int* BSort() const=0;
virtual void getdata() const=0;
But your derived class has:
int* BSort();
void getdata();
Repeating the virtual keyword is optional, but without the const these are separate functions, unrelated to the virtual base functions.
As a result, those pure virtual functions remain un-overridden in the derived class, and so imSort (silly name for a type if you ask me) is still abstract.
Your fixed derived class definition is thus:
class imSort : public Sort {
private:
int i, j, num;
int temp;
int* a;
public:
imSort();
int* BSort() const; // <--- const
void getdata() const; // <--- const
}; // <--- ;
(Notice how indentation improves the legibility of your code? And you forgot the ; at the end of your class definition.)
Please write a fully-formed question next time, and reduce your problem to a minimal testcase.
If the virtual functions in the abstract Sort class are declared const, so should the implementations in the imSort class, but they are not.
So just add const here and there...