标题:在编译中提示变量未定义
作者:ghoulr
日期:2006-04-08 04:23
内容:
我是刚刚才开始学习Qt(也是刚接触linux),按照教程中的helloworld作的第一个程序就不能编译...
出错提示是这样的:
main.cpp: In function `int main(int, char**)':
main.cpp:10: `hellobtn' undeclared (first use this function)
main.cpp:10: (Each undeclared identifier is reported only once for each
function it appears in.)
main.cpp:11: `show' undeclared (first use this function)
main.cpp:12: `exec' undeclared (first use this function)
make: *** [.obj/main.o] Error 1
这是我的源程序:
#include
#include
int main(int argc,char **argv)
{
QApplication a(argc,argv);
QPushButton Hellobtn("Hello World",0);
Hellobtn.resize(100,20);
a.setMainWidget(*hellobtn);
Hellobtn,show();
return a,exec();
}
请问问题是出在哪里,谢谢
#1 [XChinux 04-08 08:32]
a.setMainWidget(*hellobtn);
应该改为a.setMainWidget(&Hellobtn);
还有下面的逗号改为分号
#2 [ghoulr 04-08 13:22]
。。。是我自己编程的时候不小心,再加上虚拟机显示比较模糊,我把程序改成这样以后:
#include
#include
int main(int argc,char **argv)
{
QApplication a(argc,argv);
QPushButton Hellobtn("Hello World",0);
Hellobtn.resize(100,20);
a.setMainWidget(&Hellobtn);
Hellobtn.show();
return a.exec();
}
出现的提示改为了:
main.cpp: In function `int main(int, char**)':
main.cpp:10: no matching function for call to `QApplication::setMainWidget(
QPushButton*)'
make: *** [.obj/main.o] Error 1
但是把 a.setMainWidget(&Hellobtn);改成 a.setMainWidget(*Hellobtn);后还是不能编译,出来的提示变成了:
main.cpp: In function `int main(int, char**)':
main.cpp:10: no match for `* QPushButton&' operator
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qpoint.h:137: candidates are:
const QPoint operator*(const QPoint&, double)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qpoint.h:140:
const QPoint operator*(double, const QPoint&)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qpoint.h:305:
const QPointF operator*(double, const QPointF&)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qpoint.h:300:
const QPointF operator*(const QPointF&, double)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qsize.h:145: const
QSize operator*(const QSize&, double)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qsize.h:148: const
QSize operator*(double, const QSize&)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qsize.h:301: const
QSizeF operator*(const QSizeF&, double)
/usr/local/Trolltech/Qt-4.1.0/include/QtCore/qsize.h:304: const
QSizeF operator*(double, const QSizeF&)
make: *** [.obj/main.o] Error 1
麻烦再帮我看看,谢谢
#3 [XChinux 04-08 13:38]
你用的是Qt 4.1.0,在Qt4.x里,已经没有了setMainWidget()这个函数了。
你把它去掉 就行了。
#4 [ghoulr 04-08 13:56]
嗯,我把那句注释掉以后编译成功了,谢谢
顺便问一下,初学是不是不适合从4.x版本开始啊
#5 [bjtony 04-09 23:34]
这不是关键,我感觉QT 3比较适合你。但是学好C++是基础。
有条件不要在虚拟机上。