Why does the cannot initialize object parameter for "some widgets" with an expression type 'MainWindow' occur?

Asked 2 years ago, Updated 2 years ago, 146 views

We launched the mainwindow project in QtCreator 4.7Qt5.11.

The pro file, the h file, and the cpp file are automatically created, but there is an error that does not make sense.
mainwindow.cpp

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

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

appearance error

cannot initialize object parameter of type 'QWidget' with an
expression type 'MainWindow'

This is the code of the initial state that has not been reorganized at all since the project was launched.
If you have qt creator 4.7 installed, you can make it right away.I just found a question with a similar title, but Cannot initialize return object of type 'int' with an value of type 'cocos2d::Scene*' was from cocoa2d-x, not qt.

I'll put other codes on it just in case.
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

# include <QMainWindow>

    class MainWindow:public QMainWindow
    {
        Q_OBJECT

    public:
        MainWindow (QWidget* parent=nullptr);
        ~MainWindow();
    };

    #endif//MAINWINDOW_H

mainwindow.pro

#-------------------------------------------------------
    #
    # Project created by Qt Creator 2018-09-14T 16:52:20
    #
    #-------------------------------------------------

    QT+=core gui widgets

    TARGET=MainWindow
    TEMPLATE=app

    # The following define makes your compiler permit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings)
    # depend on your compiler).Please consult the documentation of the
    # Deprecated API in order to know how to port your code away from it.
    DEFINES+=QT_DEPRECATED_WARNINGS

    # You can make your code fail to compile if you use decremented APIs.
    # In order to do so, comment the following line.
    # You can select to disable decremented APIs only up to a certificate version of Qt.
    # DEFINES+=QT_DISABLE_DEPRECATED_BEFORE=0x060000#disables all the APIs detailed before Qt 6.0.0

    CONFIG+=c++11

    SOURCES+=\
            main.cpp\
            mainwindow.cpp

    HEADERS+=\
            mainwindow.h

# Default rules for deployment.
qnx —target.path=/tmp/$${TARGET}/bin
else:unix: !android:target.path=/opt/$${TARGET}/bin
!isEmpty(target.path)—INSTALLS+=target

addition:
This is what happens in the environment where there are no problems.
qt 5.9.6
qt-creator 4.7.0
Everything else is the same.

However, if the compiler is msvc 2017, and you are experiencing problems,
The min-GW32 is chosen, which says 32bit.Probably the compiler
I think it's a bug caused by insufficient set of , but it's beyond my wildest guess.
I don't think I made any unusual settings during installation, but this is what happened.

Windows 10 64-bit
qt-creator 4.7
c++11
qt 5.11

c++ windows-10 qt c++11 qt-creator

2022-09-29 21:25

1 Answers

QtCreator 4.7 automatically performs code parsing by default.
If you can actually compile the errors that this points out, you can ignore them.

# After the compilation is successful, I think the error will disappear if you run the code analysis again.
# If you create a project with a UI file, code analysis may occur before the source code that is automatically generated from the UI file is generated, and it may be considered an error.


2022-09-29 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.