type cast problem in VS2005

This is a discussion on type cast problem in VS2005 within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using the the following code to cast a double to an enum. Code: (Density::KernelType)(GetScalar(pqrs[3])) But VS 2005 throws: ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    type cast problem in VS2005

    Hi,

    I'm using the the following code to cast a double to an enum.

    Code:
    (Density::KernelType)(GetScalar(pqrs[3]))
    But VS 2005 throws:

    error C2440: 'type cast' : cannot convert from 'double' to 'Density::KernelType'
    Conversions between enumeration and floating point values are no longer allowed


    But I need this cast badly. Any ways to force this coversion.
    Any solutions?

    Thanks,
    AK

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How about:
    Code:
    (Density::KernelType)int(GetScalar(pqrs[3]))
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Any time you need such a conversion, there's a design error somewhere, but JaWiB is right: by casting to an intermediate int, you can trick the compiler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You should consider C++ casts instead of c-style casts as well.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Perhaps you should be looking at improving your code, rather than attempting something as obviously imprecise as converting a double to an enum.

    Basically, you coded to "what my compiler will let me get away with" using the old compiler, and now something has decided to enforce the rules, you're struggling to catch up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM
  5. type cast to a pointer....
    By rc7j in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 03:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21