[try Beta version]
Not logged in

 
invalid use of incomplete type ‘class

Nov 2, 2010 at 9:47am
Hello,
I've a problem with my template class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
template <class KeyType, class ValueType>
class Dictionary
{
        public:
                virtual ~Dictionary() { }

                virtual bool search(const KeyType& k) = 0;

...
}


template <class KeyType, class ValueType>
class DictAsMap : public Dictionary<KeyType,ValueType>
{
         public:
                 DictAsMap();
                 virtual ~DictAsMap();
                 virtual bool search(const KeyType& k);
...
}

template <class KeyType, class ValueType>
bool DictAsMap<KeyType, ValueType>::search(const KeyType& k)
{
...
}

template <class ValueType >
bool DictAsMap<int, ValueType>::search(const int& k)
{
...
} 


i get following compilier message:

g++ -g -W -Wall -ansi -pedantic -c -o main.o main.cpp
In file included from main.cpp:17:
DictAsMap.h:86: error: invalid use of incomplete type ‘class DictAsMap<int, ValueType>’
DictAsMap.h:14: error: declaration of ‘class DictAsMap<int, ValueType>’

What is wrong??

BR Molson



Nov 2, 2010 at 11:31am
search would need to be a template. You may want to consider removing that inheritance hierarchy with those virtual functions.
Nov 2, 2010 at 11:35am
i can't remove the inheritance hierarchy, because the class dictionary have to be an abstract class. so it's necessary to mark the functions as virtual.

I like to creat two search funktions one for int keys and one for string keys.
what can i do??
Nov 2, 2010 at 12:02pm
As far as i understand what you've intended to do, you need partial specialization for DictAsMap before the body of function:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class ValueType>
class DictAsMap<int,ValueType> : public Dictionary<int,ValueType>
{
         public:
                 DictAsMap();
                 virtual ~DictAsMap();
                 virtual bool search(const int& k);
};
template <class ValueType>
bool DictAsMap<int,ValueType>::search(const int& k)
{
...
}

and the ';' after the DictAsMap and Dictionary declarations
Last edited on Nov 2, 2010 at 12:08pm
Nov 2, 2010 at 12:22pm
The partially specialised function won't appear in the vtable.

Your abstraction doesn't seem quite right to me. Have you really thought about what it means to attempt partially specialise a virtual function?
Last edited on Nov 2, 2010 at 12:23pm
Nov 2, 2010 at 12:41pm
thats my problem! I like to specialise a virtual function, partially.
But it's clear to me to do that.
Nov 2, 2010 at 1:13pm
It's meaningless as you've described it.

I think you really mean:
1
2
3
4
5
6
7
8
9
10
template <class KeyType, class ValueType>
class DictAsMap : public Dictionary<KeyType,ValueType>
{
public:
    DictAsMap();
    virtual ~DictAsMap();
    virtual bool search(const KeyType& k);
    virtual bool search(int k);
...
};


What effect are you trying to achieve?
Last edited on Nov 2, 2010 at 1:13pm
Nov 2, 2010 at 1:15pm
Virtual functions not allowed to be templates (14.5.2.3). In fact in such code you get partial spec of whole class.
Compare to member templates:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A
{
template <class B>
void f();
template <class X,class Y>
void g();
};
template<> //full spec
void A::f<int>()
{}
template<> //full spec
void g<int,int>()
{}
template<class B>//error, function template partial specialization is not allowed
void g<B,int>()
{}
Nov 2, 2010 at 2:10pm
You can derive two classes for two key types:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template <class KeyType, class ValueType>
class Dictionary
{
public:
	virtual bool search(const KeyType& k) = 0;
};

template <class ValueType>
class IntKeyDictionary : public Dictionary<int, ValueType>
{
public:
	virtual bool search(const int& k);
};

template <class ValueType>
class StringKeyDictionary : public Dictionary<std::string, ValueType>
{
public:
	virtual bool search(const std::string& k);
};


Bad base class design hidden by inheritance, however virtual method works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
template <class KeyType, class ValueType>
class Dictionary
{
public:
	virtual bool search(const KeyType& k) = 0;
};

template <class ValueType>
class IntADictionary : public Dictionary<int, ValueType>
{
public:
	virtual bool search(const int& k)
	{
		cout << "Int a search" << endl;
		return true;
	}
};

template <class ValueType>
class IntBDictionary : public Dictionary<int, ValueType>
{
public:
	virtual bool search(const int& k)
	{
		cout << "Int b search" << endl;
		return true;
	}
};

template <class ValueType>
class StringADictionary : public Dictionary<std::string, ValueType>
{
public:
	virtual bool search(const std::string& k)
	{
		cout << "String a search" << endl;
		return true;
	}
};

template <class ValueType>
class StringBDictionary : public Dictionary<std::string, ValueType>
{
public:
	virtual bool search(const std::string& k)
	{
		cout << "String b search" << endl;

		return true;
	}
};

void main ( void )
{
	Dictionary<int, int> *di1 = new IntADictionary<int>;
	Dictionary<int, int> *di2 = new IntBDictionary<int>;

	Dictionary<std::string, int> *ds1 = new StringADictionary<int>;
	Dictionary<std::string, int> *ds2 = new StringBDictionary<int>;

	di1->search(0);
	di2->search(0);

	ds1->search("a");
	ds2->search("a");

	return;
}
Topic archived. No new replies allowed.