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

Author Topic: Constructor problem, need help  (Read 2540 times)

0 Members and 2 Guests are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Constructor problem, need help
« on: May 09, 2022, 09:32:18 pm »
Hi, can someone help me with this following Problem, i wanna make a touch game on android, so i beginn with three touch buttons. I dont know why its not working, any Construcor problem

main.cpp:

#include <SFML/Graphics.hpp>
#include "buttons.h"
#include <iostream>

using namespace sf;
using namespace std;

int main(int argc, char *argv[])
{
        sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Hello SFML");
       
        buttons btn("buttons.png");
        btn.btnInit (3.0, 3.0, 300.0, 250.0, 500.0, 700.0);
       
       
       
        while (window.isOpen())
        {
               
                sf::Event event;
                while (window.pollEvent(event));
               
                window.clear(sf::Color(0, 0, 0));
               
                btn.btnDraw(window);
                window.display();

                       
        }
        return 0;
}
 

Button.h:
#ifndef BUTTONS_H
#define BUTTONS_H

#include <SFML/Graphics.hpp>

using namespace sf;
using namespace std;

class buttons {
       
       
       
       
   public :
       
       
        buttons(string btnPng);
       
        void btnInit(float btnScaleX, float btnScaleY, float btnPosX, float btnLeftPosY, float btnRightPosY, float btnFirePosY);
       
         void btnDraw(RenderWindow &window);

         
        private:
       
        Sprite btnSprite;
        Texture btnText;
         
   
       
};

#endif
 

and Buttons.cpp:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "buttons.h"


using namespace sf;
using namespace std;

buttons::buttons()
{
       
}


buttons::buttons(string btnPng){
       
        btnText.loadFromFile(btnPng);
        btnSprite.setTexture(btnText);
       
}

 void buttons::btnInit(float btnScaleX, float btnScaleY, float btnPosX, float btnLeftPosY, float btnRightPosY, float btnFirePosY){
       
        btnSprite.setScale(btnScaleX, btnScaleY);
        btnSprite.setPosition(btnPosX, btnLeftPosY);
        btnSprite.setPosition(btnPosX, btnRightPosY);
        btnSprite.setPosition(btnPosX, btnFirePosY);
       
       
       
 }
 
 void buttons::btnDraw(RenderWindow &window){
       
        window.draw(btnSprite);
        window.draw(btnSprite);
        window.draw(btnSprite);
       
 }
« Last Edit: May 09, 2022, 09:58:32 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Constructor problem, need help
« Reply #1 on: May 09, 2022, 10:02:13 pm »
And what is not working?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #2 on: May 09, 2022, 10:28:21 pm »
It says: temp.c:(text+0x134):undefined reference to 'buttons::buttons... to everything, i made this via mobilephone c4droid, hmm is it a constructor fault or the fault depends on this programm?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Constructor problem, need help
« Reply #3 on: May 09, 2022, 10:30:45 pm »
You also need to add a default constructor to the buttons header file.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #4 on: May 09, 2022, 11:07:04 pm »
Just this in header?

buttons();

Still dont work,just the same error message


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Constructor problem, need help
« Reply #5 on: May 09, 2022, 11:31:06 pm »
Make sure that buttons.cpp is also built. I don't know how this has to be configured on c4droid though.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Constructor problem, need help
« Reply #6 on: May 10, 2022, 02:07:45 am »
btnInit isn't making three buttons, it's setting the position of a single sprite 3 times in a row. The first 2 setPosition calls are overwritten.

Same with btnDraw, it's drawing the same sprite 3 times on top of itself.

You'll either need to make 3 sprites, or store the values passed to btnInit and do a setPosition before each draw.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #7 on: May 10, 2022, 09:12:41 am »
Is that the right way i built costructors in both files:
Buttons.h
...
public:

Buttons();
....

buttons.cpp

...
Buttons::Buttons(){}
....

?

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #8 on: May 10, 2022, 09:26:49 am »
Thats the error:

/data/user/0/com.n0n3m4.droidc/files/gcc/tmpdir/ccknHbgm.o: In function `main':
temp.c:(.text+0x104): undefined reference to `buttons::buttons1(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >)'
temp.c:(.text+0x13c): undefined reference to `buttons::btnInit(float, float, float, float, float, float)'
temp.c:(.text+0x1a0): undefined reference to `buttons::btnDraw(sf::RenderWindow&)'
collect2: error: ld returned 1 exit status

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #9 on: May 10, 2022, 11:51:09 am »
Okay i got a solution i copy everthing from buttons.cpp and paste it in an empty buttons.h file. But i still wanna know how the connection between two cpp files in between of a header work, is there any easy tutorial for this especially in case of sfml?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Constructor problem, need help
« Reply #10 on: May 10, 2022, 03:18:31 pm »
As said, you need to make sure both main.cpp and buttons.cpp are built.

Brief google search for "c4droid multiple source files" says:

Quote
Long-click compile button (or select "compilation settings" if buttons are hidden/moved) and configure current directory to use the mode you want.

but no idea if this is correct
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Constructor problem, need help
« Reply #11 on: May 10, 2022, 08:47:45 pm »
Yeeah it works that way, Thank you so much eXpl0it3r!!!