shared_ptr模板参数无效 [英] shared_ptr template argument invalid
问题描述
我试图有一个静态方法返回一个shared_ptr。
I am trying to have a static method return a shared_ptr.
它不是编译和给模板参数1是无效的。
It is not compiling and is giving template argument 1 is invalid.
我不知道为什么会这样。
I can not figure out why this is.
此外,堆栈溢出说我的帖子大多是代码,我应该添加更多的细节。我不知道为什么这是,因为简洁从来没有伤害任何人。
Also, stack overflow says my post is mostly code and that I should add more detail. I don't know why this is, as being concise never hurt anyone. My problem is clear cut and and can be detailed easily.
编译器错误
src/WavFile.cpp:7:24: error: template argument 1 is invalid
std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
WavFile.cpp
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
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
推荐答案
更改命名空间名称到别的东西。现在它与类的名字冲突,因为你使用命名空间WavFile;
。说明错误的简单示例:
Change the namespace name to something else. Now it clashes with the class' name, since you are using namespace WavFile;
. Simple example that illustrates 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() {}
坚持使用命名空间命名为类,然后使用命名空间X; 除去并限定类型
std :: shared_ptr< WafFile: :WavFile>
。
If you insist to have the namespace named as the class, then get rid of the using namespace X;
and qualify the type, std::shared_ptr<WafFile::WavFile>
.
这篇关于shared_ptr模板参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!