Nested structures in a function call

I'm attempting to initialize a structure in a function call, can this be done? The compiler simply gives me an error that it expects ')' before '{'.

The code I have in the header is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Definitions
#define CLR_BLACK3F		{0.0f, 0.0f, 0.0f}	//Black
#define CLR_RED3F		{1.0f, 0.0f, 0.0f}	//Red
#define CLR_GREEN3F		{0.0f, 1.0f, 0.0f}	//Green
#define CLR_YELLOW3F		{1.0f, 1.0f, 0.0f}	//Yellow
#define CLR_BLUE3F		{0.0f, 0.0f, 1.0f}	//Blue
#define CLR_MAGENTA3F		{1.0f, 0.0f, 1.0f}	//Magenta
#define CLR_CYAN3F		{0.0f, 1.0f, 1.0f}	//Cyan
#define CLR_WHITE3F		{1.0f, 1.0f, 1.0f}	//White

//Structures
struct color3f {
	float red;
	float green;
	float blue;
};

//Function prototypes
#pragma region twoDimensional
	void drawLine2f(float x1, float y1, float x2, float y2, color3f color);
#pragma endregion twoDimensional 

A lot of other junk was cut out for the sake of getting to the point. What's in the .cpp file is irrelevant for this problem.

I've tried instead of {r,g,b}; (color3f){r,g,b} as well as &(color3f){r,g,b}, always errors. Intellisense doesn't like it either.

Example call: drawLine2f(1.0f, 2.0f, 3.0f, 4.0f, CLR_WHITE3F);
That's what I'm attempting to achieve, can it be done? I'd prefer not to have global constants as opposed to #define's... I'd also prefer not to have to initialize the structure before calling the function, I'd prefer nested.
I previously had the function require 3 floats not in a structure, in which the define's were easy as I could just do '#define CLR_WHITE3F 1.0f, 1.0f, 1.0f', but structures are cleaner.
Last edited on
I believe this will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Definitions
#define CLR_BLACK3F     color3f(0.0f, 0.0f, 0.0f)	//Black
#define CLR_RED3F       color3f(1.0f, 0.0f, 0.0f)	//Red
#define CLR_GREEN3F     color3f(0.0f, 1.0f, 0.0f)	//Green
#define CLR_YELLOW3F    color3f(1.0f, 1.0f, 0.0f)	//Yellow
#define CLR_BLUE3F      color3f(0.0f, 0.0f, 1.0f)	//Blue
#define CLR_MAGENTA3F   color3f(1.0f, 0.0f, 1.0f)	//Magenta
#define CLR_CYAN3F      color3f(0.0f, 1.0f, 1.0f)	//Cyan
#define CLR_WHITE3F     color3f(1.0f, 1.0f, 1.0f)	//White

//Structures
struct color3f {
	float red;
	float green;
	float blue;

	color3f(float r, float g, float b):
        red(r),green(g),blue(b){}
};

//Function prototypes
#pragma region twoDimensional
	void drawLine2f(float x1, float y1, float x2, float y2, const color3f & color);
#pragma endregion twoDimensional 
I'll try that.

Edit: Eh, not working...

Error 22 error C2661: 'color3f::color3f' : no overloaded function takes 3 arguments
Intellisense: Error: No suitable constructor exists to convert from "float" to "color3f"

Looks like a structure's constructor can only copy other structures of the same type, can't initialize its content with specific stuff.
Last edited on
Did you copy everything I wrote here? Maybe you forgot to copy the constructor I wrote...

1
2
color3f(float r, float g, float b):
red(r),green(g),blue(b){}

EDIT: It works fine here (CodeBlocks/gcc)
Last edited on
Ah sorry, missed that part haha.

Alright that worked, cool.
Last edited on
^_^
Topic archived. No new replies allowed.