I am trying to create an object of my class ArduinoControl as a class variable in my QT QMainWindow class.
I have included the ArduinoControl header file in the Main Window header file.
I want to declarate the Arduino object, without initializing it, this will be done in the constructor of the MainWindow object, so that all functions can access the Arduino Object.
This is my QMainWindowClass:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_laserbasictestfunctions.h"
#include <QTextEdit>
#include <QLineEdit>
#include <QLabel>
#include "PrintBlock.h"
#include <stdio.h>
#include "ArduinoCommunication.h"
#include <QThread>
#include <QtTest/QtTest>
class LaserBasicTestFunctions : public QMainWindow
{
Q_OBJECT
public:
LaserBasicTestFunctions(QWidget *parent = Q_NULLPTR);
void checkActivity();
private:
Ui::LaserBasicTestFunctionsClass ui;
int programNumber;
bool ok;
bool rotate;
char portName[11];
ArduinoCommunication tableControl;
private slots:
//Tab#1 Laser Test Functions
void on_coordinateSystemButton_clicked();
void on_rectangleButton_clicked();
void on_pointButton_clicked();
void on_stopLaserButton_clicked();
void on_startButton_clicked();
//Tab#2 Print Block Functions
void on_stopLaserButton_2_clicked();
void on_startPrintBlockButton_clicked();
//Tab#3 Arduino Functions
void on_arduinoLEDButton_clicked();
}
This is the Arduino Communication header:
#pragma once
#include <iostream>
#include "SerialPort.h"
#include "ui_laserbasictestfunctions.h"
#include "laserbasictestfunctions.h"
#include <stdio.h>
#include <string.h>
#include <string>
class ArduinoCommunication {
private:
Ui::LaserBasicTestFunctionsClass& ui_;
int pin_piston1_;
int pin_suction_cup_;
int pin_led_;
char incomingData_[MAX_DATA_LENGTH];
SerialPort* arduino_;
char* portname_;
void connect_to();
void receiveData();
public:
ArduinoCommunication(char portname[], int pin_piston1, int pin_suction_cup, int pin_led, Ui::LaserBasicTestFunctionsClass& ui);
ArduinoCommunication();
void set_pin(int pin, bool high);
void actuate_pistons(bool actuate);
void actuate_suction_cup(bool actuate);
void actuate_led(bool actuate);
};
I get the following error messages:
1. 'ArduinoCommunication' no appropriate default constructor available
2. 'tableControl': unknown override specifier
3. missing type specifier - int assumed
I tried creating the ArduinoCommunication object in the constructor of QMainWindow, but then I was not able to access it via the private slot functions.
Any help is welcome!
Related
I am having trouble with a class inheriting from QObject. My problem is that I want my class queueTcp to inherit from QQueue so I can add objects of another class commandTcp to it. Everything works fine until I let commandTcp inherit QObject.
When I try to compile, I get 11 errors like:
use of deleted function 'QObject::QObject(const QObject&)
QObject::QObject(const QObject&)' is private
I tried to figure them out for the last days, but couldnĀ“t find any solution that works for me.
My code is:
queueTcp.h
#ifndef QUEUETCP_H
#define QUEUETCP_H
#include <QObject>
#include <commandtcp.h>
#include <QQueue>
class queueTcp: public QObject, public QQueue<commandTcp>
{
Q_OBJECT
public:
queueTcp();
void newCommand();
};
#endif // QUEUETCP_H
commandTcp.h
#ifndef COMMANDTCP_H
#define COMMANDTCP_H
#include <QObject>
#include <QByteArray>
class commandTcp: public QObject
{
Q_OBJECT
private:
QByteArray storeCommand;
int arraySize;
int destinationAddress;
int length;
int command;
public:
commandTcp(int, int, int, QByteArray, int);
};
#endif // COMMANDTCP_H
queueTcp.cpp
#include "queuetcp.h"
queueTcp::queueTcp() {
}
void queueTcp::newCommand() {
commandTcp temp(63, 8, 208, 0, 1);
this->enqueue(temp);
}
commandTcp.cpp
#include "commandtcp.h"
commandTcp::commandTcp(int _destinationAddress, int _length, int _command, QByteArray _userData, int _arraySize)
{
arraySize = _arraySize;
storeCommand = {0};
storeCommand.append(_userData);
destinationAddress = _destinationAddress;
length = _length;
command = _command;
}
Where is my problem?
Thanks in advance.
I've encountered an error which I cannot seem to find a solution to anywhere else.
The error occurs when I'm trying to declare an instance of the "EncodeWindow" class.The compiler is giving errors C2143,C4430 and C2238. I am simply trying to give the "MainWindow" class an instance of "EncodeWindow".
File mainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QLabel>
#include "Image.h"
#include "encodewindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
/*Check whether a given file path is valid*/
bool CheckFilePath(QString);
/*Sets UI widgets*/
void setOriginalImage_Label(const char*);
void setEncodedImage_Label(const char*);
void setDebug_TextBox(QString);
/*Saves all information about current encoding/decoding*/
void saveFile();
private slots:
void on_Encode_Button_clicked();
void on_Reset_Button_clicked();
void on_Save_Button_clicked();
void on_AddEncodeImage_Debug_Button_clicked();
void on_AddImage_Button_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
EncodeWindow *CurrentEncodeWindow = new EncodeWindow; //ERROR HERE!! C2143
int fileNumber = 0;
};
#endif // MAINWINDOW_H
File encodeWindow.h:
#ifndef ENCODEWINDOW_H
#define ENCODEWINDOW_H
#include <QMainWindow>
#include "mainwindow.h"
namespace Ui {
class EncodeWindow;
}
class EncodeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit EncodeWindow(QWidget *parent = 0);
~EncodeWindow();
/*Get filepaths from 'result' object (For access to filepaths without 'encode' Window)*/
const char* getOriginalFilePath();
const char* getEncodeFilePath();
const char* getSaveFilePath();
bool checkUI(const char*,const char*,const char*,bool*);
bool CheckFilePath(const char*);
std::string readInText(const char*);
private slots:
void on_RedChannel_CheckBox_clicked(bool);
void on_GreenChannel_CheckBox_clicked(bool);
void on_BlueChannel_CheckBox_clicked(bool);
void on_AlphaChannel_CheckBox_clicked(bool);
void on_BitDepth_Slider_sliderMoved(int);
void on_BitDepth_SpinBox_valueChanged(int);
void on_Encode_Button_clicked();
private:
Ui::EncodeWindow *ui;
Encoded result; //Enocoded object (child of Image class)
};
#endif // ENCODEWINDOW_H
Any help would be great. The code was done in Qt for an image steganography project.
Thanks.
You have circular includes. "mainwindow.h" includes "encodewindow.h" and "encodewindow.h" includes "mainwindow.h". The include guards stop the cycle, but the class definitions for whichever header is included first won't be complete in the second include file, which will result in your errors.
In the snippet above, there's nothing that I see in encodewindow.h that uses anything in mainwindow.h, so just remove the include of mainwindow.h from encodewindow.h.
I have 3 classes - GLWidget, Window and rules. Window creates an object of GLWidget called 'm_glWidget' which I am trying to access from the class rules. I have tried using 'extern' and changing things up but nothing seems to make it work. The closest I have got is a single error which says "storage class specified for 'm_glWidget'" but I am unsure on what this means. This is my code:
Window.h
#include "glwidget.h"
#include "rules.h"
class GLWidget; class rules;
class Window : public QDialog, public Ui::frmMain
{
Q_OBJECT;
public:
Window(QWidget *parent = 0);
rules *gameRules;
GLWidget *m_glWidget;
Window.cpp
#include "rules.h"
#include "glwidget.h"
class rules; class GLWidget;
using namespace std;
Window::Window(QWidget *parent):QDialog(parent)
{
m_glWidget = new GLWidget;
gameRules = new rules;
rules.h
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules();
extern GLWidget *m_glWidget;
rules.cpp
#include "window.h"
#include "glwidget.h"
using namespace std;
rules::rules(){
}
void rules:: print(){
cout << m_glWidget->x << endl;
}
How about this?
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules(GLWidget *glWidget)
: m_glWidget(glWidget)
{
}
GLWidget *m_glWidget;
This is problem #2 from this previous question:
Inheritance in Arduino Code
Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.
This is my "Board" class I have so far, that contains multiple child elements:
Board.h:
#ifndef Board_h
#define Board_h
#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>
#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"
class Board
{
public:
Board();
void draw(double* matrix);
private:
Marble marble;
//std::vector<Actor> children;
Actor* children[2];
};
#endif
Board.cpp:
#include "Arduino.h"
#include "Board.h"
#include <math.h>
#include <iterator>
#include <vector>
Board::Board()
{
}
void Board::create(double* _matrix, int _cols, int _rows) {
Marble *marble = new Marble();
Wall wall;
children[0] = marble;
//children.push_back(marble);
//children.push_back(wall);
}
void Board::draw(double* matrix) {
Serial.println("board draw");
children[0]->speak();
}
In my "loop" function I am calling
board.draw(matrix);
which results in some nutty Serial code being written out.
Clearly I am not understanding the ins and outs of pointers in arrays in classes here.
You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.
class Actor
{
public:
Actor();
virtual void speak(); // virtual
private:
};
what am I doing wrong?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "fileoperations.h"
namespace Ui {
class MainWindow;
}
class FileOperations;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
FileOperations FileController;
private slots:
void on_OpenButton_clicked();
void on_SaveButton_clicked();
void on_EncodeButton_clicked();
void on_DecodeButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When i try to compile and run the program, it says:
g:\ke\c++ projects\projects\qt\shitlencoder\mainwindow.h:18: error: C2079: 'MainWindow::FileController' uses undefined class 'FileOperations'
Here's the strange thing, if I change 'FileOperations FileController;' to 'FileOperations *FileController;'(Obviously this compiles wrongly, because the rest of my codes that you can't see havn't been adapted to '->' instead of '.')
Then if I change it back to 'FileOperations FileController;' it lets me compile the program once (And it works fine), then it has the error the next time I try to compile it.
I'm using Qt 5.0.
fileoperations.h:
#ifndef FILEOPERATIONS_H
#define FILEOPERATIONS_H
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QFileDialog>
#include <string>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <fstream>
using namespace std;
class FileOperations
{
public:
FileOperations();
void SetInputFile(QString x);
void SetOutputFile(QString x);
void EncryptAndSave(Ui::MainWindow *NUI);
void DecryptAndSave(Ui::MainWindow *NUI);
void createid(int id, int id2);
int GetCFuncion();
void SetCFuncion(int x);
long long Get_Size(string filename);
bool Get_Toobig(string path);
//DWORD WINAPI Thread_no_1();
private:
string InputFilename;
string OutputFilename;
int CFuncion;//CurrentFunction;
vector<int> conbyte1;
vector<int> conbyte2;
vector<int> opbyte1;
vector<int> opbyte2;
vector<int> passwordbytes;
};
#endif // FILEOPERATIONS_H
I assume that, in your .cpp file, you are using
#include "fileoperations.h"
Then, in fileoperations.h, you are including mainwindow.h which again includes fileoperations.h which is basically correct, since you are using a FileOperations object as parameter. But, due to the guards, class FileOperations is not seen by the compiler this time, hence FileOperations is unknown when used as parameter in your method. You need to break this dependency:
In fileoperations.h, use a forward declaration for Ui::MainWindow and remove the #include "mainwindow.h":
namespace Ui {
class MainWindow;
}
...
Since you are holding a FileOperations object in your class, you need the full class declaration. This means you have to include the header, you cannot simply forward declare the class like you are doing now. If you hold only a pointer, and do not have any code in your header that attempts to dereference the pointer, then the forward declaration is enough.
EDIT You have a cyclical include. You are including mainwindow.h in fileoperations.h. You can fix if by removing that include completely.
You have circular include issue, mainwindow.h and fileoperations.h include each other, try to remove below line from fileoperations.h
#include "mainwindow.h"