• 5769阅读
  • 6回复

[提问]请教一个问题 [复制链接]

上一主题 下一主题
离线caffee_1989
 
只看楼主 倒序阅读 楼主  发表于: 2011-05-14
出现错误:expected class-name before '{' token
一般是什么问题导致,网上很多说是大小写导致,但我改了几次都不行,大家都来发表下意见拉。
[ 此帖被caffee_1989在2011-05-14 21:08重新编辑 ]
离线XChinux

只看该作者 1楼 发表于: 2011-05-14
把代码帖出来下。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线caffee_1989
只看该作者 2楼 发表于: 2011-05-15
回 1楼(XChinux) 的帖子
其实这个程序是参考网上修改的,是用于串口通信,用一个线程实现.
一共有5个文件:thread.h  thread.cpp  mainwindow.h  mainwindow.cpp  main.cpp

thread.h :

#ifndef THREAD_H
#define THREAD_H

#include <qthread.h>

class Thread : public QThread
{
  Q_OBJECT  
public:
  Thread();  
  char buf[128];
  volatile bool stopped;
  volatile bool write_rs;
  volatile bool read_rs;

protected:
  virtual void run();
  
signals:
    void finished();
};

#endif




thread.cpp :

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>    
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <qthread.h>
#include "thread.h"

#define BAUDRATE B9600  
#define RS_DEVICE "/dev/ttySAC1"      

Thread::Thread()
{}                                              

void Thread::run()        
{
int fd,res;
struct termios oldtio,newtio;  

printf("start...\n");

fd=open(RS_DEVICE,O_RDWR|O_NOCTTY);    
if(fd<0)
{
perror("error");
exit(1);                          
}

printf("Open...\n");

tcgetattr(fd,&oldtio);          
bzero(&newtio,sizeof(newtio));    
newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;  
newtio.c_iflag|=IGNPAR;                
newtio.c_lflag=0;  

newtio.c_cc[VMIN]=0;      
newtio.c_cc[VTIME]=100;                  
tcflush(fd,TCIFLUSH);                    
tcsetattr(fd,TCSANOW,&newtio);          

while(stopped)                        
{
if(write_rs)                      
{
write_rs=0;
write(fd,"QtEmbedded-4.5.1",16);
}

if(read_rs)                          
{
read_rs=0;
res=read(fd,buf,10);
buf[res]=0;
emit finished();                  
}

}
printf("Close...\n");
tcsetattr(fd,TCSANOW,&oldtio);    
::close(fd);
}



mainwindow.h :

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <qwidget.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include "thread.h"        

class MainWindow:public QWidget
{
  Q_OBJECT
    
public:
  MainWindow(QWidget *parent=0);
  
public slots:
  void writeThread();
  void readThread();
  void closeThread();
  void display();

private:
     QPushButton *writeButton;
     QPushButton *readButton;
     QPushButton *closeButton;
     QLabel *dis_label;
     Thread *yy;
};
#endif



mainwindow.cpp :

#include"mainwindow.h"
#include <qpushbutton.h>
#include <qlabel.h>

MainWindow::MainWindow(QWidget *parent)
  :QWidget(parent)
{
  writeButton = new QPushButton("write",this);
  readButton = new QPushButton("read",this);
  closeButton = new QPushButton("close",this);
  dis_label = new QLabel ("none",this);
  
   dis_label->setGeometry(30, 30, 150, 30);
  writeButton->setGeometry(30, 120, 80, 30);
   readButton->setGeometry(120, 120, 80, 30);
   closeButton->setGeometry(210, 120, 80, 30);
  
  yy = new Thread;
  yy->start();        
  yy->stopped=1;      
  yy->write_rs=0;
  yy->read_rs=0;
    
  connect(writeButton,SIGNAL(clicked()),this,SLOT(writeThread()));    
  connect(readButton,SIGNAL(clicked()),this,SLOT(readThread()));
  connect(closeButton,SIGNAL(clicked()),this,SLOT(closeThread()));
  connect(yy,SIGNAL(finished()),this,SLOT(display()));    
  
     showMaximized () ;
}

void MainWindow::display()
{
dis_label->setText(yy->buf);    
}

void MainWindow::writeThread()  
  yy->write_rs=1;
}

void MainWindow::readThread()
{
  yy->read_rs=1;
}

void MainWindow::closeThread()
{
yy->stopped=0;
}



main.cpp :

#include<qapplication.h>
#include"mainwindow.h"

int main(int argc,char *argv[])
{
  QApplication app(argc,argv);
  MainWindow mw;
  mw.show();
  return app.exec();
}





离线得道之兔

只看该作者 3楼 发表于: 2011-05-15
错误提示信息说是第几行出现错误?
岂能尽如人意,但求无愧我心。
离线caffee_1989
只看该作者 4楼 发表于: 2011-05-15
回 3楼(得道之兔) 的帖子
是thread.h的第8行:
class Thread : public QThread  这一行
离线得道之兔

只看该作者 5楼 发表于: 2011-05-17
不好意思,我也看不出来。
如果确认不是拼写错误的话,可以考虑是否配置的问题。
岂能尽如人意,但求无愧我心。
离线caffee_1989
只看该作者 6楼 发表于: 2011-05-20
回 5楼(得道之兔) 的帖子
谢谢拉,我怀疑是配置问题。
快速回复
限100 字节
 
上一个 下一个