C2059:语法错误:Qt 中的'public'

C2059: syntax error : 'public' in Qt

本文关键字:public Qt 语法 错误 C2059 中的      更新时间:2023-10-16

我遇到了这个错误,这让我抓狂。我刚刚用QtCreator添加了一个类,但它不会构建。

#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

问题是Qt Creator添加了signalslots,尽管我添加了C++类(而不是Qt类),并且没有从QObject派生它。

解决方案是从类中删除signalslots单词,或从QObject#include <QObject> 派生