I declare my own class in the main class
class MainWindow : public QMainWindow.
like this: (I’m going to put in a bit of excess, so that you can quickly find the fragments)
private: Ui::MainWindow *ui; Control control;//собственно мой класс
Then I try to use it in the implementation of this class (when pressed, it should work):
void MainWindow::on_but_1_pressed(){ ui->but_1->setStyleSheet("QPushButton{background-color: red;}"); control.usrTurn(0);//вот здесь должна сработать функция //и внести важные данные в объект класса. }
What a disgrace ??:
mainwindow.obj: -1: error: LNK2019: link to unresolved external character "
public: __cdecl Control::Control(void)
" (??0Control@@QEAA@XZ
) in the function "public: __cdecl MainWindow::MainWindow(class QWidget *)
"(??0MainWindow@@QEAA@PEAVQWidget@@@Z
)mainwindow.obj: -1: error: LNK2019: link to unresolved external character "
public: void __cdecl Control::usrTurn(int)
" (?usrTurn@Control@@QEAAXH@Z
) in the function "private: void __cdecl MainWindow::on_but_1_pressed(void)
"(?on_but_1_pressed@MainWindow@@AEAAXXZ
)
And govnokod in person :)
Main function: (nothing interesting)
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
Class declaration:
#ifndef CONTROL_H #define CONTROL_H class Control { //для управления полем char game[8]; //для создание игроков(присвоение х или о) const char O = 'O'; const char X = 'X'; void setGame(int index, char player);//установить значение в индекс void inPlay();//создать игроков/хрестик ходит первый char player_usr; char player_comp; public: Control(); char getUsr(); char getComp(); //game sector //char getGame(int index);//какое значение на месте index void usrTurn(int index);//пользователь ходит на индекс клетку void compTurn();//копмп ходит//ии }; #endif // CONTROL_H
Implementation (class unfinished, some functions are not implemented. It does not matter ...):
#include "control.h" #include <iostream> #include <Windows.h> #include <conio.h> #include <ctime> #include <random> #include "Debag.h"//абсолютно рабочий using namespace std; Control::Control() { for(int i = 0; i < 9; i++) game[i] = 'N'; inPlay(); } void Control::usrTurn(int index) { const char PLAYER = player_usr; setGame(index, PLAYER); } void Control::setGame(int index, char player) { if (index < 0 || index > 8) {//ERROR_01 log("01", "some problem whith index"); exit(1); } if (game->GetValue(index) == 'N') { game[index] = player; } else {//ERROR_02 log("02", "There no empty place (ControlPros.cpp)"); exit(1); } } void Control::inPlay() { srand(time(0)); (rand() % 2); if (rand() % 2 == 0) { player_usr = X; player_comp = O; } else { player_usr = O; player_comp = X; } } char Control::getUsr() { return player_usr; } char Control::getComp() { return player_comp; }
I think everything works as it should, something is wrong in the Qt classes:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<control.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_but_1_pressed(); void on_but_1_released(); void on_but_2_pressed(); void on_but_2_released(); private: Ui::MainWindow *ui; Control control;// я пытаюсь объявить класс }; #endif // MAINWINDOW_H
Here I am trying to apply it:
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->but_1->setStyleSheet("QPushButton{background-color: blue;}"); ui->but_2->setStyleSheet("QPushButton{background-color: blue;}"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_but_1_pressed(){ ui->but_1->setStyleSheet("QPushButton{background-color: red;}"); control.usrTurn(0);//вот здесь я пытаюсь его использовать } void MainWindow::on_but_1_released(){ ui->but_1->setStyleSheet("QPushButton{background-color: blue;}"); } void MainWindow::on_but_2_pressed(){ ui->but_2->setStyleSheet("QPushButton{background-color: red;}"); } void MainWindow::on_but_2_released(){ ui->but_2->setStyleSheet("QPushButton{background-color: blue;}"); }
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { }
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { }
does not inspire confidence. And#include<control.h>
too. Try to simplify the namespace (remove all) and files (cram everything into one pair of h + cpp), plus the normal names of the classes would do well to give. If everything works, there will be a starting point from which you can move in the right direction. - Athari