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

Author Topic: texture.loadFromFile error: expected constructor, deconstructor or ...  (Read 7128 times)

0 Members and 1 Guest are viewing this topic.

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Hi, I have a header file, which includes the graphics.hpp, in it I make a texture:

sf::Texture texture;
texture.loadFromFile("/data/textures/menu/New Game.pdn");

In another.h file, in which I include this .h file, I create a sprite:

sf::Sprite sprite(texture);



In my main.cpp, I include both of these headers and the graphics.hpp and I do:

window.draw(sprite);
If I now build and run the code, then It errors in the texture.loadFromFile line, saying:

Expected constructor, deconstructo or type conversion before . token

How to fix??
Makerimages-It`s in the pixel

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #1 on: December 29, 2012, 04:28:17 pm »
You are aware that statements must be inside functions? You can't just put them in headers. You should really learn C++ before you use SFML...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #2 on: December 29, 2012, 05:29:13 pm »
I recommend you to start with how C++ compiles, since it appears you have many problems regarding that matter.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #3 on: December 30, 2012, 03:16:27 pm »
You are aware that statements must be inside functions? You can't just put them in headers.
How stupid of me,, that was it.

You should really learn C++ before you use SFML...
I recommend you to start with how C++ compiles, since it appears you have many problems regarding that matter.
Dont get mad at me, my brain crashed in the C++ department for a few hours yesterday...
Makerimages-It`s in the pixel

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #4 on: December 30, 2012, 04:10:10 pm »
Seems, that altough I made a function for those, it now gives scope errors.

TextureManager.h:

#ifndef TEXTUREMANAGER_H_INCLUDED
#define TEXTUREMANAGER_H_INCLUDED

#include <SFML/Graphics.hpp>
using namespace std;
void loadTextures()
{

sf::Texture texture;
 texture.loadFromFile("/data/textures/menu/New Game.pdn");


}
#endif // TEXTUREMANAGER_H_INCLUDED



 

SpriteManager.h:

#ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H
#include <SFML/Graphics.hpp>
#include "TextureManager.h"
using namespace std;
void loadSprites()
{
  sf::Sprite NewGamesprite(texture);
 

}





#endif // SPRITEMANAGER_H



 

main.cpp:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include "Character.h"
#include "TextureManager.h"
#include "SpriteManager.h"
using namespace std;

int main()
{
loadTextures();
loadSprites();
  sf::RenderWindow window(sf::VideoMode(800, 600), "Reckon Dash.exe", sf::Style::Default);


cout <<"Window open"<<endl;
  // run the program as long as the window is open
    while (window.isOpen())
    {


        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
            {
                window.close();

            }
        }

    window.clear();
    window.display();
    window.draw(NewGamesprite);

    }
return 0;
}


 

in the loadsprites it says that texture was not declared in this scope and in main.cpp it says that NewGamesprite was not declared in this scope. How to fix??
Makerimages-It`s in the pixel

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #5 on: December 30, 2012, 04:11:58 pm »
Learn C++.
Back to C++ gamedev with SFML in May 2023

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #6 on: December 30, 2012, 06:45:26 pm »
Can soeone just give me a pointer on what to do tho solve this??
Makerimages-It`s in the pixel

Ideka

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #7 on: December 30, 2012, 07:27:52 pm »
Probably, but you would most likely end up having more and more problems.
Go learn more about the language. Once you do, you'll find that you won't have these kind of problems anymore, and if you do, you'll be able to solve them by yourself.
You could start here: http://www.cplusplus.com/doc/tutorial/ That tutorial is pretty nice, I think. It isn't too long and it covers a good variety of subjects.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #8 on: December 30, 2012, 07:48:47 pm »
Can soeone just give me a pointer on what to do tho solve this??

As I said earlier, start with how C++ compiles, it saves hours of wasted time and poor programming habits.

In texturemanager.h (which should be a class, not a single method) the texture gets destroyed before use as it goes out of scope. In spritemanager.h  (once again should be a class, otherwise just declare them straight in main.cpp) the texture you are trying to assign doesn't exist by that point. And the main function tries to draw a sprite that doesn't exist either.

If you come from Java, C# or any other memory managed language then check this: http://en.sfml-dev.org/forums/index.php?topic=9926

Just ignore the last post, it adds nothing to the issue and C++ performance boost (in comparison to managed languages) is actually a necessary "evil" for game programing.
« Last Edit: December 30, 2012, 07:59:10 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #9 on: December 31, 2012, 11:44:03 am »
Allright I made those to a class (separate .h and .cpp) I make the sprites and textures in constructors. The only problem is that the SpriteManager.cpp doing sf::Sprite NewGamesprite(NewGameTex) says that the NewGameTex was not declared in this scope. Its the only problem I have, how to fix? i have included the TextureManager.h.
Makerimages-It`s in the pixel

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #10 on: December 31, 2012, 11:46:15 pm »
Please don't keep on this and learn the language, if you knew anything about scopes then you would have no problems, you can't expect to use a C++ library fully without knowing something so basic of the language it's written in. However, given you are still asking and haven't actually read anything like it has been said many times run this:


int main()
{
sf::Texture texture;
texture.loadFromFile("/data/textures/menu/New Game.pdn");
sf::Sprite NewGamesprite(texture);

sf::RenderWindow window(sf::VideoMode(800, 600), "Reckon Dash.exe", sf::Style::Default);


std::cout <<"Window open"<<endl;
  // run the program as long as the window is open
    while (window.isOpen())
    {


        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
            {
                window.close();

            }
        }

    window.clear();
    window.draw(NewGamesprite);
    window.display();
   

    }
return 0;
}

 


That should display your sprite, however if you don't learn C++ well you'll just stumble at every single little pebble in your way, if you aren't willing to make an effort then nobody will help you, learn the language first, then use the library as needed. Also try to read a little about the library, that file type may not be supported, use a conventional image type and you'll have no problems.
« Last Edit: December 31, 2012, 11:54:07 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

pwnstar23

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: texture.loadFromFile error: expected constructor, deconstructor or ...
« Reply #11 on: January 07, 2013, 11:45:26 pm »
Can soeone just give me a pointer on what to do tho solve this??

LearnCPP * pLearnCPP = new LearnCPP();

Here you go.  Don't forget to delete it when you're done.