为什么在一行中使用这个C ++函数两次会导致编译错误?

编程入门 行业动态 更新时间:2024-10-19 14:36:58
本文介绍了为什么在一行中使用这个C ++函数两次会导致编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一些麻烦,试图在Visual C ++ 2010中实现一个智能平等测试宏类型模板函数,它与一个错误。我修复它通过将参数的值包装在一个额外的函数,但现在我发现我不能在一行中使用该函数两次!

I ran into some trouble trying to implement a smart equality test macro-type template function in Visual C++ 2010 that had to do with a bug in VS in regard to default arguments of template functions. I fixed it by wrapping the value of the parameter in an extra function, but now I found that I can't use the function twice in one line!

头文件:

// example.h #pragma once #include <limits> namespace myspace { // Need to define this separately to avoid a Visual Studio bug template<typename T> T epsilon() { return std::numeric_limits<T>::epsilon(); } // A generic equality test template<typename T> inline bool smartEqual( const T &v1, const T &v2, const T &eps = epsilon<T>()) { return (v1 == v2); } // Template specialization for floating-point numbers template<> bool smartEqual<float>( const float &v1, const float &v2, const float &eps); } // namespace myspace

源文件:

// example.cpp #include "example.h" using namespace std; using namespace myspace; // equal-macro specialization for floats using epsilon template<> bool myspace::smartEqual<float>( const float &v1, const float &v2, const float &eps) { return (fabs(v1 - v2) < eps); } int _tmain(int argc, _TCHAR* argv[]) { float a,b; bool x = smartEqual(a,b); // works ok bool x = smartEqual(a,b) && smartEqual(b,a); // error return 0; }

错误报告如下:

------ Build started:Project:test,Configuration:Debug Win32 ------ test.cpp c:\users\\\inja\documents\visual studio 2010\projects\test\test\test.cpp(24):错误C2440:'default argument':无法从'const float *'to'const float&' 原因:无法从'const float *'转换为'const float' 没有可进行此转换的上下文 p>

------ Build started: Project: test, Configuration: Debug Win32 ------ test.cpp c:\users\ninja\documents\visual studio 2010\projects\test\test\test.cpp(24): error C2440: 'default argument' : cannot convert from 'const float *' to 'const float &' Reason: cannot convert from 'const float *' to 'const float' There is no context in which this conversion is possible

冒犯行是我尝试使用逻辑AND调用smartEqual()两次。

The offending line is the one where I try to call smartEqual() twice using the logical AND.

我不明白为什么会发生这种情况。将eps从引用类型更改为直接值类型会修复它,但我希望我知道发生了什么。

I don't understand why this happens. Changing "eps" from a reference type to a straightforward value type fixes it, but I wish I knew what was going on.

谢谢!

推荐答案

我想你现在已经点击此VS10错误。

您的代码在VS11测试版上编译成功。

Your code compiles OK on VS11 Beta.

您可以通过更改 smartEqual 至:

template<typename T> inline bool smartEqual( const T &v1, const T &v2) { return (v1 == v2); }

and simply specialising for float (and double) like this:

template<> bool myspace::smartEqual<float>( const float &v1, const float &v2) { return (fabs(v1 - v2) < std::numeric_limits<float>::epsilon()); }

另一个选项是更改epsilon参数传递值:

Another option is to change the epsilon parameter to pass by value:

template<typename T> inline bool smartEqual( const T &v1, const T &v2, T eps = epsilon<T>()) { return (v1 == v2); }

更多推荐

为什么在一行中使用这个C ++函数两次会导致编译错误?

本文发布于:2023-11-09 21:00:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   使用这个   函数   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!