小编Tor*_*ius的帖子

使用类模板需要模板参数列表

我从我的类中移出了方法实现并捕获了以下错误:

use of class template requires template argument list
Run Code Online (Sandbox Code Playgroud)

对于方法whitch根本不需要模板类型...(对于其他方法都可以)

template<class T>
class MutableQueue
{
public:
    bool empty() const;
    const T& front() const;
    void push(const T& element);
    T pop();

private:
    queue<T> queue;
    mutable boost::mutex mutex;
    boost::condition condition;
};
Run Code Online (Sandbox Code Playgroud)

错误的实施

template<>   //template<class T> also incorrect
bool MutableQueue::empty() const
{
    scoped_lock lock(mutex);
    return queue.empty();
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

21
推荐指数
2
解决办法
7万
查看次数

提升异步tcp客户端

我刚刚开始使用boost.我正在使用异步套接字编写TCP客户端服务器.

任务如下:

  1. 客户端向服务器发送一个号码
  2. 客户端可以在收到服务器的答案之前发送另一个nubmer.
  3. 服务器接收一个号码,用它做一些计算并将结果发送回客户端.
  4. 多个客户端可以连接到服务器.

现在工作如下

  • 从客户端向服务器发送号码
  • 服务器接收当前线程中的数字并在OnReceive处理程序中计算(我知道这很糟糕......但我应该如何开始一个新的线程来并行执行计算)
  • 服务器发回应答但客户端已断开连接

如何允许客户端从键盘输入数字并同时等待服务器的回答?

为什么我的客户不等待服务器的答案?

客户端代码:

using boost::asio::ip::tcp;

class TCPClient
{
    public:
        TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter);
        void Close();

    private:
        boost::asio::io_service& m_IOService;
        tcp::socket m_Socket;

        string m_SendBuffer;
        static const size_t m_BufLen = 100;
        char m_RecieveBuffer[m_BufLen*2];

        void OnConnect(const boost::system::error_code& ErrorCode, tcp::resolver::iterator EndPointIter);
        void OnReceive(const boost::system::error_code& ErrorCode);
        void OnSend(const boost::system::error_code& ErrorCode);
        void DoClose();
};

TCPClient::TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter)
: m_IOService(IO_Service), m_Socket(IO_Service), m_SendBuffer("")
{
    tcp::endpoint EndPoint = *EndPointIter;

    m_Socket.async_connect(EndPoint,
        boost::bind(&TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter));
}

void TCPClient::Close()
{
    m_IOService.post( …
Run Code Online (Sandbox Code Playgroud)

c++ client boost boost-asio

5
推荐指数
1
解决办法
1万
查看次数

带升压单元测试的g ++项目编译

我正在尝试在Linux上编译单元测试(boost),但编译器发生了错误.有人可以查看我的命令吗?

g++ -o UTest ../UTest/UT1.cpp ../UTest/UT2.cpp -lboost_system -lboost_thread -lboost_unit_test_framework 
Run Code Online (Sandbox Code Playgroud)

错误

/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
Run Code Online (Sandbox Code Playgroud)

main()从g ++命令中删除了因为它在使用时不应该boost unit test使用.

怎么了?

PS没有单元测试(带main())的项目编译得很好.Windows上的单元测试工作正常.

更新

问题main()已解决.但是一个新的诞生了.

双方UT1.cppUT2.cpp已列入UTCommon.h,现在我有很多类似如下的错误

错误

tmp2.cpp:(.text+0xd44a): multiple definition of `boost::unit_test::unit_test_log_t::operator<<(boost::unit_test::lazy_ostream const&)'
/tmp/cc0jw8uR.o:tmp.cpp:(.text+0xd44a): first defined here
/tmp/cctLn9QJ.o: In function `boost::test_tools::tt_detail::equal_impl(char const*, char const*)'
Run Code Online (Sandbox Code Playgroud)

UTCommon.h

#ifndef UT_COMMON_H
#define UT_COMMON_H

#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE UnitTest
#endif

#if defined (__GNUC__) && defined(__unix__)
    #include <boost/test/included/unit_test.hpp>
#elif defined …
Run Code Online (Sandbox Code Playgroud)

c++ boost g++

4
推荐指数
1
解决办法
7152
查看次数

具有内存大小定义的标头

是否有标准的C/C++标头,其定义为byte,kilobyte,megabyte,......?我不想做自己的定义.在我看来这很脏.

例:

if (size > MEGABYTE)
{ ... }
Run Code Online (Sandbox Code Playgroud)

c c++ size header-files c-preprocessor

3
推荐指数
2
解决办法
5114
查看次数

标签 统计

c++ ×4

boost ×2

boost-asio ×1

c ×1

c-preprocessor ×1

client ×1

g++ ×1

header-files ×1

size ×1

templates ×1