Advertisement

C++ templates and abstract classes problem

Started by September 21, 2004 02:29 PM
3 comments, last by Damage_Incorporated 20 years, 2 months ago
Ok, trying to dive into STL with C++ so I thought I'd make a simple mud server but I kinda stumbled into a little problem with std::map or rather seems to be std::pair that is messing up. The relevant code is as follows:

// Abstract callback functor for sockets. Server knows sockets/fds nothing more...
	class Callback
	{
		public:
			virtual ~Callback() = 0;
			virtual void *data() = 0;
			virtual void data(void *) = 0;
			virtual int operator () (int, void *) = 0;
	};
	
	struct serverdata_t
	{
		int                      con;
		int                      maxfd; // used for sets
		fd_set                   master;
		fd_set                   read;
		fd_set                   write; // will probably not be used?
		std::map<int, Callback>  callbacks;
	};


Now the error I get from g++ is as follows: In file included from src/server.cxx:23: src/server.h:27:16: util: No such file or directory src/server.cxx:73:3: warning: no newline at end of file /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_pair.h: In instantiation of `std::pair<const int, server::Callback>': /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_tree.h:135: instantiated from `std::_Rb_tree_node<std::pair<const int, server::Callback> >' /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_tree.h:1064: instantiated from `void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = int, _Val = std::pair<const int, server::Callback>, _KeyOfValue = std::_Select1st<std::pair<const int, server::Callback> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, server::Callback> >]' /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_tree.h:565: instantiated from `std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = int, _Val = std::pair<const int, server::Callback>, _KeyOfValue = std::_Select1st<std::pair<const int, server::Callback> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, server::Callback> >]' src/server.cxx:47: instantiated from here /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_pair.h:69: error: cannot declare field `std::pair<const int, server::Callback>::second' to be of type `server::Callback' /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3/bits/stl_pair.h:69: error: because the following virtual functions are abstract: src/server.cxx:34: error: virtual int server::Callback::operator()(int, void*) src/server.cxx:33: error: virtual void server::Callback::data(void*) src/server.cxx:32: error: virtual void* server::Callback::data() src/server.cxx:31: error: virtual server::Callback::~Callback() make: *** [obj/server.o] Error 1 I'm kinda new to templates and STL so anyone care to shed some light on what I'm doing wrong, I'm pretty sure it's quite simple. Oh and I'm using Gentoo with g++ -v: Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.1/specs Configured with: /var/tmp/portage/gcc-3.4.1-r3/work/gcc-3.4.1/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4/info --enable-shared --host=i686-pc-linux-gnu --target=i686-pc-linux-gnu --with-system-zlib --enable-languages=c,c++ --enable-threads=posix --enable-long-long --disable-checking --disable-libunwind-exceptions --enable-cstdio=stdio --enable-version-specific-runtime-libs --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.1/include/g++-v3 --with-local-prefix=/usr/local --disable-werror --enable-shared --enable-nls --without-included-gettext --disable-multilib --enable-__cxa_atexit --enable-clocale=gnu Thread model: posix gcc version 3.4.1 20040803 (Gentoo Linux 3.4.1-r3, ssp-3.4-2, pie-8.7.6.5) - Damage Incorporated.
You declare your map as containing objects of type Callback, but Callback is an abstract class—it has pure virtual functions, after all—so it cannot be instantiated (and obviously, if you cannot instantiate the class, you cannot put instances of it in a map). You probably want your map to contain Callback pointers, so that you can use them polymorphically.
Advertisement
Uhm, yeah that's probably what I want to do. I did say it was something simple didn't I? Anyways thanks. ;)

- Damage Incorporated
also it seems that your include paths are wrong, because src/server.h tries to include "util" and cant find it. (this is in the 2nd line of your compiler error). You can set include paths with -I /path/to/include/files.
Quote: Original post by crupp
also it seems that your include paths are wrong, because src/server.h tries to include "util" and cant find it. (this is in the 2nd line of your compiler error). You can set include paths with -I /path/to/include/files.


Yeah that's sorta because util doesn't exist anywhere else but in my imagination and it should probably stay there for the good of mankind ;)

- Damage Incorporated

This topic is closed to new replies.

Advertisement