Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ template classes question (http://www.programmingforums.org/showthread.php?t=13471)

rsnd Jul 4th, 2007 1:38 PM

C++ template classes question
 
:

#include <stdio.h>
#include <memory>
template <class _Type, int _MinLen = 32, _Type _DefValue = 0>
class dlist {
public:
        _Type * items;       
        int length;
};
dlist<char*> motd_lines;
int main (){
    return 0;
}



:

H:\test.cpp:9: error: could not convert template argument `0' to `char*'
H:\test.cpp:9: error: invalid type in declaration before ';' token


Using the latest version of Dev-Cpp and whatever Mingw that came with it. My question is...what am I doing wrong here? It compiles perfectly with both VC++ 6 and 2005.

pegasus001 Jul 4th, 2007 1:41 PM

Replace the stdio with cstdio.

rsnd Jul 4th, 2007 1:47 PM

umm ok...what was the problem?

pegasus001 Jul 4th, 2007 1:48 PM

All the C header files in C++ have a c letter before them.

rsnd Jul 4th, 2007 2:05 PM

Well my project didn't compile in Dev-Cpp so I extracted that bit. But it sill doesn't compile. How about this?
:

#include <cstdio>
#include <memory>
template <class _Type, int _MinLen = 32, _Type _DevValue = 0>
class dlist {
public:
        _Type * items;       
        int length;
        _Type get(int i){
                return (i >= 0 && i < length)? items[i] : _DevValue ;
        }
};
dlist<char*> motd_lines;
int main (){
    return 0;
}


Quote:

H:\test.cpp:12: error: could not convert template argument `0' to `char*'
H:\test.cpp:12: error: invalid type in declaration before ';' token
seems like some sort of type casting failure but isnot 0 a valid value for a pointer?

Cache Jul 4th, 2007 2:34 PM

I think pointer template arguments have to have external linkage. Presumably '0' doesn't. You'll have to do some digging.

rsnd Jul 4th, 2007 2:48 PM

Well, "dlist<int,32,5> motd_lines;" would work just fine. I'm removed the "_DevValue" parameter and it compiled just fine. Thanks everyone.

The Dark Jul 4th, 2007 6:42 PM

You could try using a cast to _Type around the 0 in the initialiser.

Cache Jul 4th, 2007 7:18 PM

Quote:

Originally Posted by The Dark (Post 130032)
You could try using a cast to _Type around the 0 in the initialiser.

It still wouldn't have external linkage.


All times are GMT -5. The time now is 2:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC