声明对象前在全局函数中使用类的成员函数 [英] Using member functions of class in global function before declaring object

查看:30
本文介绍了声明对象前在全局函数中使用类的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件处理函数,在该函数中调用了一个类的成员函数.事件处理函数在类 cpp 文件中声明,但不是类的一部分,它不是成员函数.

当我编译代码时,编译器说该函数是作用域中的注释,因为它正在调用全局事件处理函数中的成员函数.

我的问题如下:有没有办法在全局函数中使用meber函数?(对象首先在运行时创建).

下面是成员函数和全局事件处理程序:

全局事件处理程序:void adbEventHandler(Connection * connection, adb_eventType 事件, uint16_t 长度, uint8_t * 数据){Serial.println("在数据接收处理程序中");Serial.println("收到数据:");Serial.println(数据[0]);Serial.println(数据[1]);字符 a = 数据 [0];字符 b = 数据 [1];Serial.println(a);Serial.println(b);//uint16_t data2 = 数据;//USBCommunicator 类的成员函数发送缓冲区(数据,大小(数据));

}

会员功能:

void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){连接->写入(大小,(uint8_t*)&缓冲区);}

更新

在 Daniel 的回复下(谢谢!),我将头文件和 cpp 文件中的成员函数更改为静态,如下所示:

 static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size);

在全局事件处理程序中调用该函数如下:

//shell 连接的事件处理程序;每当数据从 Android 发送到微控制器时调用void adbEventHandler(Connection * connection, adb_eventType 事件, uint16_t 长度, uint8_t * 数据){Serial.println("在数据接收处理程序中");Serial.println("收到数据:");//Serial.println(*data);Serial.println(数据[0]);Serial.println(数据[1]);字符 a = 数据 [0];字符 b = 数据 [1];Serial.println(a);Serial.println(b);//uint16_t data2 = 数据;CommunicationModuleUSB::SendBuffer(data, sizeof(data));}

直到现在我编译时才出现以下错误:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.h:26:错误:成员SendBuffer"上的额外资格CommunicationModuleUSB::".

有人知道该由谁来解决吗?

更新 2

再次感谢丹尼尔的回复!

我已根据您的反馈更改了成员函数.但现在我收到以下错误:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB\CommunicationModuleUSB.cpp:77: 错误:无法声明成员函数 'static void CommunicationModuleUSB::SendBuffer(uint8_t*, int)' 以具有静态链接

我在头文件中将 Connection 变量设为静态.下面是cpp文件中的头文件和函数定义.

您(或其他人)有任何线索吗?欢迎所有建议!

头文件:

#include "CommunicationModule.h"#include #include 类 CommunicationModuleUSB : public CommunicationModule{民众:通信模块USB();整数连接();无效发送();int CheckConnection();无效接收();static void SendBuffer(uint8_t* Buffer, int Size);void RecieveBuffer(char Buffer[], int Size);//Adb 连接使这个静态....(这是对的吗?静态连接 * 连接;//传感器采样的经过时间好久不见;私人的:};

cpp文件中的函数声明:

static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){连接->写入(大小,(uint8_t*)&缓冲区);}

以及全局函数中的调用:

CommunicationModuleUSB::SendBuffer(data, sizeof(data));

更新 3

我在 Daniel 的帮助下更新了 te 代码,我现在唯一的问题是在类中声明的 Connection 变量不再在范围内.

我得到的编译器错误如下:C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: 未定义对 CommunicationModuleUSB::connection 的引用C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79:未定义引用CommunicationModuleUSB::connection'CommunicationModuleUSB\CommunicationModuleUSB.cpp.o: 在函数 CommunicationModuleUSB::Connect()' 中:C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53:未定义引用CommunicationModuleUSB::connection'C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53:未定义对`CommunicationModuleUSB::connection'的引用

连接变量在头文件中声明如下:

//Adb 连接使这个静态....(是这样吗?静态连接 * 连接;

该变量用于以下成员函数:

void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){连接->写入(大小,(uint8_t*)&缓冲区);}

并在以下全局事件处理函数中使用:

//shell 连接的事件处理程序;每当数据从 Android 发送到微控制器时调用void adbEventHandler(Connection * connection, adb_eventType 事件, uint16_t 长度, uint8_t * 数据){Serial.println("在数据接收处理程序中");Serial.println("收到数据:");Serial.println(数据[0]);Serial.println(数据[1]);字符 a = 数据 [0];字符 b = 数据 [1];Serial.println(a);Serial.println(b);CommunicationModuleUSB::SendBuffer(data, sizeof(data));}

有人建议如何解决这个问题吗?

解决方案

成员函数是一个成员函数,这是有原因的.您正在调用 SendBuffer() 就好像它是在全局范围内定义的普通函数一样,但事实并非如此.您可以通过两种方式调用成员函数.

首先:创建一个类的实例,然后调用方法:

CommunicationModuleUSB cm();cm.SendBuffer(data, sizeof(data));

第二:你使方法static,所以签名如下:

static void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size);

所以声明看起来像这样:

