Home > Software engineering >  Problem with initializing an object with a constructor in another class constructor
Problem with initializing an object with a constructor in another class constructor

Time:02-03

this is my first question. I'm a beginner so bear with me.

So, right here:

TitleScreen::TitleScreen(ScreenManager& sm) :
    sm(sm),

// The problem lies here
    play({ 1500.f, 400.f }, tsButtons, "Play", "fonts/OldSchoolAdventures-42j9.ttf"),
    options({ 1500.f, 500.f }, tsButtons, "Options", "fonts/OldSchoolAdventures-42j9.ttf"),
    quit({ 1500.f, 600.f }, tsButtons, "Quit", "fonts/OldSchoolAdventures-42j9.ttf")

There's an error for the Button instances "play", "options", and "quit": "no instance of constructor 'Button::Button' matches the argument list" that use a custom constructor:

Button(const sf::Vector2f&& position, sf::Font& font, const std::string& text, std::string& fontFile);

First, the error was "expected a ')'".

The Button constructor seems and the initializations seem to add up, but it still gives me an error.

The code that I think is necessary to know what's going on:

TitleScreen.h:

#pragma once
#include "Screens.h"
#include "Button.h"
#include "ScreenManager2.h"
#include <SFML/Graphics.hpp>

class TitleScreen : public Screens
{
private:
    Button play;
    Button options;
    Button quit;

    Font tsButtons;

    ScreenManager& sm;

public:
    TitleScreen(ScreenManager& sm);

    void display(sf::RenderWindow& window);

    void update(sf::Time dt);

    void handleInput(sf::Event& event);
};

In TitleScreen.cpp:

TitleScreen::TitleScreen(ScreenManager& sm) :
    sm(sm),

// The problem lies here
    play({ 1500.f, 400.f }, tsButtons, "Play", "fonts/OldSchoolAdventures-42j9.ttf"),
    options({ 1500.f, 500.f }, tsButtons, "Options", "fonts/OldSchoolAdventures-42j9.ttf"),
    quit({ 1500.f, 600.f }, tsButtons, "Quit", "fonts/OldSchoolAdventures-42j9.ttf")
{
    if (!tsButtons.loadFromFile("fonts/OldSchoolAdventures-42j9.ttf"));
    {
        throw "Error: Font not found.";
    }

}

Button.h:

#pragma once

#include <SFML/Graphics.hpp>
#include <string>

class Button
{
public:
    Button(const sf::Vector2f& size, const sf::Vector2f& position, sf::Font& font, const std::string& text, std::string& fontFile);
// This is the constructor I'm trying to use:
    Button(const sf::Vector2f&& position, sf::Font& font, const std::string& text, std::string& fontFile);

Button.cpp:

Button::Button(const sf::Vector2f&& position, sf::Font& font, const std::string& text, std::string& fontFile)
{
    if (!font.loadFromFile(fontFile))
    {
        throw "Error: Font not found";
    }

    //...

Also, if it wasn't apparent, I'm using SFML.

I've tried using chat GPT to answer my question on how to fix it. It's told me to replace Vector2f with curly braces (which I did, as you can see), and that worked, but it's not helping me fix it, as in it's telling me wrong things, like the constructor I'm trying to use has three arguments when it has four (it has some bugs still you know).

Thanks for bearing through.

CodePudding user response:

    Button(
          // ...
            std::string& fontFile);

The fourth parameter to the constructor is a reference to a std::string.

play(
    // ...
      "fonts/OldSchoolAdventures-42j9.ttf"),

The actual attempted value getting passed in is a literal character string. It is not a std::string. It is a const char (&)[ (whatever its size is)].

This kind of a conversion, from a literal character string to a std::string is possible, there is an implicit constructor and a conversion for it, but it produces a temporary object.

This is how C works: implicit conversions result in temporary objects, when used in this kind of a context. In C a reference cannot be bound to a temporary object. Only a reference to a const object can.

You must either change the constructor parameter to a std::string, or a const std::string &, or obtain an actual, living, breathing std::string object from somewhere else, in order to pass it by reference.

I've tried using chat GPT

This is a bot that has no empirical understanding of C , and is not a replacement for a C textbook, unfortunately.

  • Related