C ++在GCC上使用已删除的功能但在MSVC上没有使用

时间:2014-04-16 07:45:45

标签: c++ gcc boost

以下在msvc上编译,而不是在gcc上编译。为什么呢?

game_tracker.cpp:

#include "stdafx.hpp"
#include "game_tracker.hpp"
#include "basic_game_server.hpp"
#include "basic_session.hpp"
#include "api_session.hpp"
#include "protocols.hpp"

using namespace boost::asio;
using namespace std;

namespace games_zone{

    game_tracker::game_tracker(unsigned short port) : tracker_(io_service(thread::hardware_concurrency())),
        api_(io_service(thread::hardware_concurrency())),
        acceptor_(api_, ip::tcp::endpoint(ip::tcp::v4(), port), false),
        socket_(api_)
    {
        tracker_work_ = new boost::asio::io_service::work(tracker_);
        api_work_ = new boost::asio::io_service::work(api_);

        protocols::hl_game_server gs(boost::asio::ip::address_v4::from_string("89.44.246.184"), 27015);
        add(gs);
    }

    ...
}

game_tracker.hpp:

#pragma once

#include "basic_game_server.hpp"

namespace games_zone{
    class game_tracker :
        private boost::noncopyable
    {
    public:
        game_tracker(unsigned short);
        ~game_tracker();
        void run();
    private:
        boost::asio::io_service tracker_;
        boost::asio::io_service api_;
        boost::asio::ip::tcp::acceptor acceptor_;
        boost::asio::ip::tcp::socket socket_;
        boost::asio::io_service::work* tracker_work_;
        boost::asio::io_service::work* api_work_;
        std::vector<basic_game_server*> servers_;

        void add(basic_game_server&);
        void do_accept();
    };
}

gcc错误:

root@ubuntu:~/gz_tracker/build# make
Scanning dependencies of target gz_tracker
[ 16%] Building CXX object games_zone/CMakeFiles/gz_tracker.dir/game_tracker.cpp.o
/root/gz_tracker/games_zone/game_tracker.cpp: In constructor ‘games_zone::game_tracker::game_tracker(short unsigned int)’:
/root/gz_tracker/games_zone/game_tracker.cpp:16:15: error: use of deleted function ‘boost::asio::io_service::io_service(const boost::asio::io_service&)’
   socket_(api_)
               ^
In file included from /usr/local/include/boost/asio/basic_io_object.hpp:19:0,
                 from /usr/local/include/boost/asio/basic_socket.hpp:20,
                 from /usr/local/include/boost/asio/basic_datagram_socket.hpp:20,
                 from /usr/local/include/boost/asio.hpp:21,
                 from /root/gz_tracker/games_zone/stdafx.hpp:10,
                 from /root/gz_tracker/games_zone/game_tracker.cpp:1:
/usr/local/include/boost/asio/io_service.hpp:185:7: note: ‘boost::asio::io_service::io_service(const boost::asio::io_service&)’ is implicitly deleted because the default definition would be ill-formed:
 class io_service
       ^
In file included from /usr/local/include/boost/asio/io_service.hpp:23:0,
                 from /usr/local/include/boost/asio/basic_io_object.hpp:19,
                 from /usr/local/include/boost/asio/basic_socket.hpp:20,
                 from /usr/local/include/boost/asio/basic_datagram_socket.hpp:20,
                 from /usr/local/include/boost/asio.hpp:21,
                 from /root/gz_tracker/games_zone/stdafx.hpp:10,
                 from /root/gz_tracker/games_zone/game_tracker.cpp:1:
/usr/local/include/boost/asio/detail/noncopyable.hpp:32:3: error: ‘boost::asio::detail::noncopyable::noncopyable(const boost::asio::detail::noncopyable&)’ is private
   noncopyable(const noncopyable&);
   ^
In file included from /usr/local/include/boost/asio/basic_io_object.hpp:19:0,
                 from /usr/local/include/boost/asio/basic_socket.hpp:20,
                 from /usr/local/include/boost/asio/basic_datagram_socket.hpp:20,
                 from /usr/local/include/boost/asio.hpp:21,
                 from /root/gz_tracker/games_zone/stdafx.hpp:10,
                 from /root/gz_tracker/games_zone/game_tracker.cpp:1:
/usr/local/include/boost/asio/io_service.hpp:185:7: error: within this context
 class io_service
       ^
/root/gz_tracker/games_zone/game_tracker.cpp:16:15: error: use of deleted function ‘boost::asio::io_service::io_service(const boost::asio::io_service&)’
   socket_(api_)
               ^
make[2]: *** [games_zone/CMakeFiles/gz_tracker.dir/game_tracker.cpp.o] Error 1
make[1]: *** [games_zone/CMakeFiles/gz_tracker.dir/all] Error 2
make: *** [all] Error 2

我使用boost 1.55(当前版本)并在Microsoft Visual Studio 2013上编译和运行。 我从错误中了解到api_无法绑定到explicit basic_stream_socket(boost::asio::io_service & io_service);

我错了什么?这是GCC的错误吗?

2 个答案:

答案 0 :(得分:7)

在这两行中:

tracker_(io_service(thread::hardware_concurrency())),
api_(io_service(thread::hardware_concurrency())),

您正在创建一个临时io_service并将其复制到您的成员变量中(由于复制构造函数是私有的,因此不允许这样做。)

尝试

tracker_(thread::hardware_concurrency()),
api_(thread::hardware_concurrency()),

答案 1 :(得分:-2)

检查您选择的C ++标准是否相等。处于调试模式和释放模式。所选的C ++标准中可能没有实现类型,函数或名称空间,或者存在该类型。

就我而言,我有以下错误(示例):

•尚未声明“ std :: literals”

using namespace std::literals::string_literals;

•std :: random_device {}出现“使用已删除功能”错误

std::random_device device = std::random_device{}  

就我而言

•调试模式-std = c ++ 17
•释放模式-std = c ++ 14(由于C ++ 14 /中缺少功能,类型或名称空间而导致的错误,您需要C ++17。)大量错误。

发布模式更改为C ++ 17。一切正常,0错误0警告。

选择2017 c ++标准不是强制性的,您必须选择已实现项目所需的所有功能的首选项或必要标准之一。

更新 在具有远程Linux编译功能的Clion Windows 2018.3上进行了测试。

使用-std = c ++ 11 Using -std=c++11

使用-std = c ++ 17 Using -std=c++17

C ++是一个不断发展的标准:2003年以后出现了2011年(C ++ 11),然后是2014年(C ++ 14),现在我们有了2017年(C ++ 17),我们正在努力到2020年(C ++) 20)。许多事情正在改变,它们已被弃用,其他功能是新的。如果您想要最新的更新,则应该使用最新的标准,如果您的软件具有不建议使用的代码,则必须使用创建它的标准。