• 8521阅读
  • 3回复

c++静态变量问题,本书57页 [复制链接]

上一主题 下一主题
离线pbcnet
 
只看楼主 倒序阅读 楼主  发表于: 2009-07-24
《C++ GUI Qt 4编程》(第2版)57页“通过把recentFiles变量声明为静态变量,可以相当容易的解决问题”

我在mainwindow.h中把 QStringList recentFiles;修改为
static QStringList recentFiles;
保存后编译出错,
Description    Resource    Path    Location    Type
make: *** [debug] Error 2    spreadsheet        line 0    C/C++ Problem
more undefined references to `MainWindow::recentFiles' follow    spreadsheet        line 495, external location: ./debug/mainwindow.o:D:\qt-book\chap04\spreadsheet\mainwindow.cpp    C/C++ Problem
undefined reference to `MainWindow::recentFiles'    mainwindow.cpp    /spreadsheet    line 400    C/C++ Problem
undefined reference to `MainWindow::recentFiles'    mainwindow.cpp    /spreadsheet    line 419    C/C++ Problem
undefined reference to `MainWindow::recentFiles'    mainwindow.cpp    /spreadsheet    line 473    C/C++ Problem
undefined reference to `MainWindow::recentFiles'    mainwindow.cpp    /spreadsheet    line 474    C/C++ Problem
undefined reference to `MainWindow::recentFiles'    mainwindow.cpp    /spreadsheet    line 488    C/C++ Problem
make[1]: *** [debug\spreadsheet.exe] Error 1    spreadsheet        line 0    C/C++ Problem
把所有使用recentFiles地方修改为 MainWindow::recentFiles后保存编译仍然出错:
错误同上。
有什么解决办法?在c++中使用静态成员变量初始化好像不是那么容易。
离线pbcnet
只看该作者 1楼 发表于: 2009-07-25
真冷清啊,自己的问题自己解决吧
声明称静态变量后
需要自己初始化,在mainwindow.cpp中加入 QStringList MainWindow::recentFiles;

QStringList MainWindow::recentFiles;
MainWindow::MainWindow()
{
   。。。。。
}
离线rqzrqh

只看该作者 2楼 发表于: 2009-07-26
应该是只声明了而没有定义,因此没有分配空间
这个版块从没见过
离线zyq840112

只看该作者 3楼 发表于: 2009-09-02
学习了!
快速回复
限100 字节
 
上一个 下一个