小编Lig*_*ica的帖子

用const对象移动语义

我有这样的代码:

class Pair{
public:
    Pair(Pair && other){};

    Pair(Pair & other){};
};

class IROList{
public:
    virtual const Pair get(const char *key) const = 0;

    inline const Pair operator[](const char *key) const{
        return this->get(key);
        // error: binding ‘const Pair’ to reference of type ‘Pair&&’ discards qualifiers
    }
};
Run Code Online (Sandbox Code Playgroud)

在编译时,它产生

error: binding ‘const Pair’ to reference of type ‘Pair&&’ discards qualifiers
Run Code Online (Sandbox Code Playgroud)

如果我将移动构造函数更改为const,则错误消失.

    Pair(const Pair && other){};
Run Code Online (Sandbox Code Playgroud)

但是,如果移动构造函数采用const,我无法真正移动数据.我应该复制它.

除了删除返回方法的const之外,是否有任何解决方法,例如

    virtual Pair get(const char *key) const = 0;
    inline Pair operator[](const char *key) const;
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

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

将char *提供给std :: string进行管理(最终释放内存)

我必须使用一个库函数,该函数为生成的字符串分配一点内存并返回a char*,期望调用者最终使用释放内存free()

// Example declaration of the library function:
char* foo();
// ...
// Example usage:
auto foo_str = foo();
// ...
free(foo_str);
Run Code Online (Sandbox Code Playgroud)

是否可以std::string从此指针构造一个,将内存的所有权传递给字符串对象,以便在销毁字符串时将其释放?我知道我可以实现这种RAII行为的我自己的包装器,但是我猜想这个轮子已经被发明了一次。

c++ c++11

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

C++错误:"size"声明为返回函数的函数

我不知道为什么我在编译时遇到错误,当size()返回类型size_t时,"error:"size"声明为函数返回函数".任何帮助将不胜感激,谢谢.

// Text.h
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;

namespace w3 {
    class Text {
        string* arrayRecords;
        size_t numRecords;
    public:
        Text();
        Text(const char* fileName);
        size_t size() const;
        ~Text();
    };
}

// Text.cpp

#include "Text.h"

namespace w3 {
    Text::Text() {
        numRecords = 0;
        arrayRecords = nullptr;
    }

    Text::Text(const char* fileName) {
        //
    }

    size_t Text::size() const() {
        return numRecords;
    }

