未定义的参考问题

时间:2010-08-16 17:00:18

标签: c++

当我尝试编译时:

#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);
        }
    }
}

我得到未定义的引用错误:

findrzkeeprz@Aardvak:~/Documents/Chidori/Engine/Audio$ make
    g++ -ggdb -I../../ -I../../Engine -I../../Include -I../../Public -o audio main.cpp ../../Libraries/FMODEX/libfmodex.so
    /tmp/cctNhPVy.o: In function `main':
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:9: undefined reference to `OriAudioCache::OriAudioCache(FMOD::System*, int)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:12: undefined reference to `OriAudioCache::LoadSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, AudioLoadMode)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:13: undefined reference to `OriAudioCache::FindSound(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    /home/findrzkeeprz/Documents/Chidori/Engine/Audio/main.cpp:14: undefined reference to `OriAudioCache::PlaySound(FMOD::Sound*)'
    collect2: ld returned 1 exit status
    make: *** [audio] Error 1

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

你正在编译OriAudioCache.hpp,当你应该编译OriAudioCache.cpp时,假设该文件包含实现。

答案 1 :(得分:0)

通常,来自编译器(实际上是链接器)的术语“未定义的引用”意味着某些代码片段正在访问链接器无法找到的符号。

这些错误的常见原因:

  • 包含定义的源文件 不包含在构建过​​程中。
  • 源片段(代码行)是 没有删除。
  • 头文件(类声明) 声明给定的方法或符号(未删除或需要实现)。
  • 范围分辨率不正确或缺失 operator(namespace issue)。
  • 使用错误版本的构建过程 源文件(例如未检查的文件 进入CMS。)。

这些错误的大多数情况不是关于C ++引用运算符,也不是解引用指针。

解决方案:

  • 独立眼睛的代码审查。
  • 代码检查工具:Cppcheck, Valgrind,Klocwork等
  • 减少更改,编译更多 常。
  • 使用Test Driven Development。 ; - )