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. Writing a Generic Function

Writing a Generic Function

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++algorithmstutorial
2 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.
  • D Offline
    D Offline
    Donguy1976
    wrote on last edited by
    #1

    I have 3 functions that do sorting on a CArray.

    CArrays are declared in header file:

    CArray<int*, int*>m_Integer;
    CArray<string*, string*>m_String;
    CArray<double*, double*>m_Double;

    void sortIntegers()
    {

    // Do sorting for m_Integer here
    }

    void sortStrings()
    {
    // DO sorting for m_String
    }

    void sortDouble()
    {
    // Do sorting for m_Double
    }

    Now what i want to do is, have one generic function and then pass the CArray to that.

    So far what i did is,

    in Header file:

    void sortAll(CArray<void*, void*> m_Array);

    in CPP file:

    void sortAll(CArray<void*, void*> m_Array)
    {
    int size = m_Array.GetSize();

    ............
    ............
    ............
    
    m\_Array.GetAt(0); // Error here
    ................
    ................
    

    }

    The error is that, it needs a class type and not a void type.

    I am not sure how to proceed with writing a generic function to handle all different data types in one.

    Any help is welcome!

    L 1 Reply Last reply
    0
    • D Donguy1976

      I have 3 functions that do sorting on a CArray.

      CArrays are declared in header file:

      CArray<int*, int*>m_Integer;
      CArray<string*, string*>m_String;
      CArray<double*, double*>m_Double;

      void sortIntegers()
      {

      // Do sorting for m_Integer here
      }

      void sortStrings()
      {
      // DO sorting for m_String
      }

      void sortDouble()
      {
      // Do sorting for m_Double
      }

      Now what i want to do is, have one generic function and then pass the CArray to that.

      So far what i did is,

      in Header file:

      void sortAll(CArray<void*, void*> m_Array);

      in CPP file:

      void sortAll(CArray<void*, void*> m_Array)
      {
      int size = m_Array.GetSize();

      ............
      ............
      ............
      
      m\_Array.GetAt(0); // Error here
      ................
      ................
      

      }

      The error is that, it needs a class type and not a void type.

      I am not sure how to proceed with writing a generic function to handle all different data types in one.

      Any help is welcome!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to use Template classes; see https://www.google.com/search?q=template+class+c%2B%2B[^].

      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