tags:

views:

69

answers:

3

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.

+2  A: 

Try this:

class blah { 
    mt_queue_c<int>    m_serial_q;    // this is 48 

    mt_queue_c<char>   m_serial_q2;      // this is 50 

    blah() : m_serial_q("string"), m_serial_q2(test2)
    {

    }
}; 
Saxon Druce
A: 

make default constructor for your class blah. and initialize the value for the template object in constructor initialization list

YeenFei
+2  A: 

This has nothing to with templates in particular - you can't initialize non-static members directly in the class definition (C++03, §9.2/4):

A member-declarator can contain a constant-initializer only if it declares a static member (9.4) of const integral or const enumeration type, see 9.4.2.

If you want to explicitly initialize data members, use the constructors initializer-list:

blah::blah() : m_serial_q("string") {}
Georg Fritzsche
Gah! I was looking for a zebra, focusing on the obscure error message and thinking it must be something special with templates. I completely overlooked the basics for nesting a class.Thanks to all for their comments. I won't be making this mistake again.
John Rocha