template & typedefs declaration problem

Discussion in 'C++' started by krema2ren@gmail.com, Dec 8, 2005.

  1. Guest

    Hi

    Does some of you know how to declare getCollection() in the cpp file?
    My code snippet below produces following compile error:

    foo.cpp(48) : error C2143: syntax error : missing ';' before '&'
    foo.cpp(48) : error C2501: foo<T>::Collection' : missing storage-class
    or type specifiers
    foo.cpp(48) : error C2065: 'T' : undeclared identifier
    foo.cpp(48) : error C2955: 'foo' : use of class template requires
    template argument list

    Hope you guys can help me...

    foo.h
    --------------------------------------
    template <class T>
    class foo
    {
    public:
    typedef std::vector< foo<T> > Collection;

    foo();
    virtual ~foo(void);

    Collection& getCollection();

    private:
    Collection col;
    };


    foo.cpp
    --------------------------------------
    template <class T>
    foo<T>::foo(void)
    {
    }

    template <class T>
    foo<T>::~foo(void)
    {
    }

    template <class T>
    foo<T>::Collection& foo<T>::getCollection()
    {
    return col;
    };
    , Dec 8, 2005
    #1
    1. Advertising

  2. * :
    > Hi
    >
    > Does some of you know how to declare getCollection() in the cpp file?


    2 don'ts:

    * Technically, only one compiler supports placing template definitions
    in a separately compiled cpp file.

    * Stylistically, if you use the name 'getCollection' instead of just
    'collection', your code will be less readable, and in other cases
    than the one you have you will have removed a useful name for an
    optimized command-oriented version of an expression-oriented func.


    > My code snippet below produces following compile error:
    >
    > foo.cpp(48) : error C2143: syntax error : missing ';' before '&'
    > foo.cpp(48) : error C2501: foo<T>::Collection' : missing storage-class
    > or type specifiers
    > foo.cpp(48) : error C2065: 'T' : undeclared identifier
    > foo.cpp(48) : error C2955: 'foo' : use of class template requires
    > template argument list
    >
    > Hope you guys can help me...
    >
    > foo.h
    > --------------------------------------


    Here you need

    #ifndef FOO_H
    #define FOO_H

    #include <vector>


    > template <class T>
    > class foo
    > {
    > public:
    > typedef std::vector< foo<T> > Collection;


    Are you aware that you're defining a tree structure? Each foo
    contains a possibly non-empty collection of foo's.

    >
    > foo();
    > virtual ~foo(void);


    'void' is a C-ism: don't.


    >
    > Collection& getCollection();


    Should probably also have a 'const' version of that function.


    >
    > private:
    > Collection col;
    > };


    #endif

    >
    >
    > foo.cpp


    See above -- with most compilers you simply can't place these
    definitons in a separately compiled cpp file: put them in the
    header file.


    > template <class T>
    > foo<T>::foo(void)
    > {
    > }
    >
    > template <class T>
    > foo<T>::~foo(void)
    > {
    > }
    >
    > template <class T>
    > foo<T>::Collection& foo<T>::getCollection()


    Here you need a 'typename' to tell the compiler that Collection is a type:

    typename foo<T>::Collection& foo<T>::getCollection()

    > {
    > return col;
    > };


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 8, 2005
    #2
    1. Advertising

  3. krema2ren Guest

    >Are you aware that you're defining a tree structure? Each foo
    >contains a possibly non-empty collection of foo's.


    Yes, I'm aware of that.

    >Technically, only one compiler supports placing template definitions
    > in a separately compiled cpp file.


    I'm using MSVC 7.1, and it seems that it is supported here. Does GCC
    not support that?


    Thanks a lot for your quick answer.

    - Dennis
    krema2ren, Dec 8, 2005
    #3
  4. * krema2ren:
    > >Are you aware that you're defining a tree structure? Each foo
    > >contains a possibly non-empty collection of foo's.

    >
    > Yes, I'm aware of that.
    >
    > >Technically, only one compiler supports placing template definitions
    > > in a separately compiled cpp file.

    >
    > I'm using MSVC 7.1, and it seems that it is supported here. Does GCC
    > not support that?


    Neither MSVC 7.1 nor GCC support that, yet.

    It will compile but won't link.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 8, 2005
    #4
  5. Sumit Rajan Guest

    Sumit Rajan, Dec 8, 2005
    #5
  6. krema2ren Guest

    >Neither MSVC 7.1 nor GCC support that, yet.
    >It will compile but won't link.


    Unfortunately you are right....

    - Dennis
    krema2ren, Dec 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. emerth
    Replies:
    3
    Views:
    370
    emerth
    Aug 8, 2003
  2. Simon Elliott

    Forward declaration and typedefs

    Simon Elliott, Jan 7, 2005, in forum: C++
    Replies:
    5
    Views:
    3,960
    Simon Elliott
    Jan 7, 2005
  3. blueblueblue2005
    Replies:
    6
    Views:
    996
    blueblueblue2005
    Jul 19, 2005
  4. Replies:
    3
    Views:
    706
    Gianni Mariani
    Dec 8, 2006
  5. Grizlyk
    Replies:
    4
    Views:
    363
    Grizlyk
    Jan 15, 2007
Loading...

Share This Page