Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ?

why passing argument by reference(&ref) in a template function shows error ? but works fine if arguments passed by pointer(*ptr) ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 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.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    in the below program it i have used both ways i.e. passed arguments by reference and pointers. 1. Arguments by reference:

    #include
    using namespace std;

    template
    void swap(T &x, T &y){
    T temp = x;
    x = y;
    y = temp;
    }

    void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;
    
    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
    

    }

    int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
    

    }

    2. Arguments by pointers:

    #include
    using namespace std;

    template
    void swap(T *x, T *y){
    T temp = x;
    x = y;
    y = temp;
    }

    void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;
    
    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
    

    }

    int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
    

    }

    also if i pass the arguments by reference but make it a constant then also i works, why ?

    #include
    using namespace std;

    template
    void swap(const T &x, const T &y){
    T temp = x;
    x = y;
    y = temp;
    }

    void fun(int m, int n, float a, float b){

    cout << "m & n before swap: " << m << " " << n << endl;
    swap(m, n);
    cout << "m & n after swap: " << m << " " << n << endl;
    
    cout << "i & j before swap: " << a << " " << b << endl;
    swap(a, b);
    cout << "i & j after swap: " << a << " " << b << endl;
    

    }

    int main(){
    fun(100, 200, 11.22, 33.44);

    return 0;
    

    }

    Thank you.

    C 1 Reply Last reply
    0
    • T Tarun Jha

      in the below program it i have used both ways i.e. passed arguments by reference and pointers. 1. Arguments by reference:

      #include
      using namespace std;

      template
      void swap(T &x, T &y){
      T temp = x;
      x = y;
      y = temp;
      }

      void fun(int m, int n, float a, float b){

      cout << "m & n before swap: " << m << " " << n << endl;
      swap(m, n);
      cout << "m & n after swap: " << m << " " << n << endl;
      
      cout << "i & j before swap: " << a << " " << b << endl;
      swap(a, b);
      cout << "i & j after swap: " << a << " " << b << endl;
      

      }

      int main(){
      fun(100, 200, 11.22, 33.44);

      return 0;
      

      }

      2. Arguments by pointers:

      #include
      using namespace std;

      template
      void swap(T *x, T *y){
      T temp = x;
      x = y;
      y = temp;
      }

      void fun(int m, int n, float a, float b){

      cout << "m & n before swap: " << m << " " << n << endl;
      swap(m, n);
      cout << "m & n after swap: " << m << " " << n << endl;
      
      cout << "i & j before swap: " << a << " " << b << endl;
      swap(a, b);
      cout << "i & j after swap: " << a << " " << b << endl;
      

      }

      int main(){
      fun(100, 200, 11.22, 33.44);

      return 0;
      

      }

      also if i pass the arguments by reference but make it a constant then also i works, why ?

      #include
      using namespace std;

      template
      void swap(const T &x, const T &y){
      T temp = x;
      x = y;
      y = temp;
      }

      void fun(int m, int n, float a, float b){

      cout << "m & n before swap: " << m << " " << n << endl;
      swap(m, n);
      cout << "m & n after swap: " << m << " " << n << endl;
      
      cout << "i & j before swap: " << a << " " << b << endl;
      swap(a, b);
      cout << "i & j after swap: " << a << " " << b << endl;
      

      }

      int main(){
      fun(100, 200, 11.22, 33.44);

      return 0;
      

      }

      Thank you.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You have a name clash with std::swap. Try

      #include using std::cout;
      using std::endl;

      template
      void swap(T &x, T &y){
      T temp = x;
      x = y;
      y = temp;
      }

      void fun(int m, int n, float a, float b){

      cout << "m & n before swap: " << m << " " << n << endl;
      swap(m, n);
      cout << "m & n after swap: " << m << " " << n << endl;
      
      cout << "i & j before swap: " << a << " " << b << endl;
      swap(a, b);
      cout << "i & j after swap: " << a << " " << b << endl;
      

      }

      int main()
      {
      fun(100, 200, 11.22, 33.44);
      }

      T 1 Reply Last reply
      0
      • C CPallini

        You have a name clash with std::swap. Try

        #include using std::cout;
        using std::endl;

        template
        void swap(T &x, T &y){
        T temp = x;
        x = y;
        y = temp;
        }

        void fun(int m, int n, float a, float b){

        cout << "m & n before swap: " << m << " " << n << endl;
        swap(m, n);
        cout << "m & n after swap: " << m << " " << n << endl;
        
        cout << "i & j before swap: " << a << " " << b << endl;
        swap(a, b);
        cout << "i & j after swap: " << a << " " << b << endl;
        

        }

        int main()
        {
        fun(100, 200, 11.22, 33.44);
        }

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        ohh... Thank you

        C 1 Reply Last reply
        0
        • T Tarun Jha

          ohh... Thank you

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          You are welcome.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups