Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]: error: expected initializer before'sf'.  (Read 16335 times)

0 Members and 2 Guests are viewing this topic.

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« on: January 10, 2011, 02:09:21 am »
Ok im having some trouble playing music. Ive tried everything and im not sure how to fix it.

Code: [Select]

#include <iostream>
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <sstream>

using namespace std;
using namespace sf;

int main()
{

    sf::Music Music;

    if (!Music.OpenFromFile("Music\\NLTT.mp3"))
        return EXIT_FAILURE;

    std::cout << "NLTT.mp3: " << std::endl;
    std::cout << " " << Music.GetDuration()      << " sec"           << std::endl;
    std::cout << " " << Music.GetSampleRate()    << " samples / sec" << std::endl;
    std::cout << " " << Music.GetChannelsCount() << " channels"      << std::endl;


    sf::Music::Music();
    bool sf::Music::OpenFromFile(const std::string&)
    sf::Music::GetDuration() const
    sf::SoundStream::GetSampleRate() const
    sf::SoundStream::GetChannelsCount() const
    sf::SoundStream::Play()
    sf::Sound::Status
    sf::SoundStream::GetStatus() const
    sf::Music::~Music()

    Music.Play();
    std::cout << "Playing..." << std::endl;


    while (Music.GetStatus() == sf::Music::Playing)
    {
        sf::Sleep(0.1f);
    }
    std::cout << std::endl;

    std::cin.ignore(10000, '\n');
    return EXIT_SUCCESS;

}
    return 0;
}


This is the error:

In function 'int main()':
|21|error: expected initializer before 'sf'|
||=== Build finished: 1 errors, 0 warnings ===|


The error is pointing to this line:
Code: [Select]
sf::Music::GetDuration() const

Im not sure what is wrong but if anyone could help me it would be appreciated.

Thanks!
- DarK_DemoN -

downtimes

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED]: error: expected initializer before'sf'.
« Reply #1 on: January 10, 2011, 03:13:36 am »
I have no Idea what your trying to do..
its a bit confusing..


first:

Code: [Select]


using namespace std;
using namespace sf;

int main()
{

    sf::Music Music;


if you write:
Code: [Select]

using namespace sf;


you don't need to do the sf:: in
Code: [Select]

sf::Music music;



second:
Code: [Select]

    sf::Music::Music();
    bool sf::Music::OpenFromFile(const std::string&)
    sf::Music::GetDuration() const
    sf::SoundStream::GetSampleRate() const
    sf::SoundStream::GetChannelsCount() const
    sf::SoundStream::Play()
    sf::Sound::Status
    sf::SoundStream::GetStatus() const
    sf::Music::~Music()


is totally wrong. What are you trying to do there?

Maybe you should first try to learn how to Programm C++ before attempting to work
with SFML. It will make your life way easier if you understand the features of the language.

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[SOLVED]: error: expected initializer before'sf'.
« Reply #2 on: January 10, 2011, 03:18:51 am »
You got several problems here.  For one thing, if you are going to use namespaces (in this case std and sf) then you don't need to use sf:: or st:: when making calls.  The point of using a namespace is to avoid that.  For example, if you are using namespace std, you don't need std::cout, just cout.

Ok, for the more serious problems.  Calls in C++ need to end with a semicolon.  None of the ones after sf::Music::Music(); do until you get to Music::Play();

For that matter, what are you trying to do there?

The following...
Code: [Select]
sf::Music Music;

    if (!Music.OpenFromFile("Music\\NLTT.mp3"))
        return EXIT_FAILURE;

    std::cout << "NLTT.mp3: " << std::endl;
    std::cout << " " << Music.GetDuration()      << " sec"           << std::endl;
    std::cout << " " << Music.GetSampleRate()    << " samples / sec" << std::endl;
    std::cout << " " << Music.GetChannelsCount() << " channels"      << std::endl;

...is what you want. All that stuff after is A) not valid c++ and B) does not do anything or act on any object.  Those are just methods of the sf::Music class itself.

Try using this (I took away namespaces since they are not essential here and clearly causing confusion):
Code: [Select]

#include <iostream>
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <sstream>


int main()
{

    sf::Music Music;

    if (!Music.OpenFromFile("Music\\NLTT.mp3"))
        return EXIT_FAILURE;

    std::cout << "NLTT.mp3: " << std::endl;
    std::cout << " " << Music.GetDuration()      << " sec"           << std::endl;
    std::cout << " " << Music.GetSampleRate()    << " samples / sec" << std::endl;
    std::cout << " " << Music.GetChannelsCount() << " channels"      << std::endl;


    Music.Play();
    std::cout << "Playing..." << std::endl;


    while (Music.GetStatus() == sf::Music::Playing)
    {
        sf::Sleep(0.1f);
    }
    std::cout << std::endl;

    std::cin.ignore(10000, '\n');
    return EXIT_SUCCESS;

}


