1

So, I want to create a "recent files" section in the "File menu" of my spreadsheet application. While building the application, the function that is supposed to update the recentFileActions QStringList generates the following error/home/axel/QtSDK/Code/QMainWindow/mainwindow.cpp:-1: error: undefined reference to 'MainWindow::recentFiles'

So from the error I get that recentFiles isn't defined? Because I have this in the private section of my header: QStringList static recentFiles;

This is the whole updateRecentFileActions() function:

void MainWindow::updateRecentFileActions(){ QMutableStringListIterator i(recentFiles); while (i.hasNext()) { if (!QFile::exists(i.next())) i.remove(); } for (int j = 0; j < MaxRecentFiles; ++j) { if (j < recentFiles.count()) { QString text = tr("&%1 %2") .arg(j + 1) .arg(strippedName(recentFiles[j])); recentFileActions[j]->setText(text); recentFileActions[j]->setData(recentFiles[j]); recentFileActions[j]->setVisible(true); } else { recentFileActions[j]->setVisible(false); } } separatorAction->setVisible(!recentFiles.isEmpty()); } 

I'll add any missing information.

Thank you.

1 Answer 1

5
QStringList static recentFiles; 

That is only declaration. You need to define the static variable in the source file :

QStringList MainWindow::recentFiles; 

If you do not understand why you need to do it, take a look into See this faq item.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean that I need to paste QStringList MainWindow::recentFiles; somewhere in the .cpp? or that I need to change QStringList static recentFiles; for QStringList MainWindow::recentFiles;? Because if I try to add QStringList MainWindow::recentFiles; to either the constructor in my .cpp or in the function that is creating the issue I get a invalid use of qualified name error :(.... Sorry I'm new to c++
@Axel If you declared that member variable static in the header, then you need to define in the source (.cpp) file. If you do not declare is static, then you do not need to do it. See faq 10.12
Ideally I would want to declare recentFiles as static, which would mean that I need to define it in the .cpp. In order to define recentFiles, I guess that i need to copy QStringList MainWindow::recentFiles; somewhere in the .cpp?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.