gpt4 book ai didi

c++ - 在 Visual Studio 2015 中调用可变参数模板函数会出现错误 C2660

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:50 26 4
gpt4 key购买 nike

以下代码在 Visual Studio 2015 Community 中给出 C2660 错误:

class Logger {
public:
template <class T> void writeLine(const T& value) {
if (this->filestream.is_open()) {
filestream << object << std::endl;
}
}

template <typename T> void write(const T& value) {
if (this->filestream.is_open()) {
filestream << value;
}
}

template <typename T, typename... Targs> void write(const T& value, const Targs&... args) {
if (this->filestream.is_open()) {
filestream << value;
this->write(args...);
}
}

// ... singleton stuff
}

我是这样调用函数的:

#define LOG(x) Logger::instance().write(__FILE__, " (line ", __LINE__, "): ", x, std::endl);

这是输出日志中的错误:

encoder.cpp(51): error C2660: 'Logger::write': function does not take 6 arguments

最佳答案

问题是 std::endl 本身就是一个模板

template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );

因此一个可能的解决方案是

var.write(__FILE__, " (line ", __LINE__, "): ", 22, std::endl<char, std::char_traits<char>>);

有关此问题的更多信息 here

关于c++ - 在 Visual Studio 2015 中调用可变参数模板函数会出现错误 C2660,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40675131/

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