Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';编译AMQP-CPP示例代码?_C++_Compilation_G++_Amqp - Fatal编程技术网

C++ 为什么可以';编译AMQP-CPP示例代码?

C++ 为什么可以';编译AMQP-CPP示例代码?,c++,compilation,g++,amqp,C++,Compilation,G++,Amqp,我正在尝试从AMQP-CPP自述文件编译一些示例代码:。但由于某些原因,我无法编译为TcpConnection提供的示例。一切看起来都很好,但我不断得到: g++ --std=c++11 -Wall -lamqpcpp -lpthread -ldl -c main.cpp -o main.o main.cpp:20:1: error: 'channel' does not name a type channel.declareExchange("my-exchange", AMQP::fan

我正在尝试从AMQP-CPP自述文件编译一些示例代码:。但由于某些原因,我无法编译为TcpConnection提供的示例。一切看起来都很好,但我不断得到:

g++ --std=c++11 -Wall -lamqpcpp -lpthread -ldl -c main.cpp -o main.o

main.cpp:20:1: error: 'channel' does not name a type
 channel.declareExchange("my-exchange", AMQP::fanout);
 ^~~~~~~
main.cpp:21:1: error: 'channel' does not name a type
 channel.declareQueue("my-queue");
 ^~~~~~~
main.cpp:22:1: error: 'channel' does not name a type
 channel.bindQueue("my-exchange", "my-queue", "my-routing-key");


有关守则:

//Main.cpp
#include "MyTcpHandler.h"
#include <amqpcpp.h>
#include <amqpcpp/linux_tcp.h>
#include <amqpcpp/linux_tcp/tcpchannel.h>

// create an instance of your own tcp handler
MyTcpHandler myHandler;

// address of the server
AMQP::Address address("amqp://guest:guest@localhost/vhost");

// create a AMQP connection object
AMQP::TcpConnection connection(&myHandler, address);

// and create a channel
AMQP::TcpChannel channel(&connection);                                                                                                                                                                      


