I want to write a test program with amqpcppCopernicaMarketingSoftware/AMQP-CPP. But the compiler(g++) complains:
g++ main.cpp rabbitmq_handler.cpp -std=c++11 -lamqpcpp -lpthread -ldl
In file included from /usr/local/include/amqpcpp/linux_tcp.h:2:0,
from /usr/local/include/amqpcpp/libevent.h:23,
from rabbitmq_handler.h:6,
from main.cpp:1:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:30:30: error: expected class-name before ‘,’ token
private ConnectionHandler,
^
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:32:1: error: expected class-name before ‘{’ token
{
^
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:39:10: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type
std::shared_ptr<TcpState> _state;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:45:5: error: ‘Connection’ does not name a type; did you mean ‘TcpConnection’?
Connection _connection;
^~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:53:34: error: ‘Connection’ has not been declared
virtual uint16_t onNegotiate(Connection *connection, uint16_t interval) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:61:25: error: ‘Connection’ has not been declared
virtual void onData(Connection *connection, const char *buffer, size_t size) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:67:30: error: ‘Connection’ has not been declared
virtual void onHeartbeat(Connection *connection) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:74:26: error: ‘Connection’ has not been declared
virtual void onError(Connection *connection, const char *message) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:80:30: error: ‘Connection’ has not been declared
virtual void onConnected(Connection *connection) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:86:27: error: ‘Connection’ has not been declared
virtual void onClosed(Connection *connection) override;
^~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:92:26: error: ‘Buffer’ does not name a type
uint64_t parse(const Buffer &buffer)
^~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:112:46: error: ‘Address’ does not name a type
TcpConnection(TcpHandler *handler, const Address &address);
^~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:53:22: error: ‘virtual uint16_t AMQP::TcpConnection::onNegotiate(int*, uint16_t)’ marked ‘override’, but does not override
virtual uint16_t onNegotiate(Connection *connection, uint16_t interval) override;
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:61:18: error: ‘virtual void AMQP::TcpConnection::onData(int*, const char*, size_t)’ marked ‘override’, but does not override
virtual void onData(Connection *connection, const char *buffer, size_t size) override;
^~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:67:18: error: ‘virtual void AMQP::TcpConnection::onHeartbeat(int*)’ marked ‘override’, but does not override
virtual void onHeartbeat(Connection *connection) override;
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:74:18: error: ‘virtual void AMQP::TcpConnection::onError(int*, const char*)’ marked ‘override’, but does not override
virtual void onError(Connection *connection, const char *message) override;
^~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:80:18: error: ‘virtual void AMQP::TcpConnection::onConnected(int*)’ marked ‘override’, but does not override
virtual void onConnected(Connection *connection) override;
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:86:18: error: ‘virtual void AMQP::TcpConnection::onClosed(int*)’ marked ‘override’, but does not override
virtual void onClosed(Connection *connection) override;
^~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘uint64_t AMQP::TcpConnection::parse(const int&)’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:95:16: error: ‘_connection’ was not declared in this scope
return _connection.parse(buffer);
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:95:16: note: suggested alternative: ‘TcpConnection’
return _connection.parse(buffer);
^~~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘bool AMQP::TcpConnection::close()’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:154:16: error: ‘_connection’ was not declared in this scope
return _connection.close();
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:154:16: note: suggested alternative: ‘TcpConnection’
return _connection.close();
^~~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘uint32_t AMQP::TcpConnection::maxFrame() const’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:163:16: error: ‘_connection’ was not declared in this scope
return _connection.maxFrame();
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:163:16: note: suggested alternative: ‘TcpConnection’
return _connection.maxFrame();
^~~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘uint32_t AMQP::TcpConnection::expected() const’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:172:16: error: ‘_connection’ was not declared in this scope
return _connection.expected();
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:172:16: note: suggested alternative: ‘TcpConnection’
return _connection.expected();
^~~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘std::size_t AMQP::TcpConnection::channels() const’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:182:16: error: ‘_connection’ was not declared in this scope
return _connection.channels();
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:182:16: note: suggested alternative: ‘TcpConnection’
return _connection.channels();
^~~~~~~~~~~
TcpConnection
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h: In member function ‘bool AMQP::TcpConnection::heartbeat()’:
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:197:16: error: ‘_connection’ was not declared in this scope
return _connection.heartbeat();
^~~~~~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpconnection.h:197:16: note: suggested alternative: ‘TcpConnection’
return _connection.heartbeat();
^~~~~~~~~~~
TcpConnection
In file included from /usr/local/include/amqpcpp/linux_tcp.h:3:0,
from /usr/local/include/amqpcpp/libevent.h:23,
from rabbitmq_handler.h:6,
from main.cpp:1:
/usr/local/include/amqpcpp/linux_tcp/tcpchannel.h: At global scope:
/usr/local/include/amqpcpp/linux_tcp/tcpchannel.h:24:1: error: expected class-name before ‘{’ token
{
^
/usr/local/include/amqpcpp/linux_tcp/tcpchannel.h: In constructor ‘AMQP::TcpChannel::TcpChannel(AMQP::TcpConnection*)’:
/usr/local/include/amqpcpp/linux_tcp/tcpchannel.h:35:9: error: class ‘AMQP::TcpChannel’ does not have any field named ‘Channel’
Channel(&connection->_connection) {}
^~~~~~~
/usr/local/include/amqpcpp/linux_tcp/tcpchannel.h:35:30: error: ‘class AMQP::TcpConnection’ has no member named ‘_connection’; did you mean ‘TcpConnection’?
Channel(&connection->_connection) {}
^~~~~~~~~~~
TcpConnection
In file included from rabbitmq_handler.h:6:0,
from main.cpp:1:
/usr/local/include/amqpcpp/libevent.h: At global scope:
/usr/local/include/amqpcpp/libevent.h:140:10: error: ‘map’ in namespace ‘std’ does not name a template type
std::map<int,std::unique_ptr<Watcher>> _watchers;
^~~
/usr/local/include/amqpcpp/libevent.h: In member function ‘virtual void AMQP::LibEventHandler::monitor(AMQP::TcpConnection*, int, int)’:
/usr/local/include/amqpcpp/libevent.h:152:21: error: ‘_watchers’ was not declared in this scope
auto iter = _watchers.find(fd);
^~~~~~~~~
/usr/local/include/amqpcpp/libevent.h:152:21: note: suggested alternative: ‘Watcher’
auto iter = _watchers.find(fd);
^~~~~~~~~
Watcher
/usr/local/include/amqpcpp/libevent.h:161:34: error: ‘unique_ptr’ is not a member of ‘std’
_watchers[fd] = std::unique_ptr<Watcher>(new Watcher(_evbase, connection, fd, flags));
^~~~~~~~~~
/usr/local/include/amqpcpp/libevent.h:161:52: error: expected primary-expression before ‘>’ token
_watchers[fd] = std::unique_ptr<Watcher>(new Watcher(_evbase, connection, fd, flags));
The g++
version is 7.3.0, the system is ubuntu16.04
, amqpcpp
version is 3.1.0.
My code is as below, there are 3 files, rabbitmq_handler.h
, rabbitmq_handler.cpp
and main.cc
.
rabbitmq_handler.h
#pragma once
#include <functional>
#include <unistd.h>
#include <event2/event.h>
#include <amqpcpp/libevent.h>
#include <amqpcpp/linux_tcp.h>
class LibEventHandlerMyError : public AMQP::LibEventHandler
{
public:
LibEventHandlerMyError(struct event_base* evbase) : LibEventHandler(evbase), evbase_(evbase) {}
void onError(AMQP::TcpConnection *connection, const char *message) override
{
event_base_loopbreak(evbase_);
}
private:
struct event_base* evbase_ {nullptr};
};
int init_rabbitmq_conn();
rabbitmq_handler.cpp
#include <iostream>
#include <amqpcpp.h>
#include "rabbitmq_handler.h"
int init_rabbitmq_conn()
{
auto evbase = event_base_new();
LibEventHandlerMyError hndl(evbase);
AMQP::TcpConnection connection(&hndl, AMQP::Address("amqp://localhost:5672/"));
AMQP::TcpChannel channel(&connection);
channel.onError([&evbase](const char* message)
{
std::cout << "Channel error: " << message << std::endl;
event_base_loopbreak(evbase);
});
channel.declareQueue("hello", AMQP::exclusive)
.onSuccess
(
[&connection](const std::string &name,
uint32_t messagecount,
uint32_t consumercount)
{
std::cout << "Queue: " << name << std::endl;
}
)
.onFinalize
(
[&connection]()
{
std::cout << "Finalize." << std::endl;
connection.close();
}
);
channel.publish("", "hello", "Hello, world!");
event_base_dispatch(evbase);
event_base_free(evbase);
return 0;
}
main.cpp
#include "rabbitmq_handler.h"
int main(){
init_rabbitmq_conn();
return 0;
}
You violate the rule in the manual:
After building there are two relevant files to include when using the library.
+-----------+---------------+
| File | Include when? |
+-----------+---------------+
| amqpcpp.h | Always |
+-----------+---------------+
You must #include <amqpcpp.h>
before any #include <amqpcpp/...>
in your rabbitmq_handler.h.