Jump to content
  • Advertisement
Sign in to follow this  
tekmoto

Creating a fixed-size vector in a C++ header file.

This topic is 4581 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 everyone, I get compiler syntax errors when I run the following: =========================================================== // SoundSystem.h #include <vector> #include <stdio.h> #define MAX_CHANNELS 10 using namespace std; class SoundSystem { private: int val1; int val2; int val2; vector<Channel*> channels(MAX_CHANNELS); public: UpdateChannels(); ... }; =========================================================== Visual Studio says the error occurs when I declare that vector. When I take out (MAX_CHANNELS) so that it's not fixed-sized, the code runs. Interestingly, when I declare that vector inside one of my functions (and comment that declaration out in my header), it compiles just fine. The error I get is: error C2059: syntax error: 'constant' error C2059: syntax error: 'constant' fatal error C1903: unable to recover from previous error(s); stopping compilation. I don't understand why I can't declare the fixed-sized array there in the header, but can in functions. The following code also compiles: ========================================================== class SoundSystem { private: int val1; int val2; int val2; public: UpdateChannels() { vector<Channel*> channels(MAX_CHANNELS); } ... }; ========================================================== Is this a Visual Studio bug? For the record, I'm using Visual Studio 2003 .net Thanks for the help, Tek

Share this post


Link to post
Share on other sites
Advertisement
You can construct member variables in the member initializer lists for your constructors, like this:
class SoundSystem
{
public:

SoundSystem();

private:

std::vector<Channel*> channels;
};

SoundSystem::SoundSystem() : channels(MAX_CHANNELS) {}
That should do what you're wanting.

Also it's better to scope explicitly than to have 'using' declarations in your header files.

Share this post


Link to post
Share on other sites
Since you're using a fixed size vector of pointer-types, why not just use a simple array?

Share this post


Link to post
Share on other sites
That works, thanks a lot!

Quote:
Original post by Nypyren
Since you're using a fixed size vector of pointer-types, why not just use a simple array?


Because a vector still has a lot of nice functions a simple array doesn't have, and I want to be consistent :)

Share this post


Link to post
Share on other sites
Quote:
Original post by tekmoto
That works, thanks a lot!

Quote:
Original post by Nypyren
Since you're using a fixed size vector of pointer-types, why not just use a simple array?


Because a vector still has a lot of nice functions a simple array doesn't have, and I want to be consistent :)


Then check Boost.Array out. [smile]

It’ll allow you to use stuff like:
class SoundSystem {
public:
SoundSysmtem() {
std::fill(channels.begin(), channels.end(), new Channel);
}

private:
boost::array<Channel*, MAX_CHANNELS> channels;
};

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!