[SOLVED]how do you declare a custom class in mainwindow.h?
-
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!!!
-
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? -
Thanks for the quick reply!
I have included the character class and I will update the mainwindow files in a sec.
Thanks again,
Duncan
-
@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 ?
-
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!
-
It depends: do you need any feature of QObject in that class ?
-
Not exactly sure, changing this post to Solved!
Thanks for your help!