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. Managed C++/CLI
  4. Generic List Template, Function Parameter

Generic List Template, Function Parameter

Scheduled Pinned Locked Moved Managed C++/CLI
helpcloudtestingbeta-testingquestion
3 Posts 3 Posters 11 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.
  • B Offline
    B Offline
    Brennan Salibrici
    wrote on last edited by
    #1

    Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:

    void mCtrlWrap::IsVisible(bool TF, List^ list)

    {
    if (TF == true)
    {
    list[0]->Visible = true;
    list[0]->BackColor = Color::Azure;
    }
    else if (TF != true)
    {
    list[0]->Visible = false;
    }

    }

    but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried

    template
    void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list)

    {
    if (TF == true)
    {
    list[0]->Visible = true;
    list[0]->BackColor = Color::Azure;
    }
    else if (TF != true)
    {
    list[0]->Visible = false;
    }

    }

    which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan

    L 1 2 Replies Last reply
    0
    • B Brennan Salibrici

      Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:

      void mCtrlWrap::IsVisible(bool TF, List^ list)

      {
      if (TF == true)
      {
      list[0]->Visible = true;
      list[0]->BackColor = Color::Azure;
      }
      else if (TF != true)
      {
      list[0]->Visible = false;
      }

      }

      but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried

      template
      void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list)

      {
      if (TF == true)
      {
      list[0]->Visible = true;
      list[0]->BackColor = Color::Azure;
      }
      else if (TF != true)
      {
      list[0]->Visible = false;
      }

      }

      which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan

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

      In C# you would handle this by casting each component to a Control object. I assume you can do the same in C++/CLI. Also, why do you need the else if clause above? TF can only be true or false, if it is not true then you only need a simple else clause for the second block, thus:

      if (TF == true) // or just if (TF)
      {
      // what to do if it is true
      }
      else
      {
      // what to do if it is false
      }

      1 Reply Last reply
      0
      • B Brennan Salibrici

        Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:

        void mCtrlWrap::IsVisible(bool TF, List^ list)

        {
        if (TF == true)
        {
        list[0]->Visible = true;
        list[0]->BackColor = Color::Azure;
        }
        else if (TF != true)
        {
        list[0]->Visible = false;
        }

        }

        but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried

        template
        void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list)

        {
        if (TF == true)
        {
        list[0]->Visible = true;
        list[0]->BackColor = Color::Azure;
        }
        else if (TF != true)
        {
        list[0]->Visible = false;
        }

        }

        which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan

        1 Offline
        1 Offline
        11917640 Member
        wrote on last edited by
        #3

        You are mixing C++ template and .NET generics. This is OK is you understand what are you doing. In your specific case, this is classic C++ template problem. To call templated function C++ compiler must see the function implementation, and not only prototype. So, you can instantiate IsVisible function only in the same .cpp file. Or make it inline by moving the function implementation to .h file.

        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