instantiating a class with abstract functions | Member | | Join Date: Jun 2007
Posts: 71
| | |
So here is how I can summarize my situation. In function.hpp I have the following function:
[HTML]virtual void accept(FunctionVisitor<D,R,I>& v) = 0;[/HTML]
And in detfunction.hpp I have the following:
[HTML]template <class D, class R> class DFunction: public Function <D, R> {...}
template <class D, class R> class AtomicDFunction : public DFunction<D, R>
{...}
template <class D1, class D2, class R> class TwoVarDFunction : public AtomicDFunction<curious2007::pair<D1, D2>, R> {...}[/HTML]
Now, in main.cpp
I have the following:
[HTML]int addition(const curious2007::pair<double, double>& p)
{
return (int) (p.first+p.second);
}
int main()
{
TwoVarDFunction<double, double, int> myfun;
myfun.function(addition);
cout << "Call addition: " <<myfun.calculate(1.0, 2.0) << endl;
}
[/HTML]
and I get the following error:
error C2259: 'TwoVarDFunction<D1,D2,R>' : cannot instantiate abstract class
1> with
1> [
1> D1=double,
1> D2=double,
1> R=int
1> ]
1> due to following members:
1> 'void Function<D,R>::accept(FunctionVisitor<D,R,I> &)' : is abstract
1> with
1> [
1> D=curious2007::pair<double,double>,
1> R=int,
1> I=int
1> ]
1> see declaration of 'Function<D,R>::accept'
1> with
1> [
1> D=curious2007::pair<double,double>,
1> R=int
1> ]
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,202
| | | re: instantiating a class with abstract functions
You can not instantiate abstract classes, that is a class has has or inherits a pure virtual function.
A pure virtual function is a virtual function that is declared in a class without being defined anywhere by putting = 0 at the end of the declaration.
Not being able to instantiate abstract classes makes sense in light of this, if you could you would end up with a NULL vtable entry leaving you with a problem if the function that table entry was for was called.
Your declaration of accept in function.hpp is declaring a pure virtual function therefore the class Function is abstract.
You then derive 3 other classes from function -
template <class D, class R> class DFunction: public Function <D, R> {...}
-
-
template <class D, class R> class AtomicDFunction : public DFunction<D, R> {...}
-
-
template <class D1, class D2, class R> class TwoVarDFunction : public AtomicDFunction<curious2007::pair<D1, D2>, R> {...}
-
If these classes do not override and provide a definition of accept then they too will be abstract.
When you try in instantiate them the compiler will give an error.
| | Member | | Join Date: Jun 2007
Posts: 71
| | | re: instantiating a class with abstract functions
Ok, this makes sense. I was not sure if this accept function was useful at all so I got rid of its decleration. Now I have the following kind of errors:
1>Compiling manifest to resources...
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall TwoVarDFunction<double,double,int>::~TwoVarDFuncti on<double,double,int>(void)" (??1?$TwoVarDFunction@NNH@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall TwoVarDFunction<double,double,int>::calculate(doub le const &,double const &)const " (?calculate@?$TwoVarDFunction@NNH@@QBEHABN0@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall AtomicDFunction<class curious2007::pair<double,double>,int>::function(in t (__cdecl*)(class curious2007::pair<double,double> const &))" (?function@?$AtomicDFunction@V?$pair@NN@curious200 7@@H@@QAEXP6AHABV?$pair@NN@curious2007@@@Z@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoVarDFunction<double,double,int>::TwoVarDFunctio n<double,double,int>(void)" (??0?$TwoVarDFunction@NNH@@QAE@XZ) referenced in function _main
1>C:\Users\admin\Documents\Visual Studio 2005\Projects\vec\Debug\vec.exe : fatal error LNK1120: 4 unresolved externals
I have no clue about what this is trying to say.
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,202
| | | re: instantiating a class with abstract functions
You have functions, constructors and destructors declared for classes you have declared that have no defined body
e.g. -
template <class D1, class D2, class R> class TwoVarDFunction : public AtomicDFunction<curious2007::pair<D1, D2>, R>
-
{
-
public:
-
TwoVarDFunction(); // Constructor declared but not defined
-
}
-
| | Member | | Join Date: Jun 2007
Posts: 71
| | | re: instantiating a class with abstract functions
Ok, you were right. I have another question though. My problem was that the definitions were in a cpp file and I have forgotten to include that. However, a couple of people have told me that there is no need to include cpp files as long as I include hpp files with the same name. If this is correct than why do I have to include cpp files explicitly here?
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,202
| | | re: instantiating a class with abstract functions
You should never include cpp files, that is bad form. What you must do is compile all the cpp files to object files and link them all the object files together to create the executable.
However for template classes this is not true. For a template class you should put all the declarations on definitions into 1 file or if you put them in 2 files #inlcude the file of definitions from the file of declarations.
Additionally I use alternate extensions for files containing templates (I use cxx and hxx). This is because a template is not a definition, it is a pattern that the compiler uses to create a definition as such the entire template must be visible when the compiler compiles a piece of code using the template.
| | Member | | Join Date: Jun 2007
Posts: 71
| | | re: instantiating a class with abstract functions
thanks, this was very helpful.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|