|
-
February 19th, 2008, 08:09 AM
#1
how to use abstract template base class?
Abstract.h:
Code:
#ifndef ABSTRUCTGROUP_H_
#define ABSTRUCTGROUP_H_
template<class Type>
class AbstructGroup {
public:
virtual Type element(int) const = 0;
virtual int order() const = 0;
virtual Type inverse(const Type& x) const = 0;
virtual Type operation(const Type& left, const Type& right) const = 0;
};
#endif /*ABSTRUCTGROUP_H_*/
D3.h:
Code:
#ifndef D3_H_
#define D3_H_
#include <map>
#include <string>
#include <algorithm>
#include "AbstractGroup.h"
using namespace std;
class D3 : public AbstructGroup<string> {
//...
};
#endif /*D3_H_*/
Main.cpp:
Code:
#include "D3.h"
int main()
{
D3 d3;
AbstractGroup<class Type> *g = &d3;
return 0;
}
I'm getting error in the declaration of *g in main.cpp:
`AbstractGroup' was not declared in this scope Main.cpp
expected `;' before "class" Main.cpp
expected primary-expression before "class" Main.cpp
Compiled with mingw 5.1.3. Note that there is no other cpp file except main.cpp. What is the solution to these errors? Thanks in advance.
~Donotalo()
-
February 19th, 2008, 08:15 AM
#2
Re: how to use abstract template base class?
What are you trying to do here:
Code:
AbstractGroup<class Type> *g = &d3;
If it is an attempt at polymorphism, I believe it should be:
Code:
AbstractGroup<string>* g = &d3;
Incidentally, AbstructGroup should have a virtual destructor since it is intended to be a base class template.
Also, you should not use using directives or using declarations in header files (except within some local scope) as it would take effect in source files that include those headers.
Last edited by laserlight; February 19th, 2008 at 08:17 AM.
-
February 19th, 2008, 11:38 AM
#3
Re: how to use abstract template base class?
Thanks for your reply. But using
Code:
AbstractGroup<string>* g = &d3;
does not eliminate the errors. Besides, my intention is some thing different. There are many other classes that derived from AbstractGroup<Type>. Type may be string, int or whatever. I want to conditionally select one instance and assign its address to g. Then I want to manipulate the object through g.
~Donotalo()
-
February 19th, 2008, 11:45 AM
#4
Re: how to use abstract template base class?
 Originally Posted by Donotalo
Thanks for your reply. But using
Code:
AbstractGroup<string>* g = &d3;
does not eliminate the errors. Besides, my intention is some thing different. There are many other classes that derived from AbstractGroup<Type>. Type may be string, int or whatever. I want to conditionally select one instance and assign its address to g. Then I want to manipulate the object through g.
You can't. There is no relationship between the various instantions of a template, so AbstractGroup<string> and AbstractGroup<int> are unrelated classes - you can't convert between them. This means that classes derived from different instantiations of the base class are also unrelated.
You could make an untemplated base class for AbstractGroup, but that wouldn't gain you anything because it would have no sensible behaviour.
maybe if you described your high-level requirements we might be able to come up with a solution.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 19th, 2008, 11:56 AM
#5
Re: how to use abstract template base class?
 Originally Posted by Donotalo
Thanks for your reply. But using
Code:
AbstractGroup<string>* g = &d3;
does not eliminate the errors.
Oh, that's just a typographical error on your part. You defined AbstructGroup, but really wanted to define AbstractGroup.
-
February 20th, 2008, 07:02 AM
#6
Re: how to use abstract template base class?
 Originally Posted by laserlight
Oh, that's just a typographical error on your part. You defined AbstructGroup, but really wanted to define AbstractGroup.
so silly of me.
Here is the fixed code:
AbstractGroup.h:
Code:
#ifndef ABSTRACTGROUP_H_
#define ABSTRACTGROUP_H_
template<class Type>
class AbstractGroup {
public:
virtual Type element(int) const = 0;
virtual int order() const = 0;
virtual Type inverse(const Type& x) const = 0;
virtual Type operation(const Type& left, const Type& right) const = 0;
};
#endif /*ABSTRACTGROUP_H_*/
D3.h:
Code:
#ifndef D3_H_
#define D3_H_
#include <map>
#include <string>
#include <algorithm>
#include "AbstractGroup.h"
using namespace std;
class D3 : public AbstractGroup<string> {
//...
};
#endif /*D3_H_*/
Main.cpp:
Code:
#include "D3.h"
#include <iostream>
using namespace std;
int main()
{
D3 d3;
AbstractGroup<class Type> *g = (AbstractGroup<class Type> *) &d3;
cout << g->order() << endl;
cout << g->inverse(string("123")) << endl;
return 0;
}
mingw warns in the line where g->inverse() is used:
no matching function for call to `AbstractGroup<Type>::inverse(std::string)' MingWPractice Main.cpp
as expected. 
 Originally Posted by Graham
You could make an untemplated base class for AbstractGroup, but that wouldn't gain you anything because it would have no sensible behaviour.
maybe if you described your high-level requirements we might be able to come up with a solution.
All I want is to use the "interface" defined in AbstractGroup. There will be several classes that will use the interface. All of these derived classes (like D3) must have four functions in common: the pure virtual functions in AbstractGroup.
I also need to select a particular instance chosen by the user. So I would like to use AbstractGroup *g which cannot be specific, like AbstractGroup<string>* because the user may choose a derived class instance base on AbstractGroup<int>. Is there any alternative to use an abstract template class?
~Donotalo()
-
February 20th, 2008, 07:57 AM
#7
Re: how to use abstract template base class?
i just learn that what i want to achieve is completely impossible. i need to redesign.
~Donotalo()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|