为什么我得到一个“非法令牌”编译时错误与这片C ++代码? [英] Why do I get an "illegal token" compile-time error with this piece of C++ code?

查看:492
本文介绍了为什么我得到一个“非法令牌”编译时错误与这片C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(根据Visual C ++ 2010编译),我有一个头像文件中的代码:

In my application (compiled under Visual C++ 2010), I have code like this in a header file:

// example.h
#pragma once

#include <limits>

namespace myspace
{

// A generic equality test
template<typename T> inline bool equal(
    const T &v1, 
    const T &v2, 
    const T &eps = std::numeric_limits<T>::epsilon())
{
    return (v1 == v2);
}

// Template specialization for floating-point numbers
template<> bool equal<float>(
    const float &v1, 
    const float &v2, 
    const float &eps);

// A generic equality test for finite-precision real number representations (with epsilon)
template<typename T> inline bool realEqual(
    const T &p, 
    const T &q, 
    const T &eps = std::numeric_limits<T>::epsilon())
{
    return (fabs(p - q) < eps);
}

} // namespace myspace

...和一些.cpp文件中的代码:

...and some code in a .cpp file:

// example.cpp
#include "example.h"

using namespace std;
using namespace myspace;

// equal-macro specialization that calls the appropriate equality test function for real numbers
template<> bool myspace::equal<float>(
    const float &v1, 
    const float &v2, 
    const float &eps)
{
    return (realEqual(v1, v2, eps));
}

int _tmain(int argc, _TCHAR* argv[])
{
    float a,b;
    bool x = realEqual(a,b); // OK
    bool x = equal(a,b); // compile error
    return 0;
}

这无法编译,给我:


------开始:项目:测试,配置:调试Win32 ------

test.cpp

c:\users\\\
inja\documents\visual studio 2010\projects\test\test\test.h(10):错误C2589:'::':右侧的非法令牌of'::'

c:\users\\\
inja\documents\visual studio 2010\projects\test\test\test.h(10):错误C2059:语法错误:'::'

========== Build:0成功,1失败,0最新,0跳过==========

------ Build started: Project: test, Configuration: Debug Win32 ------
test.cpp
c:\users\ninja\documents\visual studio 2010\projects\test\test\test.h(10): error C2589: '::' : illegal token on right side of '::'
c:\users\ninja\documents\visual studio 2010\projects\test\test\test.h(10): error C2059: syntax error : '::'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

冒犯的行是一个定义eps参数的默认值为equal()函数。

The offending line is the one with the definition of the "eps" parameter's default value for the equal() function.

谷歌搜索显示人们有与numeric_limits的其他函数类似的非法令牌错误,即min()和max(),但是这是由于一些#define存在Windows特定的c ++标准库头文件,由于某些原因,定义了min和max。没有提到epsilon(),我绝对困惑为什么我在这里得到一个错误。无论如何,将函数名从等于更改为类似smartEqual仍然给出相同的错误,因此名称显然不是问题。是什么?

A google search revealed people've had similar "illegal token" errors with other functions from numeric_limits, namely min() and max(), but these were due to some #define present in Windows-specific c++ standard library header files, which defined "min" and "max" for some legacy reasons. No mention about epsilon(), and I'm absolutely stumped about why I'm getting an error here. Anyway, changing the function name from "equal" to something like "smartEqual" still gives the same error, so the name is obviously not the issue. What is?

谢谢!

推荐答案

由Visual Studio中的此错误:

It looks like it's caused by this bug in Visual Studio:

https: /connect.microsoft.com/VisualStudio/feedback/details/583081/

另请参阅:

模板功能专用化默认参数

这篇关于为什么我得到一个“非法令牌”编译时错误与这片C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