小编Tri*_*lJM的帖子

c ++:noskipws延迟某些数据类型的文件结束检测

据我所知noskipws,它禁止跳过白色空格.因此,如果他们想要使用,他们需要在他们的程序中使用一些char来获取空格noskipws.我尝试按+ (+ 对于Windows)设置cineof条件.但是如果我使用或输入,则使用单个输入将流设置为文件结尾.但是,如果我使用其他一些数据类型,则需要我按两次组合.如果我删除请求,其他一切正常.以下代码更准确地解释了问题:CtrlDCtrlZcharstringnoskipws

#include <iostream> 

using namespace std;

int main()
{
    cin >> noskipws; //noskipws request
    int number; //If this int is replaced with char then it works fine
    while (!cin.bad()) {
        cout << "Enter ctrl + D (ctrl + Z for windows) to set cin stream to end of file " << endl;
        cin >> number;
        if (cin.eof()) {
            break; // Reached end of …
Run Code Online (Sandbox Code Playgroud)

c++

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

抽象类和唯一指针

我的代码中出现以下错误:

error: allocating an object of abstract class type 'Material'

我不知道如何处理这个案子.

我知道std::make_unique执行分配,所以它不能分配类型的对象Material,但我不知道如何纠正它.

#include <iostream>
#include <memory>

struct Material
{
  Material() = default;
  virtual int get_color() const = 0;
};

struct Basic : public Material
{
  Basic() = default;
  virtual int get_color() const override
  {
    return 1;
  }
};

struct Mix : public Material
{
  Mix(const Material& mat1, const Material& mat2)
    : mat1_(std::make_unique<Material>(mat1))
    , mat2_(std::make_unique<Material>(mat2))
  {}

  virtual int get_color() const override
  {
    return mat1_->get_color() + mat2_->get_color();
  } …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor abstract-class c++14

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

覆盖c ++流

在C++程序中,我们有3个数据流:stdin,stdout,和stderr.我可以在控制台应用程序中覆盖它们并在使用表单的应用程序中使用它们吗?

例如,如果在某些基类中,我cout<< "..."可以"重定向"到可视化的东西(如Windows窗体)吗?

c++ overriding

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

C++强制卸载共享库

我正在尝试创建一个多次重新加载共享库的应用程序.但在某个时间点,dlmopen失败并出现错误

/usr/lib/libc.so.6: cannot allocate memory in static TLS block

以下是重现此问题的最小代码:

#include <dlfcn.h>
#include <cstdio>
#include <vector>

int main() {
  for (int i = 0; i < 100; ++i) {
    void *lib_so = dlmopen(LM_ID_NEWLM, "lib.so", RTLD_LAZY | RTLD_LOCAL);
    if (lib_so == NULL) {
      printf("Iteration %i loading failed: %s\n", i, dlerror());
      return 1;
    }
    dlclose(lib_so);
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

并清空lib.cpp,用.编译

g++ -rdynamic -ldl -Wl,-R . -o test main.cpp
g++ -fPIC -shared lib.cpp -o lib.so
Run Code Online (Sandbox Code Playgroud)

更新

它似乎即使用一个线程也会崩溃.问题是:如何强制库卸载或销毁使用的未使用的命名空间LM_ID_NEWLM

c++ linux shared-libraries

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

为什么Visual Studio编译器允许在此示例中违反私有继承?

std::unique_ptr在Visual Studio 2013和2017中发现了非常奇怪的行为.让我们考虑一个例子:

class Base 
{
public:
    virtual ~Base() = default;
    virtual void Foo() = 0;
};

class Derived : private Base 
{
public:
    void Foo() override
    {
        std::cout << "Foo";
    }
};

void Foo(std::unique_ptr<Base> a)
{
    a->Foo();
}

Foo(std::unique_ptr<Base>(new Derived())); // Compiles
Run Code Online (Sandbox Code Playgroud)

请注意,继承是私有的.此示例仅在Visual Studio上编译.而且,虚函数调用有效,因为它是公共继承.因此,我们有封装侵犯,因为从演员DerivedBase应该无法访问.任何人都可以解释为什么Visual Studio允许这样做?这是一个已知的问题吗?

由于合理的原因,下面的行无法编译.第一个和第二个用法之间的唯一区别在于第二个,B创建了命名对象.

std::unique_ptr<Base> B(new Derived()); // Doesn't compile
Run Code Online (Sandbox Code Playgroud)

是否与此问题有关,仍然没有修复?

c++ encapsulation unique-ptr visual-studio private-inheritance

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

为什么不总是使用std :: forward?

std::move和之间的区别std::forward是众所周知的,我们使用后者来保留转发对象的值类别,并使用前者转换为右值引用以启用移动语义.

有效的现代C++中,存在一种指导原则

std::move在rvalue引用std::forward上使用,在通用引用上.

但在以下场景中(以及我们不想更改值类别的场景),

template <class T>
void f(vector<T>&& a)
{
    some_func(std::move(a)); 
}
Run Code Online (Sandbox Code Playgroud)

哪里a不是转发引用而是简单的右值引用,执行以下操作不是完全相同的吗?

template <class T>
void f(vector<T>&& a)
{
    some_func(std::forward<decltype(a)>(a)); 
}
Run Code Online (Sandbox Code Playgroud)

因为这可以很容易地封装在像这样的宏中,

#define FWD(arg) std::forward<decltype(arg)>(arg)
Run Code Online (Sandbox Code Playgroud)

如此总是使用这个宏定义不方便吗?

void f(vector<T>&& a)
{
    some_func(FWD(a)); 
}
Run Code Online (Sandbox Code Playgroud)

写这两种方式不完全相同吗?

c++ c++11

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

无法使用 SSH 推送到 Github

我正在尝试推送到我使用 SSH 克隆的远程 git 存储库,但无法推送到它。我收到以下错误:

致命:远程端意外挂起

我做了一些搜索,发现大多数人都必须这样做git config ssh.postBuffer 524288000,但这对我不起作用;我仍然遇到同样的错误。

我正在 Levinux 上运行本地存储库(因为我的大学课程需要它)。我已生成 SSH 密钥并将其添加到 GitHub。使用 https 进行连接是可行的,但我宁愿使用 SSH。任何建议都会很棒!

git ssh github

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

提升1.49.0编译问题

我尝试用VS2008编译这个与boost相关的类(错误行是/**/):

#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/named_mutex.hpp> 
#include <boost/interprocess/sync/named_condition.hpp>

struct SafeShardQueue
{
    typedef boost::interprocess::scoped_lock<boost::interprocess::named_mutex> scoped_mutex;

    SafeShardQueue()//:
    {
    }

    ~SafeShardQueue()
    {
        remove();
    }

    void wait_msg()
    {
        scoped_mutex locker(*mu);
        data_avail_condition->wait(locker);
    }

    void wait_space()
    {
        scoped_mutex locker(*mu);
        queue_empty_condition->wait(locker);
    }

    boost::shared_ptr<boost::interprocess::message_queue> msgs;
    boost::shared_ptr<boost::interprocess::named_condition> data_avail_condition;
    boost::shared_ptr<boost::interprocess::named_mutex> mu;
    boost::shared_ptr<boost::interprocess::named_condition> queue_empty_condition;

    static SafeShardQueue* open()
    {
        SafeShardQueue* tmp = new SafeShardQueue();
        boost::interprocess::open_only_t open_create_type;
        /***/tmp->msgs.reset(new boost::interprocess::message_queue  (open_create_type, "message_queue"));**
        tmp->mu.reset(new boost::interprocess::named_mutex(open_create_type, "shared_queue_mutex"));
        tmp->data_avail_condition .reset(new boost::interprocess::named_condition(open_create_type, "data_avail_condition"));
        tmp->queue_empty_condition .reset(new boost::interprocess::named_condition(open_create_type, "queue_empty_condition"));
        return tmp;
    }

    static SafeShardQueue* create()
    {
        remove();
        SafeShardQueue* tmp …
Run Code Online (Sandbox Code Playgroud)

c++ boost message-queue

6
推荐指数
0
解决办法
430
查看次数

将 std::move 与向量一起使用

我有一个关于std::move在 C++ 中使用的问题。

假设我有以下类,它在其构造函数中采用 astd::vector作为参数:

class A
{
public:
    A(std::vector<char> v): v(v) {}
private:
    std::vector<char> v;
};
Run Code Online (Sandbox Code Playgroud)

但如果我在某处写下以下内容:

std::vector<char> v;
A a(v);
Run Code Online (Sandbox Code Playgroud)

的复制构造函数std::vector将被调用两次,对吧?那么我应该编写A如下所示的构造函数吗?

class A
{
public:
    A(std::vector<char> v): v(std::move(v)) {}
private:
    std::vector<char> v;
};
Run Code Online (Sandbox Code Playgroud)

如果我想调用以下内容怎么办?

std::vector<char> v;
A a(std::move(v));
Run Code Online (Sandbox Code Playgroud)

构造函数的第二个版本可以吗?或者我应该为此创建另一个构造A函数std::vector<char>&&

c++ move-semantics c++11

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

找不到“ui_mainwindow.h”文件

我刚刚在 Windows 10 PC 上安装了 Qt。我创建一个新项目,并在 mainwindow.cpp 文件中写入一些行:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
  ui->setupUi(this);
}

MainWindow::~MainWindow() {
  delete ui;
}
Run Code Online (Sandbox Code Playgroud)

但 Qt 环境在某些行上给了我错误:

mainwindow.cpp: 2: 10: error: 'ui_mainwindow.h' file not found
mainwindow.cpp: 4:24: error: unknown type name 'QWidget'
mainwindow.cpp: 6: 12: error: allocation of incomplete type 'Ui :: MainWindow'
mainwindow.h: 7: 7: note: forward declaration of 'Ui :: MainWindow'
mainwindow.cpp: 8: 7: error: member access into incomplete type 'Ui :: MainWindow'
mainwindow.h: 7: 7: note: …
Run Code Online (Sandbox Code Playgroud)

c++ qt qt-creator

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