Jump to content
  • Advertisement
Sign in to follow this  
Finalspace

How to initialize a static field in c++?

This topic is 370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following code which have 4 static members in the struct. Why can i not initialize it?

I just want a bunch of predefined vectors i can use -.-

 

Error i get: LNK2005    "public: static union Vec2f const Vec2f::Up" (?Up@Vec2f@@@2T123@A) already defined

		union Vec2f {
			struct {
				f32 x;
				f32 y;
			};
			struct {
				f32 w;
				f32 h;
			};
			f32 elements[2];

			inline Vec2f() {
				x = y = 0;
			}
			inline Vec2f(f32 value) {
				x = y = value;
			}
			inline Vec2f(f32 newX, f32 newY) {
				x = newX;
				y = newY;
			}
			inline Vec2f(const Vec2f &from) {
				x = from.x;
				y = from.y;
			}
			
			static const Vec2f Up;
			static const Vec2f Down;
			static const Vec2f Left;
			static const Vec2f Right;
		};

		const Vec2f Vec2f::Up = Vec2f(0, 1);
		const Vec2f Vec2f::Down = Vec2f(0, -1);
		const Vec2f Vec2f::Left = Vec2f(-1, 0);
		const Vec2f Vec2f::Right = Vec2f(1, 0);

 

Edited by Finalspace

Share this post


Link to post
Share on other sites
Advertisement

How about

struct Vec2f {
	union {
		struct {
			f32 x;
			f32 y;
		};
		struct {
			f32 w;
			f32 h;
		};
		f32 elements[2];
	};
  // ...

 

Share this post


Link to post
Share on other sites
6 minutes ago, _Silence_ said:

Put the initialization in the source file, not the header file.

There is no source file, its a math header file. But well another source file wont hurt... so putting the initialization stuff into the source file works. Thanks.

7 minutes ago, Sponji said:

How about


struct Vec2f {
	union {
		struct {
			f32 x;
			f32 y;
		};
		struct {
			f32 w;
			f32 h;
		};
		f32 elements[2];
	};
  // ...

 

How about what? I dont see anything at all...

Edited by Finalspace

Share this post


Link to post
Share on other sites
11 minutes ago, Finalspace said:

How about what? I dont see anything at all...

1

Yeah my bad, not sure what I was thinking, dunno why it looked so weird for me having a union with constructors. Didn't notice it was a linker error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!