Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]how do you declare a custom class in mainwindow.h?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]how do you declare a custom class in mainwindow.h?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DuncanD Offline
    DuncanD Offline
    Duncan
    wrote on last edited by Duncan
    #1

    Greetings!

    How do you declare an object in mainwindow.h so you can use the classes' object properties in functions in mainwindow.cpp?
    The error I am receiving is "mainwindow.cpp:34: error: no matching constructor for initialization of 'Character'
    test = new Character(this);
    ^ ~~~~

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "combattext.h"
    #include "character.h"
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <qglobal.h>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    namespace Ui
    {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        int x = 250;
        int y = 250;
        int z = 250;
    
    
    
    private slots:
        void on_actionNew_Window_triggered();
    
        void on_pushButton_2_clicked();
    
        void on_moveN_clicked();
    
    
    
    private:
        Ui::MainWindow *ui;
        CombatText *combat;
      //  Character *PC7;
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <qglobal.h>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "combattext.h"
    #include "character.h"
    
    
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        srand(time(NULL));
        time_t timer;
        long int sTime = 0; // Start Time
        sTime = time(&timer);
    
    
       //Character PCOne(this);
       //Character *PC7 = new Character(this);
        Character PCOne;
        Character NPCOne;
     //   PCOne = new Character(this);
    
    

    character.h file:

    #ifndef CHARACTER_H
    #define CHARACTER_H
    #include <iostream>
    #include <stdio.h>      /* printf, scanf, puts, NULL */
    #include <stdlib.h>     /* srand, rand */
    #include <time.h>       /* time */
    #include <iostream>
    #include <fstream>
    #include <qglobal.h>
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDialog>
    
    using namespace std;
    
    class Character
    {
    
    
    public:
    
    
            int gold;
      
    
    
    
        Character();
     
        void setCharacter(Character &Char);
    
    };
    
    #endif // CHARACTER_H
    

    character.cpp file:

    #include "character.h"
    
    Character::Character()
    {
    
    }
            int Character::setGold()
            {
    
    
                int rand1 = 0;
                rand1 = rand() % 50;
                gold = rand1;
    
                return gold;
    
            }
    
            void Character::setCharacter(Character &Char)
            {
    
                Char.setGold();
    
            }
    
    

    Thanks for your help!!!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      How and where is Character declared?
      Which kind of constructors are defined for that class"?
      Have you included the header file?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • DuncanD Offline
        DuncanD Offline
        Duncan
        wrote on last edited by
        #3

        Thanks for the quick reply!

        I have included the character class and I will update the mainwindow files in a sec.

        Thanks again,

        Duncan

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Duncan said:

          //Character PCOne(this);
          //Character *PC7 = new Character(this);
          Character PCOne;
          Character NPCOne;
          // PCOne = new Character(this);

          Your Character class constructor doesn't take any argument so I guess you had the error when building your project with PC7 or PCOne, right ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • DuncanD Offline
            DuncanD Offline
            Duncan
            wrote on last edited by
            #5

            Thanks for the reply SGaist!!

            I had to start a new project and create QObject classes in order to access the data in my declared objects :P

            The Character class works great when its a QObject class, should i be doing it this way??

            Thanks for the help!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It depends: do you need any feature of QObject in that class ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • DuncanD Offline
                DuncanD Offline
                Duncan
                wrote on last edited by
                #7

                Not exactly sure, changing this post to Solved!

                Thanks for your help!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved