#no appropriate default constructor

106 messages Ā· Page 1 of 1 (latest)

lone stratus
#

Why isn't the constructor recognized?

#pragma once
#include <iostream>
#include <string>
#include <raylib.h>


class TextBox {
public:
    TextBox(Vector2 Pos, Vector2 Size, std::string PlaceholderText, std::string Mask, int32_t MaxChars);

    void Draw() const;

    void Update();

    void SetFont(Font font);

private:
    Vector2 m_Pos;
    Vector2 m_Size;
    int32_t m_MaxChars;
    Font m_Font;
    std::string m_Text;
    std::string m_PlaceholderText;
    std::string m_Mask;

    std::string GetPasswordText() const;
};
#include "Textbox.h"


TextBox::TextBox(Vector2 Pos, Vector2 Size, std::string PlaceholderText, std::string Mask, int32_t MaxChars) {
    this->m_Pos = Pos;
    this->m_Size = Size;
    this->m_PlaceholderText = PlaceholderText;
    this->m_Mask = Mask;
    this->m_MaxChars = MaxChars;
}

void TextBox::Draw() const {
    ...
}

void TextBox::Update() {
    ...
}

void TextBox::SetFont(Font font) {
    ...
}

std::string TextBox::GetPasswordText() const {
    ...
}
drowsy cipherBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

tired mica
#

Why isn't the constructor recognized?

that's not the issue

#

the issue is that the type has no default constructor

#

you need to put the TextBox constructor call into the member initializer list

lone stratus
#

TextBox::TextBox(Vector2 Pos, Vector2 Size, std::string PlaceholderText="", std::string Mask="", int32_t MaxChars) ?

tired mica
#

that still won't work

#
MainMenu::MainMenu(...)
  : TextBox(... put args here ...)
{
  ...
}

and get rid of the this->textbox = part

lone stratus
#

soo.. in mainmenu.h should i replace TextBox xx; with TextBox* xx;?

lone stratus
#

but then how does Button work ?

#

the constructors are similar

tired mica
lone stratus
#

no

#

and it works

tired mica
#

is that the only constructor it has?

lone stratus
#

yep

#
#pragma once
#include <iostream>
#include <string>
#include <map>
#include <raylib.h>


namespace ButtonProperties {
    ...
}

struct ButtonProperties::ButtonColors {
    ...
};

enum class ButtonProperties::ButtonAlignment {
    CENTER,
    LEFT,
    RIGHT
};

struct ButtonProperties::ButtonFont {
    ...
};

struct ButtonProperties::ButtonStyle {
    ...
};


class Button {
private:
    std::string m_Text;
    Vector2 m_Pos;
    Vector2 m_Size;
    Font m_Font;
    ButtonProperties::ButtonStyle m_ButtonStyle;
    std::string m_ID;
    void Tick();

public:
    Button(std::string text, std::string ID, Vector2 pos, Vector2 size, ButtonProperties::ButtonStyle style);
    void Draw();
    bool IsHovered();
    bool IsClicked();
    std::string GetText();
    std::string GetID();
};
#
#include "Button.h"


Button::Button(std::string text, std::string ID, Vector2 pos, Vector2 size, ButtonProperties::ButtonStyle buttonStyle) {
    this->m_Text = text;
    this->m_ID = ID != "" ? ID : text;
    this->m_Pos = pos;
    this->m_Size = size;
    this->m_ButtonStyle = buttonStyle;
}

void Button::Draw() {
    ...

}

void Button::Tick() {

}

// TO REMOVE v3.12a2
//inline ButtonFlags operator|(ButtonFlags a, ButtonFlags b) {
//    return static_cast<ButtonFlags>(static_cast<int32_t>(a) | static_cast<int32_t>(b));
//}

bool Button::IsHovered() {
    Vector2 mousePos = GetMousePosition();
    if (mousePos.x >= this->m_Pos.x and mousePos.x <= this->m_Pos.x + this->m_Size.x and mousePos.y >= this->m_Pos.y and mousePos.y <= this->m_Pos.y + this->m_Size.y) {
        return true;
    }
    return false;
}

bool Button::IsClicked() {
    if (this->IsHovered() and IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
        return true;
    }
    return false;
}

std::string Button::GetText() {
    return this->m_Text;
}

std::string Button::GetID() {
    return this->m_ID;
}
tired mica
lone stratus
#

no

tired mica
#

what does it say now?

lone stratus
#

idk how to do that 😦

tired mica
#

do what?

lone stratus
#

the default constructor

tired mica
#

have you tried the code I sent?

lone stratus
#

ye

tired mica
#

does it work?

lone stratus
#

no

tired mica
#

what does the compiler say?

lone stratus
#

constructors doesnt match

#

or sth like that

tired mica
#

can you show the code?

#

MainMenu::MainMenu()

lone stratus
#

let me rewrite it šŸ™‚

lone stratus
#
MainMenu::MainMenu(ButtonProperties::ButtonStyle buttonStyle) {
    this->m_ButtonStyle = buttonStyle;
    this->m_Buttons.push_back(Button("Play", "1#ButtPlay", Vector2(500, 500), Vector2(200, 50), this->m_ButtonStyle));
    this->m_Buttons.push_back(Button("Profile", "1#ButtProfile", Vector2(500, 600), Vector2(200, 50), this->m_ButtonStyle));

    this->m_TextBox = TextBox(Vector2(200, 200), Vector2(150, 30), "First box", "", 20);

}
tired mica
#

that does not look like the code I sent at all

lone stratus
#

well i reverted it because it didnt work

#

i ll rewrite it in a moment

tired mica
#

I see why Button works now

lone stratus
tired mica
#

oops

#

my bad

#

it should be : m_TextBox(...)

lone stratus
#

MainMenu::MainMenu(ButtonProperties::ButtonStyle buttonStyle) : m_TextBox(Vector2(200, 200), Vector2(150, 30), "First box", "", 20){
    this->m_ButtonStyle = buttonStyle;
    this->m_Buttons.push_back(Button("Play", "1#ButtPlay", Vector2(500, 500), Vector2(200, 50), this->m_ButtonStyle));
    this->m_Buttons.push_back(Button("Profile", "1#ButtProfile", Vector2(500, 600), Vector2(200, 50), this->m_ButtonStyle));

    this->m_TextBox = TextBox(Vector2(200, 200), Vector2(150, 30), "First box", "", 20);

}

tired mica
#

well, the original problem is solved

tired mica
#

it's not needed

lone stratus
#

same error

tired mica
#

I know

#

now it's a linker error

#

is TextBox.cpp (or wherever TextBox::TextBox() is) properly added to the project?

lone stratus
#

yes

#

but i don t understant why Button works and TextBox doesn't?

tired mica
#

because you never try to construct a Button with the default constructor

lone stratus
#

well, i am kinda new to OOP, can you explain default constructors?

#

why isn't the custom one used?

tired mica
#

the default constructor is the one that has no arguments

tired mica
#

what you need to understand is by the point the constructor's body is entered, all of the members are already constructed

#

so if you don't initialize a member in the member initializer list or in the class definition, then it will be initialized using its default constructor

#

but TextBox doesn't have one, so hence the error

lone stratus
#

TextBox* m_TextBox;

#

m_TextBox = new TextBox(...);

#

would this solve it ?

tired mica
#

with that you just add an unnecessary allocation and indirection

#

yes, it would "solve" the problem, but it's not quite the best solution

#

and it still wouldn't solve the linker issue that you're now having

lone stratus
#

what if i define 2 constructors?

tired mica
#

that could work as well

#

but you really should use the member initializer list to initialize the members of an object

lone stratus
#

what s that >

#

?

tired mica
#
MainMenu::MainMenu() : m_TextBox(...), m_whatever(...), ...
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - member initializer list
{

}
lone stratus
#

and what does it do differently ?

tired mica
#
int a = 42;

vs.

int a;
a = 42;
#

the first one corresponds to the member initializer list, the second one to those this->x = ... assignments

tired mica
lone stratus
#

and what should i do now ? šŸ˜®ā€šŸ’Ø

tired mica
#

can you try a clean rebuild

lone stratus
#

it s the sa,e

#

same*

tired mica
#

can you show the build log?

lone stratus
#
Build started...
1>------ Build started: Project: Sudoku, Configuration: Debug x64 ------
1>imgui.cpp
1>imgui_demo.cpp
1>imgui_draw.cpp
1>imgui_tables.cpp
1>imgui_widgets.cpp
1>rlImGui.cpp
1>Board.cpp
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Board.cpp(38,65): warning C4244: 'initializing': conversion from 'int' to 'float', possible loss of data
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Board.cpp(38,108): warning C4244: 'initializing': conversion from 'int' to 'float', possible loss of data
1>Button.cpp
1>MainMenu.cpp
1>Resources.cpp
1>Sudoku.cpp
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(35,42): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(47,77): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(53,147): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(53,104): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(63,127): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data

#
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\src\Sudoku.cpp(63,98): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
1>SudokuGenerator.cpp
1>Textbox.cpp
1>Tile.cpp
1>UI.cpp
1>Utils.cpp
1>Generating Code...
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>MainMenu.obj : error LNK2019: unresolved external symbol "public: __cdecl TextBox::TextBox(struct Vector2,struct Vector2,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int)" (??0TextBox@@QEAA@UVector2@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@1H@Z) referenced in function "public: __cdecl MainMenu::MainMenu(struct ButtonProperties::ButtonStyle)" (??0MainMenu@@QEAA@UButtonStyle@ButtonProperties@@@Z)
1>C:\Users\Sound\Desktop\Projects\Sudoku-CPP\Sudoku\x64\Debug\Sudoku.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Sudoku.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build started at 9:00 PM and took 17.693 seconds ==========

tired mica
#

i have no clue why it cannot find the function...

lone stratus
#

well the next best solution i think is to make 2 constructors, right ?

tired mica
#

I don't see why that would solve the linker issue

lone stratus
#

it doesn t but it might work :/

tired mica
#

if anything, I'd expect you to get 2 "unresolved external symbol" errors

#

I find it very unlikely that it'll solve the issue

lone stratus
#

it works but crashes 😦

#

this i my fault actually..

#

wait

#

i don t think the parameters from the custom constructor get set...

#

and i try to use undeclared stuff

#

am i right ?

tired mica
#

?

lone stratus
#

!solved