小编yao*_*dav的帖子

c ++ Clion和VS2019中的纯虚拟析构函数

我正在尝试声明纯虚拟析构函数,在VS2019中我这样写:

    virtual ~A() = 0 {};
Run Code Online (Sandbox Code Playgroud)

很好,但是在Clion中,我接受了以下消息:

函数定义虚拟的纯说明符〜A()= 0 {};

并且迫使我为该函数编写不同的实现(不是给它带来很多麻烦,而是我想知道为什么会这样)

c++ virtual-functions pure-virtual visual-c++ clion

3
推荐指数
1
解决办法
62
查看次数

如何将 stl 队列推送函数绑定到 std::function?

我试过这个

{
    std::function<void(int)> push;
    queue<int> myqueue; 
    push = std::bind(static_cast<void (queue<int>::*)(int)>(&queue<int>::push), &myqueue,std::placeholders::_1);
    push(8);
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用

c++ queue bind std-function

3
推荐指数
1
解决办法
109
查看次数

组合两个 grep 命令来处理文件或 grep 行中的输入,该行以一个特定子字符串开头,并在其后包含另一个子字符串

我有一个文件,我想使用 grep 命令解析,该文件由如下行组成:

NVO,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
DVD,0,267,61,247357,247357,O,19:00:00.000000,06:09:08.417320,07:55:22.068670
NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
DVD,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
Run Code Online (Sandbox Code Playgroud)

我只想获取以 NVO 开头且有 ,B, 的行。输出应该是这样的:

NVO,0,267,61,247358,247358,B,19:00:00.000000,06:09:08.417407,07:55:22.079291
Run Code Online (Sandbox Code Playgroud)

获取NVO,我正在使用的线路grep ^NVO, file_name.txt ,但我无法添加,B, 我尝试过的grep '^NVO,|,B,' file_name.txt第二个条件

grep ",B," | grep "^NVO," file_name.txt 
Run Code Online (Sandbox Code Playgroud)

不幸的是,我知道我可以用两个命令来做到这一点,第一个命令写入文件,然后使用过滤器执行第二个 grep,B,命令

bash grep

2
推荐指数
1
解决办法
1099
查看次数

将向量复制到队列中

我有矢量

 std::vector<OrderInfo *> vec
Run Code Online (Sandbox Code Playgroud)

和一个队列

queue<OrderInfo *> *myQueue = new queue<OrderInfo *>;
Run Code Online (Sandbox Code Playgroud)

我想将向量复制到队列中。我尝试使用如何将整个向量复制到队列中?这个答案以及这个使用 std::copy 插入到 STL 队列中

但它不起作用,我该如何使它起作用?

这是我试过的: myQueue = new queue(vec.begin(), vec.end()); 我有

错误:没有匹配的函数调用'std::queue::queue(std::vector::iterator, std::vector::iterator)' myQueue = new queue(vec.begin(), vec.end() );

当我尝试这个时:

std::copy(vec.begin(),vec.end(),std::back_inserter(myQueue));
Run Code Online (Sandbox Code Playgroud)

我有:

来自 'BacStrategy::BacStrategy(EZXConnectionHandler&, const string&, bool, const double&, int) [with Event_Type = EZXOrderEventHandler; std::__cxx11::string = std::__cxx11::basic_string]' /home/yaodav/Desktop/git_repo/test/main.cpp:324:51:从这里 /usr/local/include/c++/7.4 需要。 0/bits/stl_iterator.h:490:7: 错误:'std::queue*' 不是类、结构或联合类型 operator=(const typename _Container::value_type& __value)

c++ stl

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

为什么擦除 std::vector 提升迭代器

当我在此处输入链接描述中运行以下代码 并且当擦除是预成型时迭代器被提升(迭代器前进到下一个元素)而在我的真实代码中它不是(我知道它不应该提升我的迭代器,当我使用擦除时,迭代器变得无效)但为什么在模拟器上会发生?

*编辑这是代码:

// Example program
#include <iostream>
#include <string>
#include <vector>

struct A {
    A(int _a):a{_a}{}
int a;
};


 struct B {
     B()
     {
          vec.push_back(new A(1));
          vec.push_back(new A(2));
     }
      std::vector<A*> vec;
     void  clean()
     {
        for(auto e = vec.begin(); e != vec.end(); )
        {
            auto it = *e;
            std::cout << it->a << std::endl;
            vec.erase(e);
        }
        std::cout << vec.size() << std::endl;
     }
 };



int main()
{
   B b;
   b.clean();
   return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++

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

如何创建监听两个不同端口的 boost::asio 服务器

我正在使用 boost 1.75.0,我正在尝试更新我的服务器,以便他可以同时监听两个不同的端口,比如说IP 127.0.0.1 port 6500 and port 6600. 我需要保留Server两个套接字吗?这是我的服务器

#include <boost/asio/io_service.hpp>
#include <boost/asio.hpp>
#include <queue>
#include "Session.h"
using boost::asio::ip::tcp;

class Server
{
 public:

  Server(boost::asio::io_service &io_service, short port)
      : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
        socket_(io_service)
  {
    do_accept();
  }

 private:
  void do_accept(){
    acceptor_.async_accept(socket_,
                           [this](boost::system::error_code ec) {
                             if (!ec) {
                               std::cout << "accept connection\n";

                               std::make_shared<Session>(std::move(socket_))->start();
                             }

                             do_accept();
                           });
  }


  tcp::acceptor acceptor_;
  tcp::socket socket_;

};
Run Code Online (Sandbox Code Playgroud)

这是我的课程

#include <boost/asio/io_service.hpp>
#include <boost/asio.hpp>
#include "message.h"
#include <fstream>
#include <boost/bind/bind.hpp>

namespace {
    using boost::asio::ip::tcp;
    auto constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-asio

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

多线程程序中的 printf 函数

我正在用 c 编写一个简单的程序,该程序有两个线程和两个全局变量,但 printf 函数不起作用,我知道这printf不是线程安全的,而且如果我\n在 print 中添加它会起作用,但我想要了解为什么没有它就无法工作?我正在添加代码,

#include <pthread.h>
#include <stdio.h>

int i;
int j;

void* runi(void* _temp)
{
    while(1)
    {
        i++;
        if(i==1000)
        {
            printf("i: %d",i);
        }
    }
    return NULL;
}

void* runj(void* _temp)
{
    while(1)
    {
        j++;
        if(j==1000)
        {
            printf("j: %d",j);
        }
    }
    return NULL;
}
main ()
{
    pthread_t threadI,threadJ;
    pthread_create(&threadI,NULL,runi,NULL);
    pthread_create(&threadJ,NULL,runj,NULL);
    pthread_join(threadI,NULL);
    pthread_join(threadJ,NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c multithreading

-1
推荐指数
1
解决办法
3016
查看次数

类模板上一个特定函数的未解析外部符号

正如标题所述,我遇到了臭名昭著的unresolved external symbol链接器问题。

我已经移动了.h文件中的所有内容。模板类不再在.h.cpp文件之间拆分。

我已将它们包含在主文件中。我已经三重四重检查了定义是否存在(显然)。

我的文件如下:

Utils.h

只是一个漂亮的 coutvectorvalarray。测试和工作。

#pragma once

#include <vector>
#include <valarray>
#include <ostream>

using std::vector;
using std::valarray;
using std::ostream;


template <typename ValueType>
ostream& operator<< (ostream&, vector<ValueType>);

template <typename ValueType>
ostream& operator<< (ostream&, valarray<ValueType>);


template <typename ValueType>
ostream& operator<< (ostream& out, vector<ValueType> v)
{
    out << "Vec[";
    for (int i = 0; i < v.size() - 1; i++)
        out << v[i] << ", "; …
Run Code Online (Sandbox Code Playgroud)

c++ linker templates visual-c++ c++11

-3
推荐指数
1
解决办法
133
查看次数