• Advertisement
Sign in to follow this  

Initialising const position Vectors

This topic is 4434 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

Hi guys, I want to declare constant postion vectors in my camera class. Eg: // header file static const float DEFAULT_CAMERA_POSITION[3]; // .cpp file const float DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0}; I can't get this to work. Does any1 know how to get this working? I'm using VC++ 2003. Thanks for any help given!

Share this post


Link to post
Share on other sites
Advertisement
Where did you put the variable ? In the class or outside the class ?. If it is outside the class then this should work:
Class.h:
static const float DEFAULT_CAMERA_POSITION[3] ;

Class.cpp:
extern const float FFF[3] = {0.0f, 0.0f, 0.0f} ;


However there're some issues that need to be considered when you use static variables, you may follow this link for more infos.

Share this post


Link to post
Share on other sites
I put it in my Camera class.

static const float DEFAULT_CAMERA_POSITION[3] // in my header

const float DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0}; // in my cpp file

There has to be a nicer way than using extern?

Share this post


Link to post
Share on other sites
If it's in your class then you'll need to do it like this:



//in header

class foo
{
...
static const float DEFAULT_CAMERA_POSITION[3];
...
};

//in cpp

const float foo::DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0};


Share this post


Link to post
Share on other sites
Is it a member of a class? If so then you want const float ClassName::DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0};. If not then you want to use extern (and remove the static qualifier) or change your design so that you are not using a global variable.

As an aside, I would encourage you to reconsider your use of ALL_CAPS_WITH_UNDERSCORES for constants. I also used to use this convention, but ALL_CAPS_WITH_UNDERSCORES is reserved for macros in many coding standards, so I changed.

Enigma

Share this post


Link to post
Share on other sites
Hi guys,

I still can't get this to compile Can any1 see a problem here? I'm getting error;

error C2655: 'Camera::DEFAULT_CAMERA_POSITION' : definition or redeclaration illegal in current scope



//header file
class Camera
{
private:
static const float DEFAULT_CAMERA_POSITION[3];
public:
}

//cpp file
Camera::Camera(int windowWidth, int windowHeight, ThreeDimensions sceneSize, ViewType viewType)
{
const float Camera::DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0};
}
[/SOURCE]


I didn't know about using CAPS for constants. I have them in all my projects but will consider changing if I find a nicer way to present them.

Thanks for the help!

Share this post


Link to post
Share on other sites
Quote:
Original post by Cacks
Hi guys,

I still can't get this to compile Can any1 see a problem here? I'm getting error;

error C2655: 'Camera::DEFAULT_CAMERA_POSITION' : definition or redeclaration illegal in current scope


*** Source Snippet Removed ***[/source]

I didn't know about using CAPS for constants. I have them in all my projects but will consider changing if I find a nicer way to present them.

Thanks for the help!


//header file
class Camera
{
private:
static const float DEFAULT_CAMERA_POSITION[3];
public:
}

//cpp file
/*
moved to up here
*/

const float Camera::DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0};

Camera::Camera(int windowWidth, int windowHeight, ThreeDimensions sceneSize, ViewType viewType)
{
//camera stuff
}



what you were doing is like trying to put one function inside another, as far as the compiler is concerned

Share this post


Link to post
Share on other sites
You should put it outside of any function:
float const ClassName::DEFAULT_CAMERA_POSITION[3] = {0.0, 0.0, 6.0};

Share this post


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

  • Advertisement