Question

i am creating a project using SFML. Everything worked fine up until now. I created a class called BufferHelper.cpp and then an error showed up.

I am working with: Visual Studio Professional 2013 SFML 2.1 Windows 8.1

I have added all libaries

the 'sfml-lib.dll's for Release and 'sfml-lib-d.dll's for Debug

Error log:

Error   1   error LNK2001: unresolved external symbol "public: class sf::SoundBuffer __thiscall BufferHelper::MergeBuffers(class sf::SoundBuffer,class sf::SoundBuffer)" (?MergeBuffers@BufferHelper@@QAE?AVSoundBuffer@sf@@V23@0@Z)    C:\Users\Krisjanis\documents\visual studio 2013\Projects\BD_Test\BD_Test\main.obj   BD_Test
Error   2   error LNK1120: 1 unresolved externals   C:\Users\Krisjanis\documents\visual studio 2013\Projects\BD_Test\Release\BD_Test.exe    BD_Test

Main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "BufferHelper.h"

int main()
{
    sf::SoundBufferRecorder recorder;
    sf::SoundBufferRecorder recorder2;

    sf::SoundBuffer buffer;
    sf::SoundBuffer buffer2;

    sf::Sound sound;
    sf::Sound sound2;

    int iTrackActive = 1;

    bool bTrack1Active = true;
    bool bTrack2Active = false;

    sf::RenderWindow window(sf::VideoMode(1200, 800), "Loopify!");
    //sf::RenderWindow window(sf::VideoMode(1200, 800), "Loopify!", sf::Style::Fullscreen);
    sf::Texture texture;
    texture.loadFromFile("images/a.png");

    BufferHelper a; 
    sf::SoundBuffer finalbuffer = a.MergeBuffers(buffer, buffer2);

    sf::Sprite sprite;
    sprite.setTexture(texture);

    bool recording = false;
    if (!sf::SoundBufferRecorder::isAvailable())
    {
        std::cout << "Recorder not available";
    }
    else
    {
        std::cout << "GO AHEAD AND RECORD";
    }

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Space && !recording)
                {
                    recording = true;
                    recorder.start();

                }
                else if (event.key.code == sf::Keyboard::Space && recording)
                {
                    recorder.stop();
                    recording = false;
                    if (iTrackActive == 1)
                    {
                        buffer = recorder.getBuffer();
                        sound.setBuffer(buffer);
                        sound.setLoop(true);
                        sound.play();
                    }
                    if (iTrackActive == 2)
                    {
                        buffer2 = recorder.getBuffer();
                        sound2.setBuffer(buffer);
                        sound2.setLoop(true);
                        sound2.play();
                    }
                }
                else if (event.key.code == sf::Keyboard::Num1)
                {
                    iTrackActive = 1;
                }
                else if (event.key.code == sf::Keyboard::Num2)
                {
                    iTrackActive = 2;
                }
            }
        }
        window.clear();
        window.draw(sprite);
        window.display();
    }

    return 0;
}

BufferHelper.cpp

    #include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include "BufferHelper.h"
#include <iostream>
#include <vector>


BufferHelper::BufferHelper()
{

}

sf::SoundBuffer MergeBuffers(sf::SoundBuffer BufferOne, sf::SoundBuffer BufferTwo)
{

    int iLength;
    int iShortLength;
    const sf::Int16* LongerSamples;
    const sf::Int16* ShorterSamples;

    if (BufferOne.getSampleCount() > BufferTwo.getSampleCount())
    {
        LongerSamples = BufferOne.getSamples();
        ShorterSamples = BufferTwo.getSamples();
        iLength = BufferOne.getSampleCount();
        iShortLength = BufferTwo.getSampleCount();
    }
    else
    {

        LongerSamples = BufferTwo.getSamples();
        ShorterSamples = BufferOne.getSamples();
        iLength = BufferTwo.getSampleCount();
        iShortLength = BufferOne.getSampleCount();
    }

    std::vector<sf::Int16> FinalSamplesVector;
    FinalSamplesVector.reserve(iLength);

    for (int i = 0; i < iLength; i++)
    {
        if (i < iShortLength)
        {
            double dSampleOne = (LongerSamples[i] + 32768.) / 65535.;
            double dSampleTwo = (ShorterSamples[i] + 32768.) / 65535.;
            double dResult = 0;

            if (dSampleOne < 0.5 && dSampleTwo < 0.5)
            {
                dResult = 2 * dSampleOne * dSampleTwo;
            }
            else
            {
                dResult = 2 * (dSampleOne + dSampleTwo) - 2 * dSampleOne * dSampleTwo - 1;
            }

            FinalSamplesVector.push_back(static_cast<sf::Int16>(dResult * 65535. - 32768.));
        }
        else
        {
            FinalSamplesVector.push_back(LongerSamples[i]);
        }
    }

    sf::SoundBuffer FinalBuffer;
    FinalBuffer.loadFromSamples(&FinalSamplesVector[0], FinalSamplesVector.size(), 2, 44100);

    return FinalBuffer;
}

BufferHelper::~BufferHelper()
{
}

BufferHelper.h

#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace sf;
class BufferHelper
{
public:
    BufferHelper();

    sf::SoundBuffer MergeBuffers(sf::SoundBuffer, sf::SoundBuffer);

    ~BufferHelper();
};

I will be thankful to recieve any kind of feedback.

Was it helpful?

Solution

You did not declare the body of the function MergeBuffers(). This has nothing to do with SFML, just add

sf::SoundBuffer BufferHelper::MergeBuffers(sf::SoundBuffer, sf::SoundBuffer)
{
// some code
}

in your cpp file.

Edit : I realize you mistakenly re-copied your main.cpp file code. Please show the good code for BufferHelper.cpp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top