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. Templates & error C2440

Templates & error C2440

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionhtmlwpfcom
7 Posts 3 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
    Rage_bla
    wrote on last edited by
    #1

    I need some help. Here it the whole code snippet: #include "stdafx.h" template int IndirectFunction(int * pData) { return 0; } //also tried //int __cdecl IndirectFunction(int * pData) //{...} typedef int (*ONEPARAMFUNCTION)(void* param); //also tried //typedef int (__cdecl *ONEPARAMFUNCTION)(void* param); int main() { // <<<<<<<<< //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'ONEPARAMFUNCTION' ONEPARAMFUNCTION t = (ONEPARAMFUNCTION) &IndirectFunction; // >>>>>>>>> return 0; } How can I make it work ?? I tried googling a little - C2440 seems to be pretty "popular" but I didn't find anything that could help me. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2440.asp also didn't help :( Seems it happens becouse of the template, if I remove it compiles ok (using only - int IndirectFunction(int * pData) without the template<>). If I replace: int IndirectFunction(int * pData) with int IndirectFunction(void * pData) it compiles ok. If I use templates and int* as parameter I get the error. Help! -- modified at 14:22 Friday 21st October, 2005

    K 1 Reply Last reply
    0
    • R Rage_bla

      I need some help. Here it the whole code snippet: #include "stdafx.h" template int IndirectFunction(int * pData) { return 0; } //also tried //int __cdecl IndirectFunction(int * pData) //{...} typedef int (*ONEPARAMFUNCTION)(void* param); //also tried //typedef int (__cdecl *ONEPARAMFUNCTION)(void* param); int main() { // <<<<<<<<< //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'ONEPARAMFUNCTION' ONEPARAMFUNCTION t = (ONEPARAMFUNCTION) &IndirectFunction; // >>>>>>>>> return 0; } How can I make it work ?? I tried googling a little - C2440 seems to be pretty "popular" but I didn't find anything that could help me. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2440.asp also didn't help :( Seems it happens becouse of the template, if I remove it compiles ok (using only - int IndirectFunction(int * pData) without the template<>). If I replace: int IndirectFunction(int * pData) with int IndirectFunction(void * pData) it compiles ok. If I use templates and int* as parameter I get the error. Help! -- modified at 14:22 Friday 21st October, 2005

      K Offline
      K Offline
      kylur
      wrote on last edited by
      #2

      try: int (*ONEPARAMFUNCITON)(int*) = IndirectFunction<int>; then: int x(666); cout << ONEPARAMFUNCITON(&x) << endl; should work! -- modified at 15:11 Friday 21st October, 2005

      R 1 Reply Last reply
      0
      • K kylur

        try: int (*ONEPARAMFUNCITON)(int*) = IndirectFunction<int>; then: int x(666); cout << ONEPARAMFUNCITON(&x) << endl; should work! -- modified at 15:11 Friday 21st October, 2005

        R Offline
        R Offline
        Rage_bla
        wrote on last edited by
        #3

        And it works perfectly! I would never think of this on my own - it looks wierd, but it works!! Thank you so much! H.

        B 1 Reply Last reply
        0
        • R Rage_bla

          And it works perfectly! I would never think of this on my own - it looks wierd, but it works!! Thank you so much! H.

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          This is normal when using templates. The compiler only creates functions that are needed in your program. Son you can't create a function pointer to such function until it exists. When you first write funtion<int>(666); the compiler creates such function. Later you can get a function pointer from it. It seems odd but this is the way it needs to be done, with some sort of dummy call to the function Hope this explains the wierd behaveior codito ergo sum

          R 1 Reply Last reply
          0
          • B BadKarma

            This is normal when using templates. The compiler only creates functions that are needed in your program. Son you can't create a function pointer to such function until it exists. When you first write funtion<int>(666); the compiler creates such function. Later you can get a function pointer from it. It seems odd but this is the way it needs to be done, with some sort of dummy call to the function Hope this explains the wierd behaveior codito ergo sum

            R Offline
            R Offline
            Rage_bla
            wrote on last edited by
            #5

            | Quote: >When you first write funtion(666); the compiler creates such function. Later you can get a function pointer from it. But that isn't what is happening (atleast I don't see it) - I first take the pointer and then call the function. And it works if I take the pointer to a function that has the exact signature, that is the int* parameter. The problems are created when I try to get a pointer to function and cast it to a function that takes a void* in the same command/line. template int IndirectFunction(int * pData) { return 0; } typedef int (*ONEPARAMFUNCTION_PVOID)(void* param); typedef int (*ONEPARAMFUNCTION_PINT)(int* param); int main() { ONEPARAMFUNCTION_PINT pF0 = &IndirectFunction; // ok!! ONEPARAMFUNCTION_PINT pF = IndirectFunction; // ok!! ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! ONEPARAMFUNCTION_PVOID pF3 = (ONEPARAMFUNCTION_PVOID)pF; // ok!! ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! int a =5; pF(&a); pF2(&a); pF3(&a); pF(&a); return 0; } The wierd part is that this works: ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! But this won't work: ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! But isn't this practicaly the same? IndirectFunction should have the same signature as the ONEPARAMFUNCTION_PINT so the cast shouldn't be needed and the second example should be the same as the first one (I even tried forcing the same calling convention, but MSVC7 still thinks that ONEPARAMFUNCTION_PINT and IndirectFunction have different signatures and requires the cast to ONEPARAMFUNCTION_PINT first) ???

            B 1 Reply Last reply
            0
            • R Rage_bla

              | Quote: >When you first write funtion(666); the compiler creates such function. Later you can get a function pointer from it. But that isn't what is happening (atleast I don't see it) - I first take the pointer and then call the function. And it works if I take the pointer to a function that has the exact signature, that is the int* parameter. The problems are created when I try to get a pointer to function and cast it to a function that takes a void* in the same command/line. template int IndirectFunction(int * pData) { return 0; } typedef int (*ONEPARAMFUNCTION_PVOID)(void* param); typedef int (*ONEPARAMFUNCTION_PINT)(int* param); int main() { ONEPARAMFUNCTION_PINT pF0 = &IndirectFunction; // ok!! ONEPARAMFUNCTION_PINT pF = IndirectFunction; // ok!! ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! ONEPARAMFUNCTION_PVOID pF3 = (ONEPARAMFUNCTION_PVOID)pF; // ok!! ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! int a =5; pF(&a); pF2(&a); pF3(&a); pF(&a); return 0; } The wierd part is that this works: ONEPARAMFUNCTION_PVOID pF4 = (ONEPARAMFUNCTION_PVOID)(ONEPARAMFUNCTION_PINT)IndirectFunction; // ok!! But this won't work: ONEPARAMFUNCTION_PVOID pF2 = (ONEPARAMFUNCTION_PVOID)IndirectFunction; // error!! But isn't this practicaly the same? IndirectFunction should have the same signature as the ONEPARAMFUNCTION_PINT so the cast shouldn't be needed and the second example should be the same as the first one (I even tried forcing the same calling convention, but MSVC7 still thinks that ONEPARAMFUNCTION_PINT and IndirectFunction have different signatures and requires the cast to ONEPARAMFUNCTION_PINT first) ???

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              I have taken your code and everything compiles without errors.:confused: But guess what :), I have used MSVS 2005 (MS VC8) so this is certainly a bug in the VC7 compiler. So your code is right but your compiler (I use the same at work :(() missed the pedals, happens to the best of us:laugh: codito ergo sum

              R 1 Reply Last reply
              0
              • B BadKarma

                I have taken your code and everything compiles without errors.:confused: But guess what :), I have used MSVS 2005 (MS VC8) so this is certainly a bug in the VC7 compiler. So your code is right but your compiler (I use the same at work :(() missed the pedals, happens to the best of us:laugh: codito ergo sum

                R Offline
                R Offline
                Rage_bla
                wrote on last edited by
                #7

                BadKarma wrote:

                I have used MSVS 2005 (MS VC8) so this is certainly a bug in the VC7 compiler.

                VC8 is probaby the only compiler I haven't tried :) VC7.1 - same error VC6 - more errors, oh well GCC3.2 - same error Thanks for your help

                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