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> ]
6 2286 Banfa 9,065
Expert Mod 8TB
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.
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.
Banfa 9,065
Expert Mod 8TB
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
-
}
-
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?
Banfa 9,065
Expert Mod 8TB
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.
thanks, this was very helpful.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Claire |
last post by:
I have a base abstract class A which contains several abstract functions
I then have an abstract class B (derived from A) which overrides a small
subset of A's abstract functions
Finally I...
|
by: active |
last post by:
Because of an example I followed I've been instantiating Image objects. Now
I notice that the documentation says the Image class is an abstract base
class.
Things seem to be working! Is the...
|
by: scottt |
last post by:
hi,
I am having a little problem passing in reference of my calling class (in my
..exe)into a DLL. Both programs are C# and what I am trying to do is pass a
reference to my one class into a DLL...
|
by: Anders Borum |
last post by:
Hello!
Whilst refactoring an application, I was looking at optimizing a
ModelFactory with generics. Unfortunately, the business objects created by
the ModelFactory doesn't provide public...
|
by: ankitjain.bvcoe |
last post by:
Hi i have the following problem in my design ::::
i want to define an abstract class LogBuffer and derive two singleton
classes from it i.e
AlarmBuffer and FireWallBuffer.For this my design is...
|
by: MattWilson.6185 |
last post by:
Hi, I'm trying to find out if something is possible, I have a few
diffrent lists that I add objects to and I would like to be able to
have a wrapper class that won't affect the internal object, for...
|
by: Sanders Kaufman |
last post by:
I'm wondering if I'm doing this right, as far as using another class
object as a PHP class property.
class my_baseclass {
var $Database;
var $ErrorMessage;
var $TableName;
var $RecordSet;...
|
by: sugard |
last post by:
I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class.
The task to do is gathering data from a textfile where data is in this format..
P...
|
by: dascandy |
last post by:
Hi,
For a project I'm working on I'm kind-of-hacking my way around
deriving a class from an interface or such to create a mock, but
instead creating the mock directly. It is usable as the...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |