Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:10:17+00:00 2026-06-15T07:10:17+00:00

I am trying to make a simple function or class that selects an image

  • 0

I am trying to make a simple function or class that selects an image and returns it or passes it in some way to a different class. Is it as simple as knowing what type the Image is considered? or do I need to do something else? I am running Code::Blocks 10.05 with GNU GCC compiler on a windows 8 computer. Any help is appreciated.

Thanks to Aesthete, I made some progress. Now I have this:

class Background{
    sf::Image BGI;
    sf::Sprite BG;
    Image& img;

    public:
    void rimage(std::string name){
        sf::Image extra;
        extra.LoadFromFile(name);
        img = extra;
    }
    void init(std::string name){
    BGI = img
    BG.SetPosition(0.f,0.f);
    BG.SetImage(BGI);

    }
};

But when I run it, I get this:

 ...4 error: ISO C++ forbids declaration of 'Image" with no type 

Also,

...10 error: 'img' is defined in this scope

I have included the libraries that I need to run SFML, I just left it out to keep things clean, I adjusted the lines the errors above occurred on to make it easier to follow.

Isn’t img now sort of a global variable within Background?
and I thought Image& was the type of img… What needs to change here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:10:19+00:00Added an answer on June 15, 2026 at 7:10 am

    You don’t need a load method, nor any extra Image objects. You can do all this processing in the constructor.

    class Background{
      private:
        // You only need an image and a background, if that.
        sf::Image BGI;
        sf::Sprite BG;
    
      public:
        // Use a constructor.
        Background(std::string name)
        {
          SetBackground(name, Vector2f(0.f, 0.f));
        }
        void SetBackground(std::string name, sf::Vector2f pos)
        {
          BGI.LoadFromFile(name);
          BG.SetImage(BGI);
          BG.SetPosition(pos);
        }
    };
    
    // Constructor loads image, sets image to sprite, and set sprite position.
    Background bg("MyBackground.png");
    
    // You can change the background image an position like so.
    bg.SetBackgrond("newImage.png", Vector2f(10.f, 20.f));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to make a simple function that will return all the data
I'm trying to a make a simple class in an actionscript file that handles
I am trying to make a simple image rotator that will rotate through a
I'm trying to make simple script using jquery $post function to pass data to
I am trying to make a simple keybinding to the na function. When I
I am trying to make a simple Queue class. Now I am stuck. Its
I'm trying to make a simple accordion style menu using divs: <div class=subdomainLevel> Subdomain
Trying to make a simple form that calculates; the line total, qty total, freight
I am trying to make a navigation that uses simple unordered lists to list
I'm trying to write a very simple function that feeds whatever text someone enters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.