错误:“xxx”没有命名类型

error: ‘xxx’ does not name a type

本文关键字:类型 xxx 错误      更新时间:2023-10-16

我有一个奇怪的问题,我不明白。我正在将一些C代码复制到c++类中,无法通过此错误"未命名类型"…我希望我复制了足够多的代码来让它有意义,原始程序是~1000行

错误是…错误:' HTTPContext '没有指定类型

错误行是"HTTPContext MainWindow::*find_rtp_session_with_url(const char *url, const char *session_id)"

className.h:

class MainWindow : public QMainWindow
{
    Q_OBJECT
 public:
     typedef struct HTTPContext{
         int fd;
     }HTTPContext;
     HTTPContext *find_rtp_session_with_url(const char *url,
                                              const char *session_id);
};

className.cpp

#include "className.h"
HTTPContext MainWindow::*find_rtp_session_with_url(const char *url,
                                          const char *session_id)
{
     HTTPContext *rtp_c;  
}

HTTPContext是在类作用域中声明的,因此要在全局作用域中的函数定义中使用,您需要显式指定它:

MainWindow::HTTPContext *MainWindow::find_rtp_session_with_url...

你需要说

MainWindow::HTTPContext* MainWindow::find_rtp_session_with_url( ...

,因为它是内部类。你也不必在这里使用typedef:

struct HTTPContext {
    int fd;
};

足以在c++中命名一个类型