//edit:  downtimes is faster than me :P But the advice is good.  Do some simple c++ tutorials and small program (like Hello World) in order to get the basics of the language.  Also, follow the SFML tutorials as they are extremely helpful when trying to learn SFML  :wink:

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #3 on: January 10, 2011, 03:30:16 am »
@Terrydil

Idid what you said (which ive done a million times before) and i get these errors:

Code: [Select]

undefined reference to `sf::Music::Music(unsigned int)'|
undefined reference to `sf::Music::OpenFromFile(std::string const&)'|
undefined reference to `sf::Music::GetDuration() const'|
undefined reference to `sf::SoundStream::GetSampleRate() const'|
undefined reference to `sf::SoundStream::GetChannelsCount() const'|
undefined reference to `sf::SoundStream::Play()'|
undefined reference to `sf::SoundStream::GetStatus() const'|
undefined reference to `sf::Music::~Music()'|
undefined reference to `sf::Music::~Music()'|
||=== Build finished: 9 errors, 0 warnings ===|


All i am trying to accomplish is getting music (NLTT.mp3) to play while my program is running. I know how to program in C++ fairly well so i do know my way around code and how to code.

One of my friends has tried to help with this issue and couldnt figure it out either and he has alot more expirience in SFML than i do so it isnt just me.

Please help

Thank you
- DarK_DemoN -

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[SOLVED]: error: expected initializer before'sf'.
« Reply #4 on: January 10, 2011, 04:43:57 am »
Quote from: "DarK_DemoN"

Idid what you said (which ive done a million times before) and i get these errors:


If you've done it before you could always look at a previous working version and see what is different.  Did you change your code like we suggested above?  If so, go ahead and post the new code (since its short) so we can see what might be going on.

From looking at your code it looks like you are using the sound.cpp example that comes with SFML 1.6 as a template, correct?  Try building that example to confirm you have everything set up right.  Then try changing it as little as possible in order to make it work for you (in this case, basically just change the file you are feeding it).  Making small changes to a working sample until it breaks is a great way to figure out what might be going wrong.

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #5 on: January 10, 2011, 05:25:50 am »
My code is the same as the one you posted and i get the errors.

Yes i used the music.cpp file as a template and when i compile that project i get the same exact errors as my code/the code you posted.

I honestly have no idea what is wrong. And its very strange that im getting the same errors that i get in my code as the ones i get using the sample. And i would have thought that since its a sample code everything should be correct and working in it.

Thank you for spending your time trying to help me i really appreciate it.
- DarK_DemoN -

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[SOLVED]: error: expected initializer before'sf'.
« Reply #6 on: January 10, 2011, 05:31:41 am »
Sounds like you aren't linking to the libs or your IDE doesn't know where to find them.  Check the SFML tutorial for whatever IDE you are using to see what you might have missed.  What are you linking to and in what order?

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #7 on: January 10, 2011, 05:56:59 am »
I checked and everything is setup correctly according to the tutorials.

What else could it be? I even googled one of the errors and didnt find anything
- DarK_DemoN -

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[SOLVED]: error: expected initializer before'sf'.
« Reply #8 on: January 10, 2011, 06:06:53 am »
Well thats what those errors are, linking errors.  Can you show us your linker options?

Also, I know it sounds dumb but the last time I had a similar problem I fixed it by starting a new project and going back through the tutorial step by step.  It can be easy to miss something silly. :)

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #9 on: January 10, 2011, 06:34:58 am »
[/img]
- DarK_DemoN -

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #10 on: January 10, 2011, 07:01:35 am »
EDIT: Ok once i figured out (actually a guess but lets move on) to add "-lsfml-audio" to my linker settings and copying and pasting the necassary (haha cant spell) dll files to the project folder it finally compiled!! but now i get this:

Failed to open "Music\NLTT.mp3" for reading

This is my "LoadFromFile code:

Code: [Select]

if (!Music.OpenFromFile("Music\\NLTT.mp3"))
        return EXIT_FAILURE;


It should load so i dont know why this is appearing and i have gone back through everything to make sure the file paths are correct and everything is fine.

HELP!!!!

Please ;D
- DarK_DemoN -

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]: error: expected initializer before'sf'.
« Reply #11 on: January 10, 2011, 07:10:29 am »
Is the Music folder placed in the working directory? If you don't know what it is you should be able to find it trough the codeblocks project settings. By default it usually are the project folder.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #12 on: January 10, 2011, 07:33:05 am »
yes its in my project folder and the music folder name is "Music", exactly as i spelled it in my code.

Are there any other settings i need to mess with?
- DarK_DemoN -

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
[SOLVED]: error: expected initializer before'sf'.
« Reply #13 on: January 10, 2011, 08:32:57 am »
EDIT: Ok i fixed it!! The song had to be in .ogg file format because its a compressed audio type i guess. (could have sworn i thought i could use .mp3 but oh well).

Anyway thank you all for your help, i really appreciated it :D
- DarK_DemoN -

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]: error: expected initializer before'sf'.
« Reply #14 on: January 10, 2011, 11:08:15 am »
Ah forgot, mp3 isn't supported because of it's licence.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything