Template parameter method being identified as a variable

Discussion in 'C++' started by Alex, Mar 1, 2005.

  1. Alex

    Alex Guest

    The following simplified code reproduces my problem:

    #include <iostream>

    struct MyTypeHolder
    {
    typedef int MyType;
    };

    struct MyOp
    {
    template<class T>
    typename T::MyType doOp(typename T::MyType t)
    {
    return t + 1;
    }
    };

    template <class Op>
    class OpUser
    {
    public:
    int getIntValue(int i)
    {
    return op_.doOp<MyTypeHolder>(i); // Line 23
    }

    private:
    Op op_;
    };

    int main()
    {
    OpUser<MyOp> op_user;

    std::cout << op_user.getIntValue(1) << std::endl;

    return 0;
    }


    This compiles and works with VS.Net 2003 but not with g++ 3.4.3, which
    I am assuming is following the standard more strictly.

    The errors reported by g++ are:

    test.cpp: In member function `int OpUser<Op>::getIntValue(int)':
    test.cpp:23: error: expected primary-expression before '>' token

    It seems to be treating the opening '<' character as a less than
    rather than the start of an invocation of an explicit template method.
    I don't really have control over the MyTypeHolder or MyOp classes, but
    I can make changes to the OpUser class.

    Does anyone know of a suitable fix for my problem?

    Thanks,

    Alex
    Alex, Mar 1, 2005
    #1
    1. Advertising

  2. * Alex:
    > The following simplified code reproduces my problem:
    >
    > #include <iostream>
    >
    > struct MyTypeHolder
    > {
    > typedef int MyType;
    > };
    >
    > struct MyOp
    > {
    > template<class T>
    > typename T::MyType doOp(typename T::MyType t)
    > {
    > return t + 1;
    > }
    > };
    >
    > template <class Op>
    > class OpUser
    > {
    > public:
    > int getIntValue(int i)
    > {
    > return op_.doOp<MyTypeHolder>(i); // Line 23
    > }
    >
    > private:
    > Op op_;
    > };
    >
    > int main()
    > {
    > OpUser<MyOp> op_user;
    >
    > std::cout << op_user.getIntValue(1) << std::endl;
    >
    > return 0;
    > }
    >
    >
    > This compiles and works with VS.Net 2003 but not with g++ 3.4.3, which
    > I am assuming is following the standard more strictly.
    >
    > The errors reported by g++ are:
    >
    > test.cpp: In member function `int OpUser<Op>::getIntValue(int)':
    > test.cpp:23: error: expected primary-expression before '>' token
    >
    > It seems to be treating the opening '<' character as a less than
    > rather than the start of an invocation of an explicit template method.
    > I don't really have control over the MyTypeHolder or MyOp classes, but
    > I can make changes to the OpUser class.
    >
    > Does anyone know of a suitable fix for my problem?


    return op_.template doOp<MyTypeHolder>(i); // Line 23

    --
    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, Mar 1, 2005
    #2
    1. Advertising

  3. Alex

    Alex Guest

    Thank you for the fast reply - that works perfectly.
    Alex, Mar 1, 2005
    #3
    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. Sebek
    Replies:
    2
    Views:
    492
    Dimitre Novatchev [MVP XML]
    Apr 5, 2004
  2. mike
    Replies:
    10
    Views:
    724
    Joris Gillis
    Nov 2, 2004
  3. DanielJohnson

    struct point not identified by gcc

    DanielJohnson, Feb 22, 2007, in forum: C Programming
    Replies:
    39
    Views:
    6,214
    CBFalconer
    Mar 7, 2007
  4. mliptak
    Replies:
    3
    Views:
    333
    mliptak
    Mar 21, 2007
  5. Rose Girl
    Replies:
    1
    Views:
    147
    Martin Honnen
    Jan 26, 2005
Loading...

Share This Page