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 5945525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:41:39+00:00 2026-05-22T16:41:39+00:00

queue1.h mainwindow.h / / A.h—Mainwindow.cpp A #includes queue1 Mainwidow #includes A A and MainWindow

  • 0
        queue1.h        mainwindow.h
        /               /
        A.h---Mainwindow.cpp

A #includes queue1
Mainwidow #includes A
A and MainWindow both need to use one instanse of queue1, so i define it in A like “static queue1 q1;”
Then in A.h i push “A” in a q1, but when i try to output it in mainwindow i get trash.
When i try to push “mainwindow” inside mainwindow.h and then output it is ok.
I’m using Qt 4.7.0 and win 7.

queue1.h

#ifndef QUEUE1_H
#define QUEUE1_H
#include <queue>
#include <string>

class queue1
{
public:
    queue1();
   std::queue<std::string> q;
};

#endif // QUEUE1_H

A.h

#ifndef A_H
#define A_H
#include "queue1.h"
static queue1 q1;
class A
{
public:
    A(){q1.q.push("A");}
};

#endif // A_H

If you uncomment “q1.q.push(“mainwindow”);” the program will first output “mainwondow” and then trash. After that queue become empty.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "a.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //q1.q.push("mainwindow");
}

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

void MainWindow::on_pushButton_clicked()
{
    ui->label->setText(q1.q.front().c_str());
    q1.q.pop();


}

For this test project i didn’t changed mainwindow.h but here it is.

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:
    Ui::MainWindow *ui;

private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H
  • 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-22T16:41:39+00:00Added an answer on May 22, 2026 at 4:41 pm

    static does not mean what you mean it does in this context — it takes away external linkage from a symbol, meaning it will only be visible within a given translation unit. And because you have it in the header, included by two units, there will be two distinct instances of your class — so when you push in A, you push to the queue in A, not the queue in mainwindow.

    The solution is to declare the variable as extern, and define it in only one translation unit.

    // in queue1.h
    class queue1 { ... };
    extern queue1 q1;
    
    // in e.g. queue1.cpp
    #include "queue1.h"
    queue1 q1;
    

    Now both A and mainwindow will use the same queue (of course you need to remove static queue1 q1;
    from A.h).

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

Sidebar

Related Questions

Can queue elements be accessed like an array? If not, then what containers similar
In my build configuration I have multiple project queues: Queue1, Queue2, ... Queue(N) I
I need to queue events and tasks for external systems in a reliable/transactional way.
I have a Queue object that I need to ensure is thread-safe. Would it
I have a list/queue of 200 commands that I need to run in a
Should FIFO queue be synchronized if there is only one reader and one writer?
We have a system that uses ActiveMQ (Queues) - and have exactly one producer
import com.google.appengine.api.labs.taskqueue.Queue; import com.google.appengine.api.labs.taskqueue.QueueFactory; import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*; // ... Queue queue = QueueFactory.getDefaultQueue(); queue.add(url(/worker).param(key,
I need to create a thread in Delphi with the following characteristics: Waits until
I'd like to know how multiprocessing is done right. Assuming I have a list

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.