gpt4 book ai didi

c++ - 如何在 C++ 中修复 "no matching function to call to..."?

转载 作者:行者123 更新时间:2023-11-28 01:21:10 24 4
gpt4 key购买 nike

嘿,我想将我的 wav 文件的二进制数据加载到我的数组中,但我收到类似这样的错误:“不匹配调用 'std::basic_ifstream(Array&, size_t&)' 的函数”,我想这样做使用模板,有人知道如何解决这个问题吗?

int main(){


ifstream infile("soundeffect.wav", std::ios::binary);

infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(44, infile.beg);

Array<int> array(length);


infile.read(array, length); //error in this line

return 0;
}

最佳答案

I get an error like: "not matching function for call to 'std::basic_ifstream(Array&, size_t&)'"

Array is a class I wrote and yes I put 'using namepace std;'

如果我很了解你有类似的东西:

#include <fstream>

using namespace std;

template<typename K>
class Array {
public:
Array(size_t ln) { /*...*/ }
// ...
};

int main(){
ifstream infile("soundeffect.wav", std::ios::binary);

infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(44, infile.beg);

Array<int> array(length);

infile.read(array, length); //error in this line

return 0;
}

当我编译时我也有

c.cc: In function ‘int main()’: c.cc:21:28: error: no matching function for call to ‘std::basic_ifstream::read(Array&, size_t&)’ infile.read(array, length); //error in this line

这是正常的,因为没有理由 ifstream::read 知道如何读取数组

一种方法是在 Array 上定义操作 read ,例如

#include <fstream>

using namespace std;

template<typename K>
class Array {
public:
Array(size_t ln) : len(ln) { /* ... */ }
istream & read(istream & in, size_t & len);
// ...
private:
size_t len;
// ...
};

// ln is an input-output var allowing to know how much elements
// was read, returns the istream to know if an error occurs
// and which one
template<typename K>
istream & Array<K>::read(istream & in, size_t & ln) {
size_t n = min(ln, len);

for (ln = 0; ln != n; ++ln)
if (! in.read(??, ??))
break;

return in;
}

int main(){
ifstream infile("soundeffect.wav", std::ios::binary);

infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(44, infile.beg);

Array<int> array(length);

array.read(infile, length);

return 0;
}

但是你确定你可以定义一个通用类 Array 允许将 wav 文件读入 Array<int>

关于c++ - 如何在 C++ 中修复 "no matching function to call to..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223865/

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