shared_ptr模板参数无效

时间:2015-10-24 01:54:08

标签: c++ shared-ptr

我正在尝试使用静态方法返回shared_ptr。

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

我无法弄清楚为什么会这样。

此外,堆栈溢出说我的帖子主要是代码,我应该添加更多细节。我不知道为什么会这样,因为简洁从不伤害任何人。我的问题很明确,而且很容易详细说明。

编译器错误

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 个答案:

答案 0 :(得分:3)

将命名空间名称更改为其他名称。现在它与班级冲突了#39;姓名,因为你是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() {}

如果您坚持要将命名空间命名为该类,那么请删除using namespace X;并限定类型std::shared_ptr<WafFile::WavFile>