473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

instantiating a class with abstract functions

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> ]
Jul 26 '07 #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

Expand|Select|Wrap|Line Numbers
  1. template <class D, class R> class DFunction: public Function <D, R> {...}
  2.  
  3. template <class D, class R> class AtomicDFunction : public DFunction<D, R> {...}
  4.  
  5. template <class D1, class D2, class R> class TwoVarDFunction : public AtomicDFunction<curious2007::pair<D1, D2>, R> {...}
  6.  
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.
Jul 26 '07 #2
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.
Jul 26 '07 #3
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.

Expand|Select|Wrap|Line Numbers
  1. template <class D1, class D2, class R> class TwoVarDFunction : public AtomicDFunction<curious2007::pair<D1, D2>, R> 
  2. {
  3. public:
  4.     TwoVarDFunction();  // Constructor declared but not defined
  5. }
  6.  
Jul 26 '07 #4
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?
Jul 27 '07 #5
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.
Jul 27 '07 #6
thanks, this was very helpful.
Jul 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
2
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...
12
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...
5
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...
7
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...
6
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...
61
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;...
19
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...
4
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
0
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...
0
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...
0
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...
1
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....
0
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
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.