在C 中运行时从指针访问对象实例
Accessing Object Instance from Pointer during Runtime in C++
我正在实现XBEEP库。我发现此片段将在主要循环中工作:
libxbee::XBee xbee("xbee3", "/dev/ttyUSB0", 9600);
myCB con(xbee, "Data", &address);
con << "Hello World";
但是,由于我将在运行时创建连接,因此我修改了上述代码,以便在调用创建连接的方法时创建对象。我将它们宣布为指针,而不是作为对象:
libxbee::XBee* xbee;
connectionCB* con;
然后在打开连接时,我实例化:
void XbeeController::XbeeController::openXbeeConnection(QString port, int bRate, QString deviceAddr)
{
baudRate = bRate;
xbee = new libxbee::XBee(DEVICE_REV, "/dev/ttyUSB0", 9600); //Burnt values for proof of concept.
con = new connectionCB(*xbee, "Data", &address);
}
我的代码现在失败
void XbeeController::XbeeController::sendXbeeMessage(std::string message)
{
con << message;
}
错误:类型的" Xbee :: ConnectionCB*’的操作数无效"answers" char*"answers" char*" to binary'operator&lt;&lt;’’ con&lt;&lt;"你好世界"; ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
如何将con
视为对象而不是指针,以便像以前一样工作?
现在con
是一个指针,但是operator<<()
要求参考,并可能定义了类似的内容: connectionCB &operator<<(connectionCB &cb, const char *str);
使用: *con << "Hello World";
相关文章:
- 使用迭代器时如何访问对象在向量中的位置?
- 无法通过指针访问对象的成员
- 如何从传递到对象的对象内部访问对象的类属性?
- 有没有办法在C++中循环访问对象的不同数据成员
- 如何访问对象动态数组中的私有成员变量?
- c++ 循环访问对象列表并删除对象
- 循环访问对象列表 c++
- 访问对象的取消引用值的语法
- 如何访问对象的成员变量的取消引用值
- C++:访问对象内部子对象的方法
- 如果访问对象,则指向的对象内部的指针会更改内存位置
- 是否可以从 C++ 中的构造函数访问对象名称?
- 仅允许访问对象的成员,而不允许访问对象本身
- 从地图访问对象会导致新对象C 的实例化
- 如何在数组中访问对象内的字符串
- 在C 中运行时从指针访问对象实例
- 有关使用矢量迭代器访问对象方法的问题
- 当对象的指针存储在矢量中时,如何访问对象中的方法?
- 显式调用"运算符 new"后无法访问对象的函数
- 尝试访问对象数组中的对象,但只保存了一个对象