我正在尝试制作一个模板对象成员在其中的类。例如如何在类中包含模板成员?
mt_queue_c<int> m_serial_q("string");
但是,当我这样做时,它无法编译。如果我移动以外的类定义的这条线,使它成为一个全局对象,它编译得很好。
我凝结代码到尽可能小的故障单元,如下图所示(是的,它不会使意义,因为其他成员变量和函数缺少...)
#include <deque>
#include <queue>
#include <pthread.h>
#include <string>
#include <iostream>
template <class T, class Container = std::deque<T> >
class mt_queue_c {
public:
explicit mt_queue_c(const std::string &name,
const Container &cont = Container()) :
m_name(name),
m_c(cont)
{};
virtual ~mt_queue_c(void) {};
protected:
// Name of queue, used in error messages.
std::string m_name;
// The container that manages the queue.
Container m_c;
};
// string object for a test
std::string test2("foobar");
// Two tests showing it works as a global
mt_queue_c<char> outside1("string");
mt_queue_c<char> outside2(test2);
// Two failed attempts to include the object as a member object.
class blah {
mt_queue_c<int> m_serial_q("string"); // this is 48
mt_queue_c<char> m_serial_q2(test2); // this is 50
};
// Adding main just because.
int main()
{
std::cout << "Hello World" << std::endl;
}
当我做到这一点,我收到的错误结果是:
make
g++ -m32 -fPIC -Werror -Wall -Wunused-function -Wunused-parameter -Wunused-variable -I. -I/views/EVENT_ENGINE/LU_7.0-2/server/CommonLib/include -I/views/EVENT_ENGINE/LU_7.0-2/server/Common/Build/Include -g -c -o ${OBJ_DIR}/testTemp.o testTemp.cxx
testTemp.cxx:48: error: expected identifier before string constant
testTemp.cxx:48: error: expected ',' or '...' before string constant
testTemp.cxx:50: error: 'test2' is not a type
make: *** [/views/EVENT_ENGINE/LU_7.0-2/server/applications/event_engine/Obj/testTemp.o] Error 1
我在做什么错?如果我们希望模板类型始终与特定类相同,如何在模块中“嵌入”模板?
在此先感谢您的帮助。
尔加!我正在寻找斑马,专注于模糊的错误信息,并认为它必须是模板中特别的东西。我完全忽略了嵌套类的基础知识。 感谢所有的评论。我不会再犯这个错误了。 – 2010-07-13 22:57:19