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. Generics problem

Generics problem

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestionlearning
5 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.
  • R Offline
    R Offline
    roshihans
    wrote on last edited by
    #1

    I'm still learning about generics so I create a generics function like this :

    generic<class T> where T : ValueType
    T Process(T val)
    {
    val = val * 15;
    return val;
    }

    and called float a = Process<float>(10.0), but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the line val = val * 15;. Then I try a simpler method like this :

    generic<class T> where T : ValueType
    T Process()
    {
    T val = 15;
    return val;
    }

    Called float a = Process<float>() and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the line T val = 15; What is the correct way to use generics? Thanks in advance

    R G 2 Replies Last reply
    0
    • R roshihans

      I'm still learning about generics so I create a generics function like this :

      generic<class T> where T : ValueType
      T Process(T val)
      {
      val = val * 15;
      return val;
      }

      and called float a = Process<float>(10.0), but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the line val = val * 15;. Then I try a simpler method like this :

      generic<class T> where T : ValueType
      T Process()
      {
      T val = 15;
      return val;
      }

      Called float a = Process<float>() and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the line T val = 15; What is the correct way to use generics? Thanks in advance

      R Offline
      R Offline
      roshihans
      wrote on last edited by
      #2

      Anybody? :(

      1 Reply Last reply
      0
      • R roshihans

        I'm still learning about generics so I create a generics function like this :

        generic<class T> where T : ValueType
        T Process(T val)
        {
        val = val * 15;
        return val;
        }

        and called float a = Process<float>(10.0), but then I got " error C2296: '*' : illegal, left operand has type 'T' " in the line val = val * 15;. Then I try a simpler method like this :

        generic<class T> where T : ValueType
        T Process()
        {
        T val = 15;
        return val;
        }

        Called float a = Process<float>() and I still got "error C2440: 'initializing' : cannot convert from 'int' to 'T' " in the line T val = 15; What is the correct way to use generics? Thanks in advance

        G Offline
        G Offline
        Ghydo
        wrote on last edited by
        #3

        The problem in your code is that you cannot convert an int to a ValueType. T is a ValueYype, so you can use only the methods defined in that class when using objects of type T. What are you trying to achieve?

        R 1 Reply Last reply
        0
        • G Ghydo

          The problem in your code is that you cannot convert an int to a ValueType. T is a ValueYype, so you can use only the methods defined in that class when using objects of type T. What are you trying to achieve?

          R Offline
          R Offline
          roshihans
          wrote on last edited by
          #4

          Thanks for your reply, Actually I want to make a 2D matrix class that doesn't just limited in one type (ValueType) only so that I can create a Matrix<float> or Matrix<int> depending on what I need. I thought that generic is the most appropriate and also the simplest solution but it turns out that I was wrong.

          G 1 Reply Last reply
          0
          • R roshihans

            Thanks for your reply, Actually I want to make a 2D matrix class that doesn't just limited in one type (ValueType) only so that I can create a Matrix<float> or Matrix<int> depending on what I need. I thought that generic is the most appropriate and also the simplest solution but it turns out that I was wrong.

            G Offline
            G Offline
            Ghydo
            wrote on last edited by
            #5

            Unfortunately C# generics are not C++ generics, C# doesn't support numeric generics because there is no common base class of int/float/whatever that support calculation operations. You can do it the old manner, creating a class for every type you wish to support. There is another solution that uses generics in a different manner here[^].

            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