Not sure why converting from color_t* to float is a problem

This is a discussion on Not sure why converting from color_t* to float is a problem within the C++ Programming forums, part of the General Programming Boards category; I am having problems initializing members of a struct Here is the struct I am trying to initialize: Code: typedef ...

  1. #1
    Shadow12345
    Guest

    Not sure why converting from color_t* to float is a problem

    I am having problems initializing members of a struct
    Here is the struct I am trying to initialize:
    Code:
    typedef struct {
    float Red;
    float Green;
    float Blue;
    float Alpha;
    } color_t;
    I am trying to pass an array of floats to a function using pointers

    Here is a snippet of the function (I augmented other parameters and the rest of the code in the function)
    Code:
    Light::Light(float * Colors) {
    	Color->Red = Colors[Red];
    	Color->Green = Colors[Green];
    	Color->Blue = Colors[Blue];
    	Color->Alpha = Colors[Alpha];
    	Material = Materials;
    }
    And of course it is giving me the following errors:
    [errors]
    C:\Documents and Settings\Charles Thibault\Desktop\ourengine\LightImplementation.cpp (15) : error C2059: syntax error : 'constant'
    C:\Documents and Settings\Charles Thibault\Desktop\ourengine\LightImplementation.cpp (16) : error C2039: 'Color' : is not a member of 'color_t'
    c:\documents and settings\charles thibault\desktop\ourengine\light.h(13) : see declaration of 'color_t'
    [/errors]
    Oh and by the way I '#defined' Red, Green, Blue, and Alpha as 0, 1, 2, and 3, respectively (Yes, I seriously need to do that otherwise I forget what element of the array to use).

    EDIT: I don't know if it is legal to have the member variable and the #defines the same words, i.e Red, Gree, Blue and Alpha are the same names for both the members of the struct and the macros

    However, I tried taking them out and I am still getting the same errors
    Last edited by Shadow12345; 11-19-2002 at 07:56 PM.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Why even declare the color_t structure if you're just going to pass an array of floats around? Pass a color_t structure.

  3. #3
    Shadow12345
    Guest
    well when I actually call the opengl function for setting up lights it can only accept a const float *

    glLightfv(Number, GL_POSITION, (const float*)Location);

    For some reason the lighting isn't getting done and i think it is because it cannot cast that to a const float*

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Not sure why converting from color_t* to float is a problem

    Your code should work fine, except for:

    Originally posted by Shadow12345

    Oh and by the way I '#defined' Red, Green, Blue, and Alpha as 0, 1, 2, and 3, respectively
    Hehehe, there you have your problem.
    You #define away the member variables' names!
    Use constants instead, and you'll do fine.

    This should be in the FAQ board as an argument of not using #define.


    Clarification:
    Code:
    #define Red 1
    struct C
    {
      int Red;
    };
    
    //Will be converted by the preprocessor to:
    
    struct C
    {
      int 1;
    };
    
    //Which clearly is an error!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  3. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 04:23 PM
  4. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 09:07 PM
  5. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 10:36 PM

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