Templated function inside a template class. Syntax.

Discussion in 'C++' started by Thormod Johansen, Apr 19, 2007.

  1. Hi,

    What is the correct syntax for defining a templated function inside a
    template class? Example:

    template <class T>
    class myClass {
    private:
    T data;
    public:
    template <class InputType>
    int myFunc(InputType arg) {
    }
    };

    To this my compiler says "declaration terminated incorrectly" at the line
    containing "template <class InputType>".

    Thanks in advance.
    Thormod Johansen, Apr 19, 2007
    #1
    1. Advertising

  2. Thormod Johansen wrote:
    > What is the correct syntax for defining a templated function inside a
    > template class? Example:
    >
    > template <class T>
    > class myClass {
    > private:
    > T data;
    > public:
    > template <class InputType>
    > int myFunc(InputType arg) {


    I'd venture a guess that you need a 'return' statement here.

    > }
    > };
    >
    > To this my compiler says "declaration terminated incorrectly" at the
    > line containing "template <class InputType>".


    I don't see a problem in the code you posted. Could you post the
    _exact_ *complete* program that causes the error?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 19, 2007
    #2
    1. Advertising

  3. Thormod Johansen wrote:
    > "Victor Bazarov" <> wrote in message
    > news:f0858p$ggf$...
    >> Thormod Johansen wrote:
    >>> To this my compiler says "declaration terminated incorrectly" at the
    >>> line containing "template <class InputType>".

    >>
    >> I don't see a problem in the code you posted. Could you post the
    >> _exact_ *complete* program that causes the error?

    >
    > Here is an excat complete example for which my compiler (paradigm c++)
    > generates the same error the same place.
    >
    > #include <embedded.h>
    >
    > template <class T>
    > class myClass {
    > private:
    > T data;
    > public:
    > myClass(T t) : data(t) {};
    >
    > template <class InputType>
    > int myFunc(InputType arg) {
    > outport(0x500, arg);
    > return 0;
    > }
    > };
    >
    > void main() {
    > myClass<unsigned char> c(2);
    >
    > int value = 0xDC;
    > c.myFunc(value);
    >
    > while(1)
    > ;
    > }
    >
    > Thanks in advance


    That's not a complete example. <embedded.h> is missing...

    Never mind that. Drop the include, drop the call to 'outport',
    replace 'void main' with 'int main', and you get a correct C++
    program. It should compile with any compliant compiler. Now,
    if it doesn't compile with the compiler you're using, it is
    apparently not compliant and cannot handle member templates (it
    is not unheard of). Complain to people from whom you got it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 19, 2007
    #3
  4. "Victor Bazarov" <> wrote in message
    news:f0858p$ggf$...
    > Thormod Johansen wrote:
    >> To this my compiler says "declaration terminated incorrectly" at the
    >> line containing "template <class InputType>".

    >
    > I don't see a problem in the code you posted. Could you post the
    > _exact_ *complete* program that causes the error?


    Here is an excat complete example for which my compiler (paradigm c++)
    generates the same error the same place.

    #include <embedded.h>

    template <class T>
    class myClass {
    private:
    T data;
    public:
    myClass(T t) : data(t) {};

    template <class InputType>
    int myFunc(InputType arg) {
    outport(0x500, arg);
    return 0;
    }
    };

    void main() {
    myClass<unsigned char> c(2);

    int value = 0xDC;
    c.myFunc(value);

    while(1)
    ;
    }

    Thanks in advance
    Thormod Johansen, Apr 19, 2007
    #4
    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. Amadeus W. M.
    Replies:
    2
    Views:
    382
    Amadeus W. M.
    Jul 4, 2006
  2. chhenning
    Replies:
    5
    Views:
    352
    chhenning
    Feb 13, 2008
  3. LiloLilo
    Replies:
    1
    Views:
    618
    Seebs
    Jun 6, 2010
  4. claudiu
    Replies:
    2
    Views:
    289
    claudiu
    Apr 1, 2011
  5. claudiu
    Replies:
    3
    Views:
    482
    claudiu
    Apr 1, 2011
Loading...

Share This Page