i am quite new to c++ and smart pointers, but it is my understanding that i can assign an unique_ptr with std::move(another unique_ptr). when i try to assign a unique_ptr with std::move(..) it throws the following error:
binary '=': no operator found which takes a right-hand operand of type 'std::unique_ptr<T [],std::default_delete<_Ty>>'
the following code block is where the error is thrown, on the second last line.
template<typename T>
void wave::WaveReader::readSamples(std::istream& input, int nr_of_bytes)
{
std::unique_ptr<T[]> a = std::make_unique<T[]>(this->data_chunk.subChunk2Size);
input.read(reinterpret_cast<char*>(this->data.data.get()), this->data_chunk.subChunk2Size);
this->data.length = (this->fmt.bitsPerSample / (nr_of_bytes * 8));
this->data.width = this->data_chunk.subChunk2Size / (this->fmt.bitsPerSample / 8);
this->data.data = std::move(a);
std::cout << sizeof(T) << std::endl;
}
some extra code:
class WaveReader
{
private:
RIFFCHUNK riff;
FMTCHUNK fmt;
DATACHUNK data_chunk;
Array<int8_t> data;
std::variant<Array<int8_t>,Array<uint16_t>,Array<uint32_t>> samples;
public:
void read(std::string filepath);
private:
void readSamples(std::istream& input);
template<typename T>
void readSamples(std::istream& input,int nr_of_bytes);
std::string getDataChunkID(DATACHUNK& data);
void read_RIFFCHUNK(std::istream& input, RIFFCHUNK& riff);
void read_FMTCHUNK(std::istream& input, FMTCHUNK& fmt);
void read_DATACHUNK(std::istream& input, DATACHUNK& data, FMTCHUNK& fmt);
};
the array class with the unique_ptr i am trying to assign to
template <typename T>
struct Array
{
std::unique_ptr<T[]> data;
uint32_t length;
uint32_t width;
Array(int width, int length) : width(width), length(length)
{
data = std::make_unique<T[]>(width);
}
Array() : width(0), length(0)
{
data = std::make_unique<T[]>(0);
}
T operator[](const Position& pos) { return data[(pos.x * length) + pos.y]; }
};
extra code: this code reads the file
void wave::WaveReader::read(std::string filename)
{
std::ifstream file(filename, std::ios::binary);
//read riff chunk
read_RIFFCHUNK(file, this->riff);
//read fmt chunk
read_FMTCHUNK(file, this->fmt);
//read data chunk, fills data object
read_DATACHUNK(file, this->data_chunk, fmt);
this->readSamples(file);
}
i use this code to determine what type the samples get stored in
void wave::WaveReader::readSamples(std::istream& input)
{
//get Type
switch (fmt.bitsPerSample)
{
case 8:
readSamples<uint8_t>(input,1);
break;
case 16:
readSamples<int16_t>(input, 2);
break;
case 24:
readSamples<int32_t>(input, 3);
break;
case 32:
readSamples<int32_t>(input, 4);
break;
default:
abort(); // need to change this probably
}
}
when i change std::unique_ptr in the array struct to a normal int it does work correctly. when i change it to a T it doesn't work.
T
insidereadSamples
? My guess is that it's notint8_t
in your code.unique_ptr<int8_t>
is not compatible withunique_ptr<int16_t>
, for example. – alter igel