// use the channel object to call the AMQP method you like
channel.declareExchange("my-exchange", AMQP::fanout);
channel.declareQueue("my-queue");
channel.bindQueue("my-exchange", "my-queue", "my-routing-key");
~                                                                 
//Main.cpp
#包括“Mytchphandler.h”
#包括
#包括
#包括
//创建自己的tcp处理程序的实例
Mytchphandler myHandler;
//服务器地址
AMQP::地址(“amqp://guest:guest@本地主机/vhost”);
//创建AMQP连接对象
AMQP::TcpConnection连接(&myHandler,address);
//并创建一个频道
AMQP::TCP通道通道和连接;
//使用channel对象调用您喜欢的AMQP方法
申报交换(“我的交换”,AMQP::扇出);
申报台(“我的队列”);
bindQueue(“我的交换”、“我的队列”、“我的路由密钥”);
~                                                                 
//MyTcpHandler.h
#包括
#包括
类MyTcpHandler:public AMQP::TcpHandler
{
/**
*方法,该方法在创建新连接时由AMQP库调用
*与处理程序关联。这是对处理程序的第一次调用
*@param connection连接到处理程序的连接
*/
虚拟连接(AMQP::TcpConnection*连接)覆盖
{
//@todo
//添加您自己的实现,例如初始化东西
//来处理连接。
}
/**
*方法,该方法在TCP连接中断时由AMQP库调用
*已建立。调用此方法后,库
*仍然需要注意设置可选的TLS层和
*在TCP层顶部设置AMQP连接。此方法
*始终与稍后对onLost()的调用配对。
*@param connection现在可以使用的连接
*/
虚拟void onConnected(AMQP::TcpConnection*连接)覆盖
{
//@todo
//添加您自己的实现(可能不需要)
}
/**
*方法,该方法在建立安全TLS连接时调用。
*这仅对amqps://连接调用。它允许您检查
*连接是否安全到您喜欢的程度(您可以
*例如,检查服务器证书)。AMQP协议仍然具有
*开始。
*@param connection已保护的连接
*来自openssl库的@param ssl结构
*@return bool True(如果可以使用连接)
*/
虚拟bool-onSecured(AMQP::TcpConnection*connection,const-SSL*SSL)覆盖
{
//@todo
//添加您自己的实现,例如通过读取
//证书,并检查它是否确实是您的
返回true;
}
/**
*方法,该方法在尝试登录时由AMQP库调用
*成功。在此之后,连接即可使用。
*@param connection现在可以使用的连接
*/
虚拟void onReady(AMQP::TcpConnection*连接)覆盖
{
//@todo
//添加您自己的实现,例如通过创建通道
//实例,并开始发布或使用
}
/**
*方法,该方法在发生致命错误时由AMQP库调用
*在连接上,例如,因为从RabbitMQ接收到数据
*无法识别,或基础连接已丢失。此
*调用之后通常是对onLost()的调用(如果发生错误
*TCP连接建立后)和OnAttached()。
*@param connection发生错误的连接
*@param message一条人类可读的错误消息
*/
虚拟void onError(AMQP::TcpConnection*连接,const char*消息)重写
{
//@todo
//添加您自己的实现,例如通过报告错误
//发送给程序的用户并记录错误
}
/**
*方法,该方法在AMQP协议结束时调用。这是
*调用connection.close()以正常关机的计数器部分
*连接。请注意,TCP连接此时仍然有效
*处于活动状态,您还将收到对onLost()和onDetached()的调用
*@param connection AMQP协议结束的连接
*/
虚拟void onClosed(AMQP::TcpConnection*连接)覆盖
{
//@todo
//添加您自己的实现(可能没有必要,但可以
//如果你想在考试结束后立即做一些事情,那么这将非常有用
//amqp连接已结束,但不想等待tcp
//要关闭的连接
}
/**
*方法,该方法在TCP连接关闭或丢失时调用。
*如果还调用了onConnected(),则始终调用此方法
*@param connection已关闭且现在不可用的连接
*/
虚拟void onLost(AMQP::TcpConnection*连接)重写
{
//@todo
//添加您自己的实现(可能不需要)
}
/**
*调用的最后一个方法。这表示不再调用
*将对连接进行处理。
*@param connection可以破坏的连接
*/
虚拟无效已连接(AMQP::TcpConnection*连接)覆盖
{
//@todo
//添加您自己的实现,如清理资源或退出应用程序
} 
/**
*方法,AMQP-CPP库在需要交互时调用该方法
*与
// MyTcpHandler.h
#include <amqpcpp.h>
#include <amqpcpp/linux_tcp.h>

class MyTcpHandler : public AMQP::TcpHandler
{
    /**
     *  Method that is called by the AMQP library when a new connection
     *  is associated with the handler. This is the first call to your handler
     *  @param  connection      The connection that is attached to the handler
     */
    virtual void onAttached(AMQP::TcpConnection *connection) override
    {
        // @todo
        //  add your own implementation, for example initialize things
        //  to handle the connection.
    }

    /**
     *  Method that is called by the AMQP library when the TCP connection 
     *  has been established. After this method has been called, the library
     *  still has take care of setting up the optional TLS layer and of
     *  setting up the AMQP connection on top of the TCP layer., This method 
     *  is always paired with a later call to onLost().
     *  @param  connection      The connection that can now be used
     */
    virtual void onConnected(AMQP::TcpConnection *connection) override
    {
        // @todo
        //  add your own implementation (probably not needed)
    }

    /**
     *  Method that is called when the secure TLS connection has been established. 
     *  This is only called for amqps:// connections. It allows you to inspect
     *  whether the connection is secure enough for your liking (you can
     *  for example check the server certicate). The AMQP protocol still has
     *  to be started.
     *  @param  connection      The connection that has been secured
     *  @param  ssl             SSL structure from openssl library
     *  @return bool            True if connection can be used
     */
    virtual bool onSecured(AMQP::TcpConnection *connection, const SSL *ssl) override
    {
        // @todo
        //  add your own implementation, for example by reading out the
        //  certificate and check if it is indeed yours
        return true;
    }

    /**
     *  Method that is called by the AMQP library when the login attempt
     *  succeeded. After this the connection is ready to use.
     *  @param  connection      The connection that can now be used
     */
    virtual void onReady(AMQP::TcpConnection *connection) override
    {
        // @todo
        //  add your own implementation, for example by creating a channel
        //  instance, and start publishing or consuming
    }

    /**
     *  Method that is called by the AMQP library when a fatal error occurs
     *  on the connection, for example because data received from RabbitMQ
     *  could not be recognized, or the underlying connection is lost. This
     *  call is normally followed by a call to onLost() (if the error occured
     *  after the TCP connection was established) and onDetached().
     *  @param  connection      The connection on which the error occured
     *  @param  message         A human readable error message
     */
    virtual void onError(AMQP::TcpConnection *connection, const char *message) override
    {
        // @todo
        //  add your own implementation, for example by reporting the error
        //  to the user of your program and logging the error
    }

    /**
     *  Method that is called when the AMQP protocol is ended. This is the
     *  counter-part of a call to connection.close() to graceful shutdown
     *  the connection. Note that the TCP connection is at this time still 
     *  active, and you will also receive calls to onLost() and onDetached()
     *  @param  connection      The connection over which the AMQP protocol ended
     */
    virtual void onClosed(AMQP::TcpConnection *connection) override 
    {
        // @todo
        //  add your own implementation (probably not necessary, but it could
        //  be useful if you want to do some something immediately after the
        //  amqp connection is over, but do not want to wait for the tcp 
        //  connection to shut down
    }

    /**
     *  Method that is called when the TCP connection was closed or lost.
     *  This method is always called if there was also a call to onConnected()
     *  @param  connection      The connection that was closed and that is now unusable
     */
    virtual void onLost(AMQP::TcpConnection *connection) override 
    {
        // @todo
        //  add your own implementation (probably not necessary)
    }

    /**
     *  Final method that is called. This signals that no further calls to your
     *  handler will be made about the connection.
     *  @param  connection      The connection that can be destructed
     */
    virtual void onDetached(AMQP::TcpConnection *connection) override 
    {
        // @todo
        //  add your own implementation, like cleanup resources or exit the application
    } 

    /**
     *  Method that is called by the AMQP-CPP library when it wants to interact
     *  with the main event loop. The AMQP-CPP library is completely non-blocking,
     *  and only make "write()" or "read()" system calls when it knows in advance
     *  that these calls will not block. To register a filedescriptor in the
     *  event loop, it calls this "monitor()" method with a filedescriptor and
     *  flags telling whether the filedescriptor should be checked for readability
     *  or writability.
     *
     *  @param  connection      The connection that wants to interact with the event loop
     *  @param  fd              The filedescriptor that should be checked
     *  @param  flags           Bitwise or of AMQP::readable and/or AMQP::writable
     */
    virtual void monitor(AMQP::TcpConnection *connection, int fd, int flags) override
    {
        // @todo
        //  add your own implementation, for example by adding the file
        //  descriptor to the main application event loop (like the select() or
        //  poll() loop). When the event loop reports that the descriptor becomes
        //  readable and/or writable, it is up to you to inform the AMQP-CPP
        //  library that the filedescriptor is active by calling the
        //  connection->process(fd, flags) method.
    }
};
channel.declareExchange("my-exchange", AMQP::fanout);
channel.declareQueue("my-queue");
channel.bindQueue("my-exchange", "my-queue", "my-routing-key");
error: 'channel' does not name a type