I created a small sample which demonstrates the problem. I sure don't understand whats going wrong here. Using Visual Studio 2010.
The classes are (very loosely) modeled after MFC
because this is what I need to use it for.
Basically I wanted to create a panel class, which can contain other panels or controls so I added a component class which keeps track of the id and the parent, and the panel class which acts as a container for it's components.
Now what I don't understand is why I get this compiler error and why I get it only when I use a control but not with a subpanel.
#include <iostream>
#include <algorithm>
#include <vector>
class CWnd
{
public:
CWnd(CWnd *pParent = NULL, int nId = -1)
{
mId = nId;
mParent = pParent;
mClassname = NULL;
}
void setId(int nId) { mId = nId; }
int getId(void) const { return mId; }
void setParent(CWnd *pParent) { mParent = pParent; }
CWnd *getParent(void) const { return mParent; }
bool create(const char *pClassname, int nId, CWnd *pParent)
{
mId = nId;
mParent = pParent;
mClassname = pClassname;
return true;
}
private:
int mId;
CWnd *mParent;
const char *mClassname;
};
class Ctrl : public CWnd
{
public:
Ctrl(CWnd *pParent, int nId = -1)
: CWnd(pParent, nId)
{
}
};
class Dialog : public CWnd
{
public:
Dialog(CWnd *pParent, int nId = -1)
: CWnd(pParent, nId)
{
}
bool create(int nId, CWnd *pParent)
{
CWnd::create("dialog", nId, pParent);
return true;
}
};
class View : public CWnd
{
public:
View(CWnd *pParent = NULL)
: CWnd(pParent)
{
}
};
template <typename W>
class Component : public W
{
public:
Component(CWnd *pParent = NULL)
: W(pParent)
{
mId = -1;
mParent = pParent;
}
virtual bool create(CWnd *pParent = NULL)
{
if(pParent)
mParent = pParent;
W::setParent(mParent);
W::setId(mId);
return true;
}
private:
int mId;
CWnd *mParent;
};
class Panel : public Component<Dialog>
{
public:
Panel(CWnd *pParent = NULL)
: Component(pParent)
{
}
virtual bool create(CWnd *pParent = NULL)
{
if(pParent != NULL)
setParent(pParent);
Dialog::create(getId(), pParent);
for(Components::iterator it = mComponents.begin(); it != mComponents.end(); ++it)
{
if(!(*it)->create(this))
return false;
}
return true;
}
void addComponent(Component *pComponent)
{
if(std::find(mComponents.begin(), mComponents.end(), pComponent) == mComponents.end())
mComponents.push_back(pComponent);
}
void removeComponent(Component *pComponent)
{
Components::iterator pos = std::find(mComponents.begin(), mComponents.end(), pComponent);
if(pos != mComponents.end())
mComponents.erase(pos);
}
protected:
typedef std::vector<Component *> Components;
private:
Components mComponents;
};
int main()
{
View v;
Panel d;
Panel p;
Component<Ctrl> listbox;
Component<Ctrl> tab;
// these are generating the error
d.addComponent(&listbox);
d.addComponent(&tab);
// only this one works.
d.addComponent(&p);
std::cout << "\nDone! Press any key..." << std::endl;
std::cin.ignore();
return 0;
}
The error message that I get is:
'Panel::addComponent' : cannot convert parameter 1 from 'Component<W> *' to 'Component<W> *'