Nick Chapman
Nick Chapman

Reputation: 4634

Trouble using tuple<sf::Texture, bool> in a C++ header file

The Problem Code

I have the following function in my functions.cpp file:

tuple<sf::Texture, bool> load_texture(string texture_path){
    bool success = true;
    sf::Texture texture;
    if (!texture.loadFromFile(texture_path)){
        cout << "Texture failed to load" << endl;
        success = false;
    }
    return make_tuple(texture, success);
}

I am using this with the SFML 2.1 package so that you understand what sf::Texture is in reference to.

I'm trying to do a forward definition of this function in my header.h file like so:

tuple<sf::Texture, bool> load_texture(string texture_path);

But I get the following errors:

I apologize if this is absurdly simple but I'm new to the header game.

My question

What do I need to include in my header file to use the tuples and what do I need to include so that the compiler understands my reference to sf::? Should I be including "SFML\Graphics.hpp"?

If you need more information or code simply let me know.

Upvotes: 0

Views: 411

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"I'm trying to do a forward definition of this function in my header.h file like so:"

tuple<sf::Texture, bool> load_texture(string texture_path);

That's not really what makes up a forward declaration, but just a simple function declaration.

The problem indicated by the compiler errors merely says you're missing a complete declaration for the sf::Texture class at that point in the declaration.

To get around this, you need to #include <Texture.hpp> in your header.h file. Also you'll need to #include <tuple> of course.


header.h

#if !defined(HEADER_H__)
#define HEADER_H__
#include <Texture.hpp>
#include <tuple>
#include <string>

std::tuple<sf::Texture, bool> load_texture(std::string texture_path);
#endif // HEADER_H__

functions.cpp

#include "header.h"

std::tuple<sf::Texture, bool> load_texture(std::string texture_path) {
    bool success = true;
    sf::Texture texture;
    if (!texture.loadFromFile(texture_path)) {
        cout << "Texture failed to load" << endl;
        success = false;
    }
    return make_tuple(texture, success);
}

Upvotes: 1

Related Questions