0

I have a pointer in a header file named global.h in my project:

int *CorrectID;

and somewhere in my mainwindow.cpp file I have this:

*CorrectID = ui->lineEdit_CurrentID->text().toInt();

but I receive this Error:

invalid type argument of unary '*' (have 'uint {aka unsigned int}')
    *CorrectID = ui->lineEdit_CurrentID->text().toInt();
     ^
  • 1
    Is this the actual code? It seems like you have this problem: https://stackoverflow.com/questions/42875580/invalid-type-argument-of-unary-have-int-error-in-c – Fantastic Mr Fox Jan 02 '19 at 09:43
  • 1
    There's no special way to set a value through a pointer, just because the pointer is declared in a header file. The code you posted looks correct, the problem is probably somewhere else (e.g. you are shadowing the declaration in the header file). As usual it would best to post a complete program, instead of just the part of the program that you think is wrong. – john Jan 02 '19 at 09:53
  • why you need to set an address to a pointer from a return value of a function... – Karthik Krish Jan 02 '19 at 10:33
  • Caution: Every .cpp file that you `#include "global.h"` in will try to redefine `ID`, most likely preventing proper linkage. Refer to [When to use extern in C++](https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c) – Kiruse Jan 02 '19 at 11:54

2 Answers2

1

You seem to have problems understanding how a pointer works.

*CorrectID = ui->lineEdit_CurrentID->text().toInt();

That code dereferences your pointer and assigns the value on the right side to the address it points at. Do to so, it already needs to point at some address. Right now, given the code you posted in your "answer", it would not have.

You need to assign an address to your pointer, otherwise, it might contain random nonsense that easily leads to a crash. You can do this like that:

CorrectId = new int;

Which creates a new variable on the heap and uses that address, although that is most likely not a valid ID.

Or given your "answer" code, you could create a stack variable in the scope of your main and set the pointer towards it, although that wouldn't be a good design.

That said, probably more important: Try to avoid global variables. I don't know your design, therefore I can't tell you how you should do it, but you having that pointer being a global variable is a red flag to me. Having an ID that can be manipulating from all directions is also, even if you'd use injection instead. Most likely, you should create a class which stores CorrectID as a member and thus takes ownership of it. A reference to this class is shared by injection and any manipulation of the member is done over the interface of the class. Now again, I don't know your big design, but in most cases, this will be the way to go.

Given your other code, it could be simply done by making it a member of your main window. Or you have some other class that controls things to which the main window has a pointer to. Best would be if you gave us context.

A possible layout of your project:

class IDController
{
    private:
    int _ID;

    public:
    void setID(int ID) { _ID = ID; }
    const int& ID() const { return _ID; }
};

int main(int argc, char *argv[])
{
IDController controller;
QApplication a(argc, argv);
MainWindow w(&a, &IDController);
...
}

class MainWindow : public QMainWindow
{
...
private:
IDController* controller;
...

MainWindow::MainWindow(QWidget* parent, IDController* controller) :
QMainWindow(parent),
controller(controller)
ui(new Ui::MainWindow)
{
...

void MainWindow::on_pushButton_clicked()
{
    controller->setID(6);
}

The name IDController should be replaced by some more organical name, some class that the ID belongs to. Note that I do not initialize _ID, which is bad. IDController should have some defined constructor. If the ID is something very volatile, one might decide to remove the getter and setter from the controller and make it public, but most likely, instead you want to extent the setter and getter to do some additional things (like verifying that a given ID is valid).

Aziuth
  • 3,652
  • 3
  • 18
  • 36
0

I built a new and very simple project with only one line of user code:

global.h file:

#ifndef GLOBAL_H
#define GLOBAL_H
int *ID;
#endif // GLOBAL_H

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "global.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
*ID = 6;
}

on this program I get no error but when I click on pushbutton, the program crashes!!

  • Hi, you should be modifying your question if there are changes, or open a new one if the question is too different. You should only add answers under the question. – ShadowMitia Jan 02 '19 at 11:03
  • @ShadowMitia the question title is suitable for both questions – reza moslemi Jan 02 '19 at 11:07
  • @rezamoslemi That is not the issue. You posted an "answer" to your question which is not an answer but an update. Please update your original question accordingly, instead of posting such an invalid answer. – Kiruse Jan 02 '19 at 11:47