Zaphoyd的broadcast_server.cpp看起来像是websocket服务器的完美主干,它可以快速地接受和发送消息&使用线程进行实际操作的连接,这样就不会中断通信。server.cpp
他的简单print_server.cpp示例很容易编译;但是,我在broadcast_server.cpp中得到了一些编译错误。
root@server:~# g++ -O3 broadcast_server.cpp -I ~/websocketpp-experimental/ -lboost_system
broadcast_server.cpp:126:37: error: 'owner_less' is not a member of 'std'
broadcast_server.cpp:126:37: error: 'owner_less' is not a member of 'std'
broadcast_server.cpp:126:70: error: 'con_list' was not declared in this scope
broadcast_server.cpp:126:70: error: template argument 2 is invalid
broadcast_server.cpp:126:18: warning: 'typedef' was ignored in this declaration [enabled by default]
broadcast_server.cpp:129:5: error: 'con_list' does not name a type
broadcast_server.cpp: In member function 'void broadcast_server::process_messages()':
broadcast_server.cpp:109:17: error: 'm_connections' was not declared in this scope
broadcast_server.cpp:112:17: error: 'm_connections' was not declared in this scope
broadcast_server.cpp:116:17: error: 'con_list' has not been declared
broadcast_server.cpp:116:36: error: expected ';' before 'it'
broadcast_server.cpp:117:22: error: 'it' was not declared in this scope
broadcast_server.cpp:117:27: error: 'm_connections' was not declared in this scope我是c++新手,这些可能都是简单的问题,但我找不到解决这些问题的方法。
'owner_less' is not a member of 'std'?较少
我猜想,当上述问题解决后,template argument 2 error就会消失。
'con_list' was not declared in this scope什么时候就在private:里?同样适用于'm_connections'
我猜当上面的错误发生时,'con_list' has not been declared就会消失。
我不知道它说的是哪一个'it',也不知道为什么它前面应该有一个;。
版本
Ubuntu 12.10
gcc --version gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
boost 1.53.0
#include**s** Extern外
#include <cstdlib>
#include <iostream>
#include <memory>
#include <set>还是什么都没有
-std=c++11
In file included from /usr/include/c++/4.7/map:60:0,
from /usr/local/include/boost/date_time/gregorian/greg_month.hpp:18,
from /usr/local/include/boost/date_time/gregorian/greg_ymd.hpp:16,
from /usr/local/include/boost/date_time/gregorian/greg_calendar.hpp:16,
from /usr/local/include/boost/date_time/gregorian/gregorian_types.hpp:19,
from /usr/local/include/boost/date_time/posix_time/posix_time_config.hpp:18,
from /usr/local/include/boost/date_time/posix_time/posix_time_system.hpp:13,
from /usr/local/include/boost/date_time/posix_time/ptime.hpp:12,
from /usr/local/include/boost/date_time/posix_time/posix_time_types.hpp:12,
from /usr/local/include/boost/thread/thread_time.hpp:11,
from /usr/local/include/boost/thread/lock_types.hpp:18,
from /usr/local/include/boost/thread/pthread/thread_data.hpp:12,
from /usr/local/include/boost/thread/thread.hpp:17,
from /usr/local/include/boost/thread.hpp:13,
from /root/websocketpp-experimental/websocketpp/common/thread.hpp:41,
from /root/websocketpp-experimental/websocketpp/concurrency/basic.hpp:31,
from /root/websocketpp-experimental/websocketpp/config/core.hpp:35,
from /root/websocketpp-experimental/websocketpp/config/asio_no_tls.hpp:31,
from broadcast_server.cpp:1:
/usr/include/c++/4.7/bits/stl_tree.h: In instantiation of 'class std::_Rb_tree<boost::weak_ptr<void>, boost::weak_ptr<void>, std::_Identity<boost::weak_ptr<void> >, std::owner_less<boost::weak_ptr<void> >, std::allocator<boost::weak_ptr<void> > >':
/usr/include/c++/4.7/bits/stl_set.h:116:17: required from 'class std::set<boost::weak_ptr<void>, std::owner_less<boost::weak_ptr<void> > >'
broadcast_server.cpp:132:14: required from here
/usr/include/c++/4.7/bits/stl_tree.h:471:31: error: invalid use of incomplete type 'struct std::owner_less<boost::weak_ptr<void> >'
In file included from /usr/include/c++/4.7/bits/shared_ptr.h:52:0,
from /usr/include/c++/4.7/memory:87,
from /usr/local/include/boost/config/no_tr1/memory.hpp:21,
from /usr/local/include/boost/smart_ptr/shared_ptr.hpp:27,
from /usr/local/include/boost/shared_ptr.hpp:17,
from /usr/local/include/boost/date_time/time_clock.hpp:17,
from /usr/local/include/boost/thread/thread_time.hpp:9,
from /usr/local/include/boost/thread/lock_types.hpp:18,
from /usr/local/include/boost/thread/pthread/thread_data.hpp:12,
from /usr/local/include/boost/thread/thread.hpp:17,
from /usr/local/include/boost/thread.hpp:13,
from /root/websocketpp-experimental/websocketpp/common/thread.hpp:41,
from /root/websocketpp-experimental/websocketpp/concurrency/basic.hpp:31,
from /root/websocketpp-experimental/websocketpp/config/core.hpp:35,
from /root/websocketpp-experimental/websocketpp/config/asio_no_tls.hpp:31,
from broadcast_server.cpp:1:
/usr/include/c++/4.7/bits/shared_ptr_base.h:270:12: error: declaration of 'struct std::owner_less<boost::weak_ptr<void> >'如何安装(依赖项、实际程序等)
https://github.com/zaphoyd/websocketpp/wiki/Setup-0.3X-on-Ubuntu-12.10
替代品欢迎
如果有人有替代std::owner_less,请张贴他们。
发布于 2013-03-18 03:26:18
std::owner_less是一个C++11 STL特性,允许您将std::weak_ptr放在诸如set和map这样的关联容器中。最终,connection_hdl是一个weak_ptr,这就是它需要自定义比较函数的原因。
WebSocket++ 0.3.x可以通过预处理器定义配置为使用C++03 + Boost或C++11,默认情况下使用C++03 + Boost。Boost weak_ptr的工作方式有点不同,不需要定制的比较函数。如果使用列表、队列或向量,也没有必要,因为这些容器不使用connection_hdl作为键。
0.3.x手册中和库捆绑在一起的大多数示例都使用C++11,因为它更简洁、更简洁。要编译和链接这些示例,您需要一个C++11编译器,并使用适当的C++11编译器标志和WebSocket++定义。Scon是我用来自动化构建的构建系统&运行所有的示例和单元测试。没有必要构建和运行单个示例程序。任何其他构建系统都应该正常工作,就像在命令行上临时提供编译器参数一样。
我在手册中设置了一个页面,描述启用/禁用C++11特性的各种选项。http://www.zaphoyd.com/websocketpp/manual/reference/cpp11-support。此手册页列出了我最常用于测试的操作系统和编译器的推荐编译器标志。
对于Linux上的g++ 4.7,我建议:"-std=c++0x -D_WEBSOCKETPP_CPP11_STL_ -D_WEBSOCKETPP_NO_CPP11_REGEX_ -lboost_regex -lboost_system“。这使得除了库(它是坏的/buggy)之外的所有C++11特性都能实现,并引入Boost版本。
发布于 2013-03-15 14:35:12
在阅读了文档之后,我认为您在构建时需要定义BOOST_ROOT_CPP11。
类似于BOOST_ROOT,但允许链接到针对C++11 STL编译的单独Boost版本
我的基础是以下标题
#ifdef _WEBSOCKETPP_CPP11_MEMORY_
using std::shared_ptr;
using std::weak_ptr;
using std::enable_shared_from_this;
using std::static_pointer_cast;
typedef std::unique_ptr<unsigned char[]> unique_ptr_uchar_array;
#else
using boost::shared_ptr;
using boost::weak_ptr;
using boost::enable_shared_from_this;
using boost::static_pointer_cast;
typedef boost::scoped_array<unsigned char> unique_ptr_uchar_array;
#endif它应该使用std::weak_ptr而不是boost::weak_ptr
如果希望使用备用容器而不是std::set,请修改类似于以下伪代码的示例
if (a.type == SUBSCRIBE) {
boost::unique_lock<boost::mutex> lock(m_connection_lock);
m_connections.push_back(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
boost::unique_lock<boost::mutex> lock(m_connection_lock);
m_connections.erase(std::remove(m_connections.begin(), m_connections.end(), a.hdl));
} else if (a.type == MESSAGE) {
boost::unique_lock<boost::mutex> lock(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
}
} else {
// undefined.
}
}
}
private:
typedef std::deque<connection_hdl> con_list;它将使用std::deque而不是std::set。排序将不被使用,但是对于我所能知道的,在本例中并不需要。
https://stackoverflow.com/questions/15423931
复制相似问题