gpt4 book ai didi

c++ - shared_ptr 模板参数无效

转载 作者:行者123 更新时间:2023-11-30 02:35:55 25 4
gpt4 key购买 nike

我试图让一个静态方法返回一个 shared_ptr。

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

我想不通这是为什么。

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

编译错误

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

音频文件.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 );
}

音频文件.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

最佳答案

将命名空间名称更改为其他名称。现在它与类名冲突,因为你是 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> .

关于c++ - shared_ptr 模板参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com