2015-12-21 83 views
0

我得到這個令我瘋狂的錯誤。我剛剛添加了一個使用Qt Creator的類,但它不會構建。C2059:語法錯誤:Qt中的'public'

#ifndef LIBRARY_H 
#define LIBRARY_H 

class Library 
{ 
public: 
    Library(); 

signals: 

public slots: 
}; 

#endif // LIBRARY_H 

的錯誤是:

library.h:12: error: C2059: syntax error : 'public' 
library.h:12: error: C2334: unexpected token(s) preceding ':'; skipping apparent function body 
library.h:13: error: C2760: syntax error : expected '{' not '}' 
library.h:13: error: C2143: syntax error : missing '}' before ';' 
library.cpp:4: error: C2535: 'Library::Library(void)' : member function already defined or declared 
library.cpp:8: error: C1004: unexpected end-of-file found 

回答

3

問題是Qt Creator中添加signal,即使我添加了一個C++類(而不是Qt的類),並沒有從QObject派生它slots

的解決方案是從類中刪除signalslots詞語或從QObject#include <QObject>

+3

Q_OBJECT宏導出它也需要在類聲明和在多重繼承的情況下 - 的QObject應該在該列表 – Shf

+0

第一@Shf謝謝Q_OBJECT宏確實需要'QObject'派生類正常工作(信號和插槽),但編譯器錯誤確實會消失,即使沒有它。這說明肯定包括它來避免一組不同的意外錯誤。爲了完整起見,我想我會評論一下。 – zar

相關問題