在另一个类中调用一个类(队列)的方法
calling the method of a class (queue) inside another class
node.h
--用于创建主机
// host declaration
class Host : public Node {
public:
Host(uint32_t id, double rate, uint32_t queue_type, uint32_t host_type); // constructor
Queue *queue; // queue to store the packets
int host_type;
};
queue.h
--队列声明,使用deque 实现
class Queue {
public:
Queue(uint32_t id, double rate, uint32_t limit_bytes, int location); // default constructor
virtual void enque(Packet *packet); // I want to call this function
};
main.cpp
--创建对象并尝试将数据包推送到主机中的队列的主程序
# include all the .h files
using namespace std;
int main()
{
Packet P1(20.4, 1, 0, 64); // creating a packet, not shows for simplicity
Host H1(0, 20.4,1, 0); // creating my host
H1.queue->enque(P1*); // This is where I get error
// "invalid pointer". I want to push the
//packet P1 to the queue in the host,
//I am not sure how to do it.
return 0;
}
非常感谢您的帮助。谢谢,
您需要使用H1.queue->enque(&P1);
。这取自动变量P1
的地址。
但请记住,&P1
仅在P1
在范围内时有效。在生产代码中,您可能希望使用new
创建一个Packet
实例,并将其用作enque
的参数。
相关文章:
- 不能将方法返回的值用于另一个方法
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 我在C++中编写了一个方法来打印树类的预序,但它显示了分割错误
- 重写另一个方法 [C++] 使用的超类回调函数
- C++:如何使用传递引用在一个方法中返回多个值
- 为什么一个方法对同一个变量有常量和非常量参数?
- 从同一类中的另一个方法调用方法时出错
- 向现有类添加一个方法以逐行读取文件的内容
- 无法从派生的一个方法调用基类方法
- 如何将一个方法中的值返回到同一类的另一个方法
- 方法重载 C++ -> 只有一个方法调用
- 在另一个线程中执行一个方法
- 是否可以在另一个方法中使用模板类中定义的虚拟方法
- 调用方法,该方法修改字段,而使用该字段的另一个方法正在执行
- 将一个方法作为参数传递给另一个方法,该方法本身将传递的方法传递给 std 线程
- 仅专用于模板类的一个方法(部分)
- 确定一个方法是否是纯虚拟的(c++)
- 是否可以判断一个方法在编译时是否可以公开访问
- 我正在尝试创建一个方法,该方法使用指针算术返回一对,它是否会出错
- 从 ndk 中的 jni 方法调用另一个C++方法