c++ - C2059 : syntax error : 'public' in Qt

标签 c++ qt

我遇到了这个让我抓狂的错误。我刚刚使用 Qt Creator 添加了一个类,但它无法构建。

#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>

关于c++ - C2059 : syntax error : 'public' in Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34405388/

相关文章:

c++ - 如何在 Qt 模型中插入可能不会发生的行?

c++ - 如何在 C++ 程序中间注销昂贵的文件 I/O

c++ - 可以在 .h 文件中声明静态全局变量吗?

c++ - 清除 vector 时,STL vector 内部的 STL vector 是否超出范围?

c++ - 如何在目录遍历中获取文件大小?

qt - 查找 QWidget 所在的屏幕

c++ - 客户端套接字发送数据但服务器套接字不接收它们。 C++缓冲流?

c++ - std::shared_ptr 线程安全

c++ - Qt C++ QLabel 可点击鼠标事件不起作用

c++ - 如何从Qt中的项目中排除cpp文件