noob在这里仍在试验模板.试图编写一个消息处理类模板
template <typename T> class MessageProcessor {
//constructor, destructor defined
//Code using t_ and other functions
foo( void ) {
//More code in a perfectly fine method
}
private: T *t_
};
Run Code Online (Sandbox Code Playgroud)
全部在头文件中定义.我已经建立并测试了我的课程,一切都很好.现在,我正在尝试这样做:
template <typename T> class MessageProcesor {
//Same stuff as before
foo(void) {
//Same code as before in foo, but one new line:
t_->getMessageSender<MessageType>();
}
private: T *t_;
};
Run Code Online (Sandbox Code Playgroud)
但是,这行在">"标记之前给出了错误表达式类型的错误.
我添加了必要的头文件来定义MessageType是什么.我之前很久就使用过这个函数,只是不是在这种情况下.
我怀疑编译器不喜欢模板函数在未定义的类模板(unspecialized?)中完全定义(专用?)的事实.我并不完全了解模板"专业化"的原因.大多数解释都集中在"完整"或"部分"的概念上,而不是首先使它成为专业的概念.
如果您想查看更多代码,请道歉.我在工作时没有互联网接入,这就是我正在做的事情,所以我必须将所有内容放入我的心理"暂存器"并将其带回家.
我是一个尝试模板和设计模式的菜鸟,想要使用模板参数ITEM创建迭代器模式.但是,我的私有成员var是一个STL容器,它本身就是一个模板,那么如何将STL容器的模板参数"同步"为类模板使用的模板参数呢?
这是实际的类:
#include "StatusIteratorInterface.h"
#include "StatusContainer.h"
#include <map>
template<typename ITEM> class StatusContainer;
template <typename ITEM>
class StatusIterator : public StatusIteratorInterface<ITEM> {
public:
StatusIterator( StatusContainer<ITEM>* container );
ITEM* getFirst( void );
ITEM* getNext( void );
bool isDone( void );
ITEM* getCurrent( void );
private:
StatusContainer<ITEM>* container_;
ITEM* currentItem_;
std::multimap< std::pair<std::string, uint32_t>, ITEM >::iterator conIter1_; //gives error
std::multimap< std::pair<string, uint32_t>, uint32_t>::iterator conIter_;
};
Run Code Online (Sandbox Code Playgroud)
conIter_很好,但'硬编码'我的模板值.包括上面的行给出了这个编译错误:
/Users/joe/Developer/Template_DesignPattern_Iterator/StatusIterator.h:33: error: expected ';' before 'conIter1_'
Run Code Online (Sandbox Code Playgroud)
我有一个main编译并运行此代码很好,但显然只适用于uint32_t类型.请原谅我,如果我使用了不正确的术语,例如'模板参数'副'模板参数'.