    Text::~Text() {
        if(arrayRecords)
            delete [] arrayRecords;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

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

将字符串数组传递给方法

我是C++的初学者,我之前使用过C但从未使用过C++.这是我的第一个程序之一,它应该做一些非常简单的事情,但是我甚至无法在方法之间传递字符串...当我setMode用字符串数组调用该方法时,方法实例接收一个空数组,并且不是我发送的那个.为什么会这样?

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

#define LED_PATH "sys/class/leds/beaglebone:green:usr"

class LED{

    private:
        string path;
        int number;

    public:
        LED(int number);
        virtual void setMode(string mode[]);
        virtual ~LED();
};


LED::LED(int number){
    ostringstream fs;
    this->number = number;
    fs << LED_PATH << number;
    this->path = string(fs.str());

    cout << this->path << endl;
}

LED::~LED(){

}

void LED::setMode(string mode[]){
    //Will use all fields of mode[] in the future
    cout << "setMode: mode: " << mode[0].c_str() << endl;

}


int …
Run Code Online (Sandbox Code Playgroud)

c++

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

C11 线程编程

我是 C 新手,正在阅读 C11 ,依赖于“thread.h”中的标准 C11 线程函数(如 cnd_init、cnd_destroy、cnd_signal、cnd_broadcast、cnd_wait)是否足够,或者我应该考虑使用其他库来使用线程服务器应用程序。

c multithreading c11

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

c ++在类外声明类成员

我在c ++ primer中有这两个例子,试图在类定义之外声明一个类成员函数.即使我删除了友谊和定义,第一个给我一个错误.第二个工作正常.任何提示?

错误:

src/Screen.h:16:47: error: no ‘void Window_mgr::clear(Window_mgr::ScreenIndex)’ member function declared in class ‘Window_mgr’
Run Code Online (Sandbox Code Playgroud)

EX1:

#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <vector>
class Screen;

class Window_mgr {

public:
    using ScreenIndex = std::vector<Screen>::size_type;
    Window_mgr();
private:
    std::vector<Screen> screens;
};

void Window_mgr::clear(Window_mgr::ScreenIndex);
class Screen {

    //friend void Window_mgr::clear(ScreenIndex);

public:
    using pos = std::string::size_type;
    Screen() = default;
    Screen(pos h, pos w): height(h), width(w), contents(h*w, ' ') { }
    Screen(pos h, pos w, char c): height(h), width(w), contents(h*w, c) { }
    char get() …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

无法创建对象并使用其模板类的方法

我在C++中使用模板几乎是新手.以下是我尝试使用的代码.我无法使用以下代码,因为我无法弄清楚如何为它创建对象并使用其中定义的方法.

 template <typename UInt> class nCr {
  public:
      typedef UInt result_type;
      typedef UInt first_argument_type;
      typedef UInt second_argument_type;

      result_type operator()(first_argument_type n, second_argument_type k) {
          if (n_ != n) {
              n_ = n;
              B_.resize(n);
          } // if n

          return B_[k];
      } // operator()

  private:
      int n_ = -1;
      std::vector<result_type> B_;

  }; 
Run Code Online (Sandbox Code Playgroud)

我是如何创建对象的:

#include <iostream>
#include "math.hpp" // WHere the above class nCr is defined

int main() {
    int n =4;
    nCr x(4,2);

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

为此,我创建错误为

error: use of class template 'jaz::Binom' …
Run Code Online (Sandbox Code Playgroud)

c++

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

通过引用返回函数调用

在此输入图像描述

我正在阅读教科书,然后我遇到上面的函数"Sales_data :: combine()".

如果我做

Total.combine(trans);
Run Code Online (Sandbox Code Playgroud)

其中Total和trans是Sales_data,在此函数调用之后,对象Total会被更改吗?什么是返回*这个点?

c++

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

我可以使用C++ 03标准库功能模拟空lambda吗?

我坚持GCC 4.4,所以尽管介绍-std=c++0x我的代码库没有使用lambdas.

是否有一个C++ 03相当于一个空的lambda [](){},我可以安全地使用它作为某种类型的泛型参数的默认参数Callback

我问,因为旧的std::plus,std::less等了方便,但我似乎无法找到一个空操作,void-returning等同.

c++ c++03

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

如何有效地将唯一对象从一个向量复制到另一个向量(由相同对象的子集组成)?

如何有效地将对象(或一系列对象)从向量A复制到向量B,

其中向量B已包含与向量A相同的某些对象,

所以没有从矢量A中复制的对象已经在向量B中列出了吗?

我有一个图表存储为边缘矢量std::vector<MinTreeEdge>minTreeInput.

我有一个从该图创建的最小生成树,存储在std::vector<MinTreeEdge>minTreeOutput.

我正在尝试添加一个随机添加一定数量的边缘minTreeOutput.为此,我想从minTreeInput后面复制元素,minTreeOutput直到后者包含所需的边数.当然,必须尚未存储复制的每个边缘对象minTreeOutput.此图表中不能有重复的边缘.

以下是我到目前为止所提出的内容.它工作,但它真的很长,我知道循环必须运行多次,具体取决于图形和树.我想知道如何正确地做到这一点:

    // Edge class
    struct MinTreeEdge
    {
        // For std::unique() between objects
        bool operator==(MinTreeEdge const &rhs) const noexcept
        {
            return lhs == rhs.lhs;
        }
        int lhs;

        int node1ID;
        int node2ID;
        int weight;
        ......
    };

             ......

    // The usage
    int currentSize = minTreeOutput.size();
    int targetSize = currentSize + numberOfEdgesToReturn;
    int sizeDistance = targetSize - currentSize;
    while(sizeDistance != 0)
    {
        //Probably really inefficient

        for(std::vector<MinTreeEdge>::iterator …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm vector

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

标签 统计

c++ ×9

c++11 ×3

algorithm ×1

c ×1

c++03 ×1

c11 ×1

move-semantics ×1

multithreading ×1

vector ×1