Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4585470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:29:59+00:00 2026-05-21T21:29:59+00:00

I’m an PyQt programmer and trying to learn C++ Qt. I’m following this link

  • 0

I’m an PyQt programmer and trying to learn C++ Qt.

I’m following this link for learning how to import my ui files. But when I’m trying to compile, I’m getting that error:

[utdmr@utdmr-arch cpp]$ qmake -project && qmake && make
/usr/bin/uic ui.ui -o ui_ui.h
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o main.o main.cpp
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. gui.h -o moc_gui.cpp
g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o moc_gui.o moc_gui.cpp
g++ -m64 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-O1 -o cpp main.o moc_gui.o    -L/usr/lib -lQtGui -lQtCore -lpthread 
moc_gui.o: In function `Gui::Gui(QWidget*)':
moc_gui.cpp:(.text+0x80): multiple definition of `Gui::Gui(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
moc_gui.o: In function `Gui::Gui(QWidget*)':
moc_gui.cpp:(.text+0x80): multiple definition of `Gui::Gui(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [cpp] Error 1

But I can’t see that multiple definition on my code. I’m also new to C++ classes’ inheritance.

Here is my files:

gui.h:

#include "ui_ui.h"

class Gui : public QWidget
{
    Q_OBJECT

    public:
        Gui(QWidget *parent=0);
    private:
        Ui::Form ui;
};


Gui::Gui(QWidget *parent)
        : QWidget(parent)
{
          ui.setupUi(this);
}

main.cpp:

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

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Gui gui;
    gui.show();

    return app.exec();
}

ui_ui.h: http://paste.kde.org/56251/ (I used pastebin because it’s long)

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:30:00+00:00Added an answer on May 21, 2026 at 9:30 pm

    You should put the function definition into a .cpp file.
    So create gui.cpp with the following contents:

    #include "gui.h"
    
    Gui::Gui(QWidget *parent)
            : QWidget(parent)
    {
              ui.setupUi(this);
    }
    

    removing it from the .h file.

    The reason you were getting the error is that the header file was being included in more than one .cpp file (your main.cpp and moc_gui.cpp, a generated file) and since the function definition was in the header file it was multiply defined.

    Oh, don’t forget to add the new .cpp file to your project. If you forget you will get link errors about undefined functions!

    @cbamber85 made some good comments on your original question. Although in this case your problems are not being caused by the lack of multiple inclusion guards as your projects get bigger you may run into issues so getting into the habit now will be useful. To protect your file gui.h against multiple inclusion you do this:

    #ifndef GUI_H_
    #define GUI_H_
    
    #include "ui_ui.h"
    
    class Gui : public QWidget
    {
        Q_OBJECT
    
        public:
            Gui(QWidget *parent=0);
        private:
            Ui::Form ui;
    };
    
    #endif
    

    The actual macro name GUI_H_ is not significant you could use SnOOpy but you need to have a different one for each header file so using some variation of the file name is common practice. It is also conventional to only use uppercase letters in #defined macros (just a convention; the compiler does not care).

    In a lot of modern compilers your can simple use:

    #pragma once
    
    #include "ui_ui.h"
    
    class Gui : public QWidget
    {
        Q_OBJECT
    
        public:
            Gui(QWidget *parent=0);
        private:
            Ui::Form ui;
    };
    

    OK, so what’s this multiple inclusion and when does it occur? Well suppose you a file called types.h that defined some basic types you used frequently. Now suppose you have two classes Foo and Bar each of which uses types from types.h. Here’s what this might look like:

    File: types.h

    typedef int Int32;
    

    File: foo.h

    #include "types.h"
    class Foo
    { 
        Int32 mCount;
    };
    

    File: bar.h

    #include "types.h"
    class Bar
    {
        Int32 mLength;
    };
    

    Notice I have left out the multiple inclusion guards. Now suppose I have a main that uses both Foo and Bar:

    #include "foo.h"
    #include "bar.h"
    
    int main(int argc, const char* argv[])
    {
        Foo f;
        Bar b;
        return 0;
    }
    

    In the absence of multiple inclusion guards this will complain that Int32 is multiply defined… What’s the problem? Well since main.cpp includes foo.h and foo.h includes types.h it gets included once there and then when bar.h gets included it also includes types.h so it gets included a second time there.

    I hope this all makes sense. I purposely left it out of my original answer, but in the light of the comments it is probably better to explain it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.