struct in header file won't work in .cpp [SOLVED]

Started by
6 comments, last by wlw_wl 15 years, 5 months ago
Hello, I do a simple thing:


// my.h file
struct color { int r,g,b; };

extern color some_color; /* extern because I also use some_color in function declarations in the same .h, like this one:*/

void do_something(int x, int y, color some_color);

// my.cpp file
#include "my.h"

color some_color;
some_color.r = 0;    // *
Seems ok to me, but not to VS. Following errors while compiling .cpp: line marked by *: error C2143: syntax error : missing ';' before '.' error C4430: missing type specifier - int assumed. error C2371: 'some_color' : redefinition; different basic types seems that, despite the declaration of some_color, he thinks I missed the type specifier, so he assumes int - that causes the first and third error. What's wrong here? [Edited by - wlw_wl on November 5, 2008 5:15:31 PM]
Advertisement
Quote:Original post by wlw_wl
extern color some_color; /* extern because I also use some_color in function declarations in the same .h, like this one:*/

void do_something(int x, int y, color some_color);

That's not a valid reason to put extern there. The parameter some_color in the function has nothing to do with the declaration above.
Sorry, it should be:

void do_something(int x, int y, color default_color = some_color);

just a "typo"
Quote:Original post by wlw_wl
just a "typo"

Don't paraphrase. Post actual code.

Also, you seem to have an assignment as a top level element. That's not possible in C++. Statements must be embedded in functions.
// from .h

struct wColor {
int r,g,b;
};

extern wColor default_color;

// that is why:
class wVertex {
public:
double x, y, z;
wColor Color;
wVertex(double x0, double y0, double z0 = 0, wColor _color = default_color);
// ...
};


// from .cpp

wColor default_color;

default_color.r = 0; default_color.g = 0; default_color.b = 0;
wMaterial *default_material = new wMaterial(default_color);

//----------------

So what you say is that I should move that to some initialization function or something?

I would suggest you provide a sensible constructor for your color struct (something like, Color(int r, int g, int b)), and then do

void do_something(int x, int y, color default_color = color(0, 0, 0));


instead of attempting to extern a color from somewhere else and using that as your default parameter.

[size=1]Visit my website, rawrrawr.com

Quote:Original post by wlw_wl
wColor default_color;

default_color.r = 0; default_color.g = 0; default_color.b = 0;

So what you say is that I should move that to some initialization function or something?

All I am saying is that statements cannot be written as top level elements.

But concerning your code, you don't need the assignments to zero. Static variables will be default initialized. Just remove the three assignments and all is fine. If you need different values than zero, you can use the struct initializier syntax:
wColor default_color = {1, 2, 3};

Note that this is NOT an assignment but a special syntax for initialization of declared variables. You can write this outside of any function.
Big thanks DevFred! The initializer is just what I (and VS) needed :-)

@bobofjoe I'll use the initializer, but thanks!

This topic is closed to new replies.

Advertisement