【问题标题】:getting error on vector<unique_ptr<X>> v在 vector<unique_ptr<X>> v 上出现错误
【发布时间】:2016-01-07 18:38:49
【问题描述】:

我对 C++ 很陌生,目前正在学习理解智能指针。因此,我目前正在开发一个用于插入、搜索和删除歌曲的小控制台程序……用于学习以适应这些东西 = )

这是我的代码:

歌曲.hpp

#pragma once
#include <vector>
#include <memory>
#include <string>

class Song
{

public:
    typedef std::unique_ptr<Song> pSong;

public:
    Song();
    ~Song();
    void setTitle(std::string title);
    void setArtist(std::string artist);
    void checkSong(std::string item, int iterator);
    void get();

private:
    std::string _title;
    std::string _artist;
};

歌曲.cpp

#include "Song.hpp"
#include <iostream>



Song::Song()
{
}


Song::~Song()
{
}

void Song::setTitle(std::string title)
{
    _title = title;
}

void Song::setArtist(std::string artist)
{
    _artist = artist;
}

void Song::checkSong(std::string item, int iterator)
{
    if (_artist == item || _title == item)
    {
        std::cout << "Item found on Slot: " << iterator << std::endl;
    }
    else 
    {
        std::cout << "No item found!" << std::endl;
    }
}

void Song::get()
{
    std::cout << _artist << " - " << _title << std::endl;
}

Main.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include "Song.hpp"

//prototype
void IntVector();
void SongVector();
Song* setSong(std::string title, std::string artist);
void find(std::string item, std::vector<Song::pSong> v);


std::vector<Song::pSong> SongList;

int main()
{
    int k;
    SongVector();
    std::cin >> k;
    return 0;
}

void IntVector()
{
    // Create Vector
    std::vector<std::unique_ptr<int>> v;

    // Create a few unique_ptr<int> instances and fill them with ints
    v.push_back(std::unique_ptr<int>(new int(30)));
    v.push_back(std::unique_ptr<int>(new int(600)));
    v.push_back(std::unique_ptr<int>(new int(200)));
    v.push_back(std::unique_ptr<int>(new int(20)));
    v.push_back(std::unique_ptr<int>(new int(200)));
    v.push_back(std::unique_ptr<int>(new int(160)));
    v.push_back(std::unique_ptr<int>(new int(4)));
    v.push_back(std::unique_ptr<int>(new int(5)));
    v.push_back(std::unique_ptr<int>(new int(315)));


    // define vector<int> for storing values of the unique_ptr
    std::vector<int> intList;

    for (int i = 0; i < v.size(); i++)
    {
        // get memory-adress of each element
        auto result = v[i].get();
        // store value of result-pointer in Vector
        intList.push_back(*result);
        std::cout << *result << std::endl;
    }

    // Sort int of new Vector
    std::sort(intList.begin(), intList.end());

    // Loop through intList and cout
    for (int i = 0; i < intList.size(); i++)
    {
        std::cout << intList[i] << std::endl;
    }

}

void SongVector()
{

    Song* first = setSong("Afroki","Steve Aoki");
    Song* secound = setSong("Hype", "Steve Aoki");
    Song* third = setSong("Madness", "Steve Aoki");
    Song* fourth = setSong("Cake Face", "Steve Aoki");
    SongList.push_back(Song::pSong(first));
    SongList.push_back(Song::pSong(secound));
    SongList.push_back(Song::pSong(third));
    SongList.push_back(Song::pSong(fourth));

    for (const auto& song : SongList)
    {
        song->get();
    }

    find("Madness", SongList);
}

Song* setSong(std::string title, std::string artist)
{
    Song* song = nullptr;
    song = new Song;
    song->setArtist(artist);
    song->setTitle(title);
    return song;
}


void find(std::string item, std::vector<Song::pSong> v)
{

    int i = 0;
    for (const auto& song : v)
    {
        song->checkSong(item,i);
        i++;
    }
}

我收到以下错误:

std::unique_ptr<Song,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
1>          with
1>          [
1>              _Ty=Song
1>          ]

我发现,这个错误只发生在调用我的 find(...) 方法时,所以我猜测某处是我的错误,但我可以t find out, what Ive 做错了。非常感谢您的帮助。

【问题讨论】:

  • 我认为,如果您将程序简化为仍然存在问题的最少代码行数,您会得到更多答案。这也可能使您意识到问题所在。
  • 具体函数为unique_ptr的拷贝构造函数。该函数被删除以确保指针 唯一的。所以你不能复制指针,或者包含此类指针的向量。
  • Shachar Shemesh Sry,但认为最好发布整个内容,而不是小块。将在未来的问题中削减我的代码......谢谢你的建议,还有你的 Bo Persson......当重新思考时,它完全有道理=)

标签: c++ c++11 stdvector unique-ptr


【解决方案1】:

std::unique_ptr 提供唯一所有权(即名称),这意味着除了其他您不能复制 std::unique_ptr 的实例 - 这意味着共享所有权。当您通过值传递std::vector&lt;std::unique_ptr&lt;whatever&gt;&gt; 时,您创建了一个向量实例的副本,它试图复制每个元素。所以最简单的解决方案是通过 const 引用传递 std::vector 实例(因为您无意修改向量):

void find( const std::string &item, const std::vector<Song::pSong>& v);

除了解决通过 (const) 引用传递的问题之外,对于非平凡对象更有效,因此您也可以将其用于std::string

在您的 intVector() 函数中:

for (int i = 0; i < v.size(); i++)
    {
        // get memory-adress of each element
        auto result = v[i].get();
        // store value of result-pointer in Vector
        intList.push_back(*result);           
        std::cout << *result << std::endl;
     }

您并不需要获取原始指针,只需使用 std::unique_ptr 本身即可:

for (int i = 0; i < v.size(); i++)
{
    // get smart pointer for each element
    const auto &result = v[i];
    // store value of result-pointer in Vector
    intList.push_back(*result);
    std::cout << *result << std::endl;
}

【讨论】:

    【解决方案2】:
    void find(std::string item, std::vector<Song::pSong> v)
    

    您需要通过引用传递向量。添加 &。

    void find(std::string item, std::vector<Song::pSong>& v)
    

    别忘了也改变函数的原型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-11
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2017-06-12
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多