shared_ptr 模板参数无效

shared_ptr template argument invalid

我正在尝试使用静态方法 return a shared_ptr。

它没有编译并且给出的模板参数 1 无效。

我不明白这是为什么。

此外,堆栈溢出说我的 post 大部分是代码,我应该添加更多细节。我不知道这是为什么,因为简洁不会伤害任何人。我的问题很明确,很容易详细说明。

编译器错误

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

将命名空间名称更改为其他名称。现在它与 class' 的名字冲突,因为你是 using namespace WavFile;。说明错误的简单示例:

#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() {}

如果您坚持将命名空间命名为 class,则去掉 using namespace X; 并限定类型 std::shared_ptr<WafFile::WavFile>