I am attempting to make a class that has a template object member inside of it. For example
mt_queue_c<int> m_serial_q("string");
However, when I do this it fails to compile. If I move this line outside of the class definition so that it becomes a global object, it compiles fine.
I condensed the code into the smallest possible fail unit as shown below (yes, it won't make sense because other members variables and functions are missing...)
#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;
}
When I do this the error results I receive are:
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
What am I doing wrong? How can one 'embed' a template in a class given that we want the template type to always be the same for a particular class?
Thanks in advance for your help.