class CommunicationModuleUSB{//其他的东西静态无效发送缓冲区(uint8_t * 缓冲区,整数大小);//其他的东西}

以及你对函数的定义:

void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){//你的代码}

现在你可以这样称呼它:

CommunicationModuleUSB::SendBuffer(data, sizeof(data));

但这有更多含义.使方法静态允许它只访问类的静态成员变量,因为它不属于任何特定对象.但是,这是有道理的,因为调用属于特定对象的方法与调用尚不存在的 Carroteat() 方法相同.

I have an event handler function and in that function there is a call to a member function of a class. The event handler function is declared in the class cpp file but not part of the class, it's no a member function.

When i compile the code the compiler says that the function is note in the scope, because it's calling a member function within the global event handler function.

My question is as followed: is there a way to use a meber function in a global function? (The object is created first on runtime).

Below is the member function and global event handler:

Global event handler:

void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t *     data)
{
     Serial.println("In data recieve handler");


    Serial.println("Data recieved: ");
    Serial.println(data[0]);
    Serial.println(data[1]);

    char a = data[0];
    char b = data[1];

   Serial.println(a);
   Serial.println(b);
   //uint16_t data2 = data;

   // Member function of USBCommunicator class
   SendBuffer(data, sizeof(data));

}

Member function:

void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){

    connection->write(Size,(uint8_t*)&Buffer);
}

Update

With the reply of Daniel (thank you!) i changed the member function in the header file and cpp file to static as followed:

    static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size);

And the function is called in the global eventhandler as followed:

// Event handler for shell connection; called whenever data sent from Android to Microcontroller
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t *       data)
{
   Serial.println("In data recieve handler");


   Serial.println("Data recieved: ");
   //Serial.println(*data);
   Serial.println(data[0]);
   Serial.println(data[1]);

   char a = data[0];
   char b = data[1];

   Serial.println(a);
   Serial.println(b);
   //uint16_t data2 = data;

   CommunicationModuleUSB::SendBuffer(data, sizeof(data));


 }

Only now i get the following error when i compile:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.h:26: error: extra qualification 'CommunicationModuleUSB::' on member 'SendBuffer.

Does anybody have a idea who to solve that?

Update 2

Thanks again Daniel for your reply!

I have changed the member function with your feedback. But now i get the following error:

C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB\CommunicationModuleUSB.cpp:77: error: cannot declare member function 'static void CommunicationModuleUSB::SendBuffer(uint8_t*, int)' to have static linkage

I have made the Connection variable static in the header file. Bellow is the header file and the function deffenition form the cpp file.

Do you (or someone else) have any clue? All suggestions are welcome!

Header file:

#include "CommunicationModule.h"
#include <SPI.h>
#include <Adb.h>

class CommunicationModuleUSB : public CommunicationModule
{
    public:

CommunicationModuleUSB();

int Connect();
      void Send();
int CheckConnection();
      void Recieve();
static void SendBuffer(uint8_t* Buffer, int Size);

void RecieveBuffer(char Buffer[], int Size);

// Adb connection made this static....(is this right?
static Connection * connection;

// Elapsed time for sensor sampling
long lastTime;

      private:
};

The function decleration in the cpp file:

static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){

    connection->write(Size,(uint8_t*)&Buffer);
}

And the call in the global function:

CommunicationModuleUSB::SendBuffer(data, sizeof(data));

Update 3

I have updated te code with the help of Daniel, the only problem that i have now is that the Connection variable that is declared in the class is not in the scope anymore.

The compiler error that i get is as followed: C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference to CommunicationModuleUSB::connection' C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference toCommunicationModuleUSB::connection' CommunicationModuleUSB\CommunicationModuleUSB.cpp.o: In function CommunicationModuleUSB::Connect()': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference toCommunicationModuleUSB::connection' C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference to `CommunicationModuleUSB::connection'

The connection variable is declared in the header file as followed:

// Adb connection made this static....(is this right?
      static Connection * connection;

The variable is used in the following member functions:

void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){

connection->write(Size,(uint8_t*)&Buffer);
}

And is used in the following global event handler function:

// Event handler for shell connection; called whenever data sent from Android to Microcontroller
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t *   data)
{
   Serial.println("In data recieve handler");


   Serial.println("Data recieved: ");
   Serial.println(data[0]);
   Serial.println(data[1]);

   char a = data[0];
   char b = data[1];

   Serial.println(a);
   Serial.println(b);

   CommunicationModuleUSB::SendBuffer(data, sizeof(data));
}

Doe anybody have a suggestion how to solve this?

解决方案

Member function is a member function and that is for a reason. You are calling SendBuffer() as if it was ordinary function defined in a global scope, which it is not. You can call member function in two ways.

First: You create a instance of a class and then you call the method:

CommunicationModuleUSB cm();
cm.SendBuffer(data, sizeof(data));

Second: You make the method static so the signature is as follows:

static void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size);

So declaration would look like so:

class CommunicationModuleUSB
{
    //Other stuff
    static void SendBuffer(uint8_t * Buffer, int Size);
    //Other stuff
}

and your definition of the function:

void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size)
{
    //Your code
}

Now you can call it like this:

CommunicationModuleUSB::SendBuffer(data, sizeof(data));

BUT this has more implications. Making the method static allows it to access only static member variables of the class as it does not belong to any particular object. This, however, makes sense as calling a method that belongs to a particular object is just the same as calling eat() method of Carrot that doesn't yet exist.

这篇关于声明对象前在全局函数中使用类的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