【发布时间】:2020-07-03 02:26:09
【问题描述】:
我正在尝试使用 zmq 创建一个通用节点,该节点将形成一个动态计算图,但是在我的类中的 zmq 套接字的前向声明中出现错误。我想知道是否有人可以对此有所了解?该类的精简版是;
node.hpp
/*
* node.hpp
*/
#ifndef NODE_
#define NODE_
#include <iostream>
#include "zmq.hpp"
class Node
{
private:
std::string name_;
std::ostream& log_;
zmq::context_t context_;
zmq::socket_t subscriber_;
zmq::socket_t publisher_;
public:
Node(std::ostream& log, std::string name);
void sendlog(std::string msg);
};
#endif // NODE_
node.cpp
/*
* node.cpp
*/
#include <iostream>
#include <string>
#include "zmq.hpp"
#include "node.hpp"
Node::Node(std::ostream& log, std::string name):
log_(log),
name_(name)
{
sendlog(std::string("initialising ") + name_);
zmq::context_t context_(1);
zmq::socket_t subscriber_(context_, zmq::socket_type::sub);
zmq::socket_t publisher_(context_, zmq::socket_type::pub);
subscriber_.connect("ipc:///tmp/out.ipc");
publisher_.connect("ipc:///tmp/in.ipc");
sendlog(std::string("finished initialisation"));
}
void Node::sendlog(std::string msg)
{
this->log_ << msg << std::endl;
}
我从 g++ 得到的错误
g++ main.cpp node.cpp -lzmq
node.cpp: In constructor ‘Node::Node(std::ostream&, std::__cxx11::string)’:
node.cpp:12:15: error: no matching function for call to ‘zmq::socket_t::socket_t()’
name_(name)
但是,当我查看 zmq.hpp 时,我看到了
namespace zmq
{
class socket_t : public detail::socket_base
...
我认为我以某种方式错误地进行了声明?我不太精通 cpp,但我将其作为一个项目来重新开始,因此欢迎一般的 cmets/文献参考。
【问题讨论】:
-
我认为问题在于
socket_t没有默认构造函数,并且您的编译器在初始化name和log时试图调用它。在构造函数体中初始化name和log会发生什么? -
尝试在构造函数的初始化列表中初始化所有
context_、subscriber_和publisher_。实际上,您已经创建了局部范围的变量,这些变量隐藏了同名的成员变量。
标签: c++ class zeromq forward-declaration