Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Where is error with this template class? this code is from a c++ tutorial book.
Forum Updated to NodeBB v4.3 + New Features

Where is error with this template class? this code is from a c++ tutorial book.

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emas
    wrote on last edited by
    #1

    @
    #include <iostream>
    using namespace std;

    // class templates

    template <class T>
    class pair {
    T value1, value2;
    public:
    pair (T first, T second) {
    value1=first;
    value2=second;
    }
    T getmax ();
    };

    template <class T>
    T pair::getmax (){
    T retval;
    retval = value1>value2? value1 : value2;
    return retval;
    }

    int main () {
    pair myobject (100, 75);
    cout << myobject.getmax();
    return 0;
    }
    @

    [edit: added coding tag @ SGaist]

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi emas.
      This is not a Qt question, i'll move it to right forum.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arnaut
        wrote on last edited by
        #3

        Hi there,
        you have some syntax problems there. The implementation of get max should be:
        @template <class T> T pair<T>::getmax (){ T retval; retval = value1>value2? value1 : value2; return retval;
        }@

        note the pair<T>::

        The compiler can't infer the template parameter, so you need to provide it:
        @ pair<int> myobject (100, 75); @

        Hope it helps,
        H.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          emas
          wrote on last edited by
          #4

          //Thanks Arnaut, but it does not work either. qt creator says:
          //D:\t\untitled9\main.cpp:25: error: reference to 'pair' is ambiguous, pair<int> myobject(100, 75);

          //the code is plain c++ project

          @
          #include <iostream>
          using namespace std;

          // class templates

          template <class T>
          class pair {
          T value1, value2;
          public:
          pair (T first, T second) {
          value1=first;
          value2=second;
          }
          T getmax ();
          };

          template <class T>
          T pair<T>::getmax (){
          T retval;
          retval = value1>value2? value1 : value2;
          return retval;
          }

          int main () {
          pair<int> myobject(100, 75);
          cout << myobject.getmax();
          return 0;
          }
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arnaut
            wrote on last edited by
            #5

            Oh, I see.

            The last problem is the using directive.
            There is a class named pair in std namespace and the compiler can't decide between your class pair or the class std::pair

            Hope it helps,
            H.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved