Jump to content
  • Advertisement
Sign in to follow this  
namingway

C++ help

This topic is 3670 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'm not quite sure what the problem is but my code breaks with this line
_sound = new FMOD::Sound*[4];
	std::string file[4];
	//create all sound effects
	for(unsigned int i = 0; i < 4; i++)
	{
		this line here--->file = globalFileDirectories->GetSoundFiles().file_string();
		_soundEngine->createSound(file.c_str(), FMOD_HARDWARE, 0, &_sound); 
	}

here's some of the related code. FMOD::Sound** _sound; std::vector<boost::filesystem::path> _soundFiles; std::vector<boost::filesystem::path> GetSoundFiles() {return _soundFiles;} Basically I'm trying to create all of the sounds at the start of the game but when I go to load the filenames it breaks...

Share this post


Link to post
Share on other sites
Advertisement
Well off the top of my head, I suspect that the problem may lay in "operator precedence". I.e. your function call GetSoundFiles() gets called first, then the array element GetSoundFiles() is accessed, then the member selection comes into effect GetSoundFiles().file_string() and then finally the member selection via pointer globalFileDirectories->GetSoundFiles().file_string().

This should be the order of things. So perhaps the introduction of some brackets, to force the correct order, may be called for here...

Share this post


Link to post
Share on other sites
Quote:
Original post by TerrorFLOP
Well off the top of my head, I suspect that the problem may lay in "operator precedence". I.e. your function call GetSoundFiles() gets called first, then the array element GetSoundFiles() is accessed, then the member selection comes into effect GetSoundFiles().file_string() and then finally the member selection via pointer globalFileDirectories->GetSoundFiles().file_string().

This should be the order of things. So perhaps the introduction of some brackets, to force the correct order, may be called for here...



I cannot help with FMOD, but it is like above quote.

globalFileDirectories->GetSoundFiles()


... is semantically nothing else then

SomeClass::GetSoundFiles( globalFileDirectories )


Your compiler will make static functions out of all your member functions, and the "this" pointer is given in the argument list. And then, every referenced member variable/function/whatever in the function GetSoundFiles is prefixed with this. Like ...


class Alpha {
private:
const int bravo;
public:
Alpha (int bravo) : bravo(bravo) {}

...

int getBravo() const {
return bravo;
}
};


... will be translated to this, after checking scope,visibility,etc. (just one example of many):


struct Alpha {
const int bravo;
};

Alpha$$Alpha (Alpha * const this, int bravo) {
this->bravo = bravo;
}

int Alpha$$GetBravo (const Alpha * const this) {
return this->bravo;
}

Share this post


Link to post
Share on other sites
std::vector<boost::filesystem::path> GetSoundFiles() {return _soundFiles;}


This is bad. Here you are creating a copy of your _soundFiles vector to return from the function. If there are 100 sound files ready to be loaded, 100 path objects will be copied four times in your for loop (copying 400 objects just to access one of them!).

What you want to do is return a reference to it (essentially a safer pointer) and you may also want to typedef that vector type.

This isnt the error in your code however.

typedef std::vector<boost::filesystem::path> path_vector;
path_vector _soundFiles;
// the entire vector of strings (or whatever boost::path contains) will not be copied because your returning a reference to _soundFiles
path_vector& GetSoundFileList () { return _soundFiles; }

Share this post


Link to post
Share on other sites
I tried using the reference but the same error came about... and I used the typedef, certainly saves alot of typing ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by thre3dee
*** Source Snippet Removed ***

This is bad. Here you are creating a copy of your _soundFiles vector to return from the function. If there are 100 sound files ready to be loaded, 100 path objects will be copied four times in your for loop (copying 400 objects just to access one of them!).

What you want to do is return a reference to it (essentially a safer pointer) and you may also want to typedef that vector type.

This isnt the error in your code however.

*** Source Snippet Removed ***


just one addition (could somebody please tell me about hte syntax of your code-display :P):

You want your "get"-methods be constant:


const path_vector& GetSoundFileList () const { return _soundFiles; }


This helps your compiler with optimisation as well as it helps the users of your class not doing funky, non-intended things with it.

If you really want to return a non-constant reference, then it should be named "GetSoundFileListReference" or similar.

Share this post


Link to post
Share on other sites
Quote:
Original post by greenhybrid
This helps your compiler with optimisation.


Not really... optimisation would only occur in very specific corner cases where the compiler has access to everything about the object being manipulated.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!