我在使用抽象类作为值时无法正确编译抽象类。理想情况下,我希望像下面这样做:
unordered_map<string, Process_Base> func_map;
Process_Base的外观如下:
//Contained in Process_Base.hpp
class Process_Base{
public:
virtual ~Process_Base(){};
virtual void process() const = 0;
};
子类的代码将会类似于:
#include "Process_Base.hpp"
class Process_Message : public Process_Base {
public:
~Process_Message(){};
virtual void process();
};
#include <stdio.h>
#include <string.h>
#include "Process_Base.hpp"
class Process_Message{
public:
void process(){
printf("%s", "Hello");
}
};
这个想法是我可以将子类添加到地图上,并有一个简单的函数,该函数将查看键值并调用子类的处理函数。
当我在CentOS 5.8上编译时,使用的是:
g++44 -Wall -c -std=c++0x -I/usr/include -g Source.cpp
我收到了以下一系列错误:
In file included from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/bits/stl_algobase.h:66,
from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/string:42,
from Source.cpp:2:
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h: In instantiation of ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base>’:
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/bits/stl_function.h:482: instantiated from ‘std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> >’
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/tr1_impl/hashtable_policy.h:790: instantiated from ‘std::__detail::_Hash_code_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base>, std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> >, std::equal_to<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>’
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/tr1_impl/hashtable:137: instantiated from ‘std::_Hashtable<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base>, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> >, std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> >, std::equal_to<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>’
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/tr1_impl/unordered_map:48: instantiated from ‘std::__unordered_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base, std::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> >, false>’
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/tr1_impl/unordered_map:190: instantiated from ‘std::unordered_map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base, std::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base> > >’
Source.cpp:14: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:73: error: cannot declare field ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Process_Base>::second’ to be of abstract type ‘Process_Base’
Process_Base.hpp:1: note: because the following virtual functions are pure within ‘Process_Base’:
Process_Base.hpp:5: note: virtual void Process_Base::process() const
make: *** [Source.o] Error 1
有人可以帮我理解这些编译错误吗?是否尝试制作函数指针的映射比子类更好?如果是,那么如何创建指向像“处理消息”这样的类中函数的指针映射呢?
unordered_map<string,Process_Base*>
。您只能拥有指向抽象类的指针。 - alecbz