我尝试访问类结构时出错

时间:2017-09-01 08:54:35

标签: c++

我正在尝试访问类结构,但它给了我以下错误:

InternalGpsReceiver.h:

class InternalGpsReceiver {
    public:
        static struct gps_data_t gpsdata;
        InternalGpsReceiver(void);
};

InternalGpsReceiver.cpp:

InternalGpsReceiver::InternalGpsReceiver(void){
    int err = gps_open("localhost", DEFAULT_GPSD_PORT, this->&gpsdata);
}

编译错误:

error: expected unqualified-id before '&' token 
    int err = gps_open("localhost", DEFAULT_GPSD_PORT, this->&gpsdata);

1 个答案:

答案 0 :(得分:0)

您需要在使用之前初始化您的结构。请查看http://en.cppreference.com/w/cpp/language/static以获取完整说明。

下面是编译的最小代码。我刚为gps_data_t,gpsopen和DEFAULT_GPSD_PORT创建了一个模型。

<强> InternalGpsReceiver.h:

#define DEFAULT_GPSD_PORT 8080

struct gps_data_t {
    int a;
    int b;
};

int gps_open(const char *xx, int x, struct gps_data_t *gps){ return 1;}

class InternalGpsReceiver {
public:
    InternalGpsReceiver(void);
    static gps_data_t gpsdata;
}

<强> InternalGpsReceiver.cpp:

#include "InternalGpsReceiver.h"

// The line that you missed.
gps_data_t InternalGpsReceiver::gpsdata;

InternalGpsReceiver::InternalGpsReceiver(void)
{
    int err = gps_open("localhost", DEFAULT_GPSD_PORT, &InternalGpsReceiver::gpsdata);
};


int main()
{
    InternalGpsReceiver x = InternalGpsReceiver();
    return 1;
}