这个问题是Undefined reference question的结果。
当我尝试编译时:
#include "OriAudioCache.hpp"
int main()
{
System *audioSystem(0);
FMOD_RESULT result;
result = System_Create(&audioSystem);
FMOD_CHECK_STATE(result);
OriAudioCache cache(audioSystem, 20);
string title("Ambitious Girl");
string path("/home/findrzkeeprz/Desktop/Resources/The_Ambitious_Girl.mp3");
cache.LoadSound(title, path, Default);
vector<OriSound>::iterator v_iter(cache.FindSound(title));
cache.PlaySound(v_iter->sound());
}
使用这些文件(直接或间接): OriAudioCache.hpp
#ifndef ORI_AUDIO_CACHE_HPP_
#define ORI_AUDIO_CACHE_HPP_
#include "OriSound.hpp"
#include "OriChannel.hpp"
class OriAudioCache
{
public:
OriAudioCache(System *audioSystem, int maxChannels);
~OriAudioCache()
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter)
{
v_iter->~OriSound();
}
delete audioSystem_;
}
void LoadSound(string const& title, string const& path, AudioLoadMode mode);
vector<OriSound>::iterator FindSound(string const& title);
void RemoveSound(string const& title);
void PlaySound(Sound* sound);
vector<OriChannel>::iterator RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode);
void ReleaseChannel(Channel *channel);
private:
void inline SortChannels() {sort(channels_.begin(),channels_.end());}
vector<OriSound> audioCache_;
vector<OriChannel> channels_;
System *audioSystem_;
};
#endif
OriAudioCache.cpp
#include "OriAudioCache.hpp"
OriAudioCache::OriAudioCache(System *audioSystem, int maxChannels)
:audioSystem_(audioSystem), channels_(maxChannels){}
void OriAudioCache::LoadSound(string const& title, string const& path, AudioLoadMode mode)
{
OriSound sound(title, path, audioSystem_, mode);
vector<OriSound>::iterator pos =lower_bound(audioCache_.begin(), audioCache_.end(), sound);
audioCache_.insert(pos, sound);
}
vector<OriSound>::iterator OriAudioCache::FindSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) return v_iter;
else continue;
}
return audioCache_.end();
}
void OriAudioCache::RemoveSound(string const& title)
{
vector<OriSound>::iterator v_iter(audioCache_.begin());
for(; v_iter != audioCache_.end(); ++v_iter) //Would better if I could use a binary search here
{
if(v_iter->title() == title) audioCache_.erase(v_iter);
else continue;
}
}
void OriAudioCache::PlaySound(Sound* sound)
{
bool channelAlloc(false);
FMOD_CHANNELINDEX allocMode = FMOD_CHANNEL_FREE;
vector<OriChannel>::iterator oriChannel = RequestChannel(channelAlloc, allocMode);
if(channelAlloc)
{
FMOD_RESULT result;
Channel *chnl = oriChannel->channel();
result = audioSystem_->playSound(allocMode, sound, false, &chnl);
FMOD_CHECK_STATE(result);
bool isPlaying(false);
chnl->isPlaying(&isPlaying);
while(isPlaying)
{
chnl->isPlaying(&isPlaying);
}
bool paused(false);
chnl->getPaused(&paused);
if(!paused)
{
ReleaseChannel(chnl);
}
SortChannels(); //sort channels, reoder for channel requests
}
}
vector<OriChannel>::iterator OriAudioCache::RequestChannel(bool &allocStatus, FMOD_CHANNELINDEX &allocMode)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
if(vOri_iter->status() == false)
{
if(vOri_iter->channel() == 0)
{
allocMode = FMOD_CHANNEL_FREE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else allocMode = FMOD_CHANNEL_REUSE;
vOri_iter->setStatus(true); // flag channel as being used
return vOri_iter;
}
else return channels_.end();
}
void OriAudioCache::ReleaseChannel(Channel *channel)
{
bool playing(false);
bool paused(false);
channel->isPlaying(&playing);
channel->getPaused(&paused);
if(!playing && !paused)
{
vector<OriChannel>::iterator vOri_iter(channels_.begin());
for(; vOri_iter != channels_.end(); ++vOri_iter)
{
if(vOri_iter->channel() == channel) vOri_iter->setStatus(false);
}
}
}
OriSound.hpp
#ifndef ORI_SOUND_HPP_
#define ORI_SOUND_HPP_
#include "Public/ThirdParty.hpp"
#include "Public/Public.hpp"
class OriSound
{
public:
OriSound()
:title_(), path_(), sound_(0)
{}
OriSound(string const& title, string const& path, System* const& audioSystem, AudioLoadMode mode)
: title_(title), path_(path)
{
FMOD_RESULT result;
audioSystem->createSound(path.c_str(), mode, 0, &sound_);
FMOD_CHECK_STATE(result);
}
~OriSound()
{
delete sound_;
}
string title() const { return title_;}
string path() const {return path_;}
Sound *sound() const {return sound_;}
private:
string title_;
string path_;
Sound *sound_;
};
bool operator<(OriSound const& lhs, OriSound const& rhs)
{
int result = (lhs.title()).compare(rhs.title());
if(result == 0) return false;
else if(result < 0 ) return true;
else return false;
}
#endif
OriChannel.hpp
#ifndef ORI_CHANNEL_HPP_
#define ORI_CHANNEL_HPP_
#include "Public/Public.hpp"
class OriChannel
{
public:
OriChannel()
:status_(false), channel_(0)
{}
bool status() const {return status_;}
Channel* channel() const {return channel_;}
void setStatus(bool value) { status_ = value;}
private:
bool status_;
Channel *channel_;
};
bool operator<(OriChannel const& lhs, OriChannel const& rhs)
{
return lhs.status() < rhs.status();
}
#endif
Public.hpp
#ifndef PUBLIC_HPP_
#define PUBLIC_HPP_
#include <algorithm>
#include <iostream>
#include <istream>
#include <ostream>
#include <vector>
using namespace std;
#endif
ThirdParty.hpp
#ifndef THIRD_PARTY_HPP_
#define THIRD_PARTY_HPP_
#include "FMODEX/fmod_errors.h"
#include "FMODEX/fmod.hpp"
#include "Public/Public.hpp"
using namespace FMOD;
void FMOD_CHECK_STATE(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
cout << "FMOD error! (" << result << ")" << FMOD_ErrorString(result) << endl;
exit(-1);
}
}
typedef enum
{
Default = FMOD_DEFAULT,
Hardware_2D_CreateStream = FMOD_HARDWARE | FMOD_2D | FMOD_CREATESTREAM ,
Hardware_2D_CreateSample = FMOD_HARDWARE | FMOD_2D | FMOD_CREATESAMPLE,
Hardware_3D_CreateStream = FMOD_HARDWARE | FMOD_3D | FMOD_CREATESTREAM ,
Hardware_3D_CreateSample = FMOD_HARDWARE | FMOD_3D | FMOD_CREATESAMPLE,
Software_2D = FMOD_SOFTWARE | FMOD_2D,
Software_2D_CreateStream = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM ,
Software_2D_CreateSample = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESAMPLE,
Software_2D_CreateCompressedSample = FMOD_SOFTWARE | FMOD_2D | FMOD_CREATECOMPRESSEDSAMPLE,
Software_3D_CreateStream = FMOD_SOFTWARE | FMOD_3D | FMOD_CREATESTREAM ,
Software_3D_CreateSample = FMOD_HARDWARE | FMOD_3D | FMOD_CREATESAMPLE,
Software_3D_CreateCompressedSample = FMOD_SOFTWARE | FMOD_3D | FMOD_CREATECOMPRESSEDSAMPLE
}AudioLoadMode;
#endif
我收到多个定义错误:
findrzkeeprz@Aardvak:~/Documents/Chidori/Engine/Audio$ make
g++ -ggdb -I../../ -I../../Engine -I../../Include -I../../Public -o audio OriAudioCache.cpp main.cpp ../../Libraries/FMODEX/libfmodex.so
/tmp/ccWiUARs.o: In function `FMOD_CHECK_STATE(FMOD_RESULT)':
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/../../Public/ThirdParty.hpp:12: multiple definition of `FMOD_CHECK_STATE(FMOD_RESULT)'
/tmp/ccF7Dod3.o:/home/findrzkeeprz/Documents/Chidori/Engine/Audio/../../Public/ThirdParty.hpp:12: first defined here
/tmp/ccWiUARs.o: In function `operator<(OriSound const&, OriSound const&)':
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/OriSound.hpp:38: multiple definition of `operator<(OriSound const&, OriSound const&)'
/tmp/ccF7Dod3.o:/home/findrzkeeprz/Documents/Chidori/Engine/Audio/OriSound.hpp:38: first defined here
/tmp/ccWiUARs.o: In function `operator<(OriChannel const&, OriChannel const&)':
/home/findrzkeeprz/Documents/Chidori/Engine/Audio/OriChannel.hpp:23: multiple definition of `operator<(OriChannel const&, OriChannel const&)'
/tmp/ccF7Dod3.o:/home/findrzkeeprz/Documents/Chidori/Engine/Audio/OriChannel.hpp:23: first defined here
collect2: ld returned 1 exit status
make: *** [audio] Error 1
我在这里做错了什么?
答案 0 :(得分:3)
您
bool operator<(OriSound const& lhs, OriSound const& rhs)
函数需要标记为inline
,或者需要在.cpp中实现,只需要在.hpp文件中声明。
inline
bool operator<(OriSound const& lhs, OriSound const& rhs)
{
// ....
}
或:
// in OriSound.hpp
bool operator<(OriSound const& lhs, OriSound const& rhs);
// in OriSound.cpp
bool operator<(OriSound const& lhs, OriSound const& rhs)
{
// ....
}