Invalid shared_ptr template argument

I am trying to return the static method shared_ptr.

It does not compile and sets the argument to template 1 to be invalid.

I can’t understand why this is so.

Also, the stack overflow says that my post is mostly code, and I have to add more details. I do not know why this is so, since brevity will never hurt anyone. My problem is clear and can be easily described.

Compiler error

src/WavFile.cpp:7:24: error: template argument 1 is invalid
 std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)

Wavfile.cpp

#include "WavFile.h"
#include "LogStream.h"
#include "assert.h"

using namespace WavFile;

std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
{
    ifstream infile;        
    infile.open( filname, ios::binary | ios::in );  
}

Wavfile.h

#pragma once

#ifndef __WAVFILE_H_
#define __WAVFILE_H_

#include <fstream>
#include <vector>
#include <memory>

namespace WavFile
{
    class WavFile;
}

class WavFile::WavFile
{

    public: 

        typedef std::vector<unsigned char> PCMData8_t;
        typedef std::vector<unsigned short int> PCMData16_t;

        struct WavFileHeader {


            unsigned int num_channels;
            unsigned int sample_rate;
            unsigned int bits_per_sample;
        };

        static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename);

    private:
        WavFile(void);  

    private:                
        WavFileHeader m_header;
        PCMData16_t m_data16;
        PCMData8_t m_data8;
};

#endif
+1
source share
1 answer

Change the namespace name to another. Now he is faced with the class name, as you are using namespace WavFile;. A simple example illustrating the error:

#include <iostream>
#include <memory>

namespace X // changing this to Y makes the code compilable
{
    class X{};
}

using namespace X;     // now both class X and namespace X are visible
std::shared_ptr<X> v() // the compiler is confused here, which X are you referring to?
{
    return {};
}

int main() {}

, , using namespace X; , std::shared_ptr<WafFile::WavFile>.

+3

Source: https://habr.com/ru/post/1612993/


All Articles