gpt4 book ai didi

c++ - vector> v 出错

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:21 25 4
gpt4 key购买 nike

我是 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 I我做错了。非常感谢您的帮助。

最佳答案

std::unique_ptr提供唯一的所有权(因此是名称),这意味着您不能复制 std::unique_ptr 的实例。 - 这意味着共享所有权。当你路过std::vector<std::unique_ptr<whatever>>通过值(value),你创建了一个 vector 实例的拷贝,它试图复制每个元素。最简单的解决方案是传递 std::vector通过 const 引用实例(因为您无意修改 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;
}

关于c++ - vector<unique_ptr<X>> v 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com