GCC的两个不寻常的错误信息 [英] GCC's two unusual error messages

查看:203
本文介绍了GCC的两个不寻常的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我遇到了两个我从未见过的错误消息。这是完全新的。



以下是代码:

 模板< typename T> 
struct adder {adder(const T& item){}};

模板< typename T>
void initializer(const T& item){adder< T>(item); }

int main()
{
initializer(const string literal);
}

在编译时,GCC会给出这些错误:


prog.cpp:在函数'void initializer(const T&)'中:

prog.cpp:6:error:declaration of 加法器< T> item'阴影参数

prog.cpp:在函数'void initializer(const T&)[with T = char [21]]':

prog。 cpp:10:从这里实例化
prog.cpp:6:错误:'adder< char [21]>的声明' item'阴影参数

prog.cpp:6:error:没有用于调用'加法器< char [21]> :: adder()'的匹配函数' strong>>
prog.cpp:3:注意:候选是:加法器< T> :: adder(const T&)[with T = char [21]]

prog。 cpp:3:note:adder< char [21]> :: adder(const adder< char [21]>&)






请参阅粗体文本。一个错误显示为两次,这是


错误:声明了加法器< T> item '隐藏参数

error:'加法器的声明< char [21]> item '阴影参数


这是什么意思?为什么它使用不同的模板参数显示两次?第一个是 T ,第二个是 char [21]


$ b 编辑: adder< T>(item)用名称​​项目 ?但那不是我想要的。我认为它应该创建一个传递 item 的临时对象作为构造函数的参数。



我想知道标准中处理此问题的部分!





其他有趣的错误是:


错误: 'adder< char [21]> :: adder()'


这表明编译器正在寻找 default构造?但是我想知道为什么编译器在实际上我的代码没有在第6行 使用它?






ideone处的代码: http://www.ideone.com/jrdLL

解决方案

理解正在发生的事情的关键是认识到:
adder(item);
是一个名为item的局部变量的定义,其类型为
加法器;括号是多余的,但完全允许
。如果你想调用构造函数,你将有
来消除歧义,通过以某种不能被
解释为数据定义的方式来编写,例如:
adder((item)) ;
(我不确定这可能是什么用途,它构造了一个临时的
加法器对象,然后在
表达式结束时将其破坏。)

一旦语句
被理解为数据声明,实际的错误消息应该清楚(er):函数参数是
,就好像它们是在顶层块中定义的一样
函数,因此加法器(item)是一个重复的(和相互矛盾的)
定义,而加法器没有默认构造函数,所以
不能定义它的一个实例无需提供参数。


Today, I came across two error messages which I never seen before. It's completely new to me.

Here is the code:

template<typename T>
struct adder { adder(const T &item) { } };

template<typename T>
void initializer(const T &item) {  adder<T>(item); }

int main() 
{
   initializer("const string literal");
}

On compiling, GCC gives these errors:

prog.cpp: In function ‘void initializer(const T&)’:
prog.cpp:6: error: declaration of ‘adder<T> item’ shadows a parameter
prog.cpp: In function ‘void initializer(const T&) [with T = char [21]]’:
prog.cpp:10: instantiated from here
prog.cpp:6: error: declaration of ‘adder<char [21]> item’ shadows a parameter
prog.cpp:6: error: no matching function for call to ‘adder<char [21]>::adder()’
prog.cpp:3: note: candidates are: adder<T>::adder(const T&) [with T = char [21]]
prog.cpp:3: note: adder<char [21]>::adder(const adder<char [21]>&)


See the bold text. One error is shown twice, which is this

error: declaration of ‘adder<T> item’ shadows a parameter
error: declaration of ‘adder<char [21]> item’ shadows a parameter

What does it mean? Why does it show twice with different template arguments? First one with T, second one with char [21]?

EDIT: does adder<T>(item) declare variable with name item? But that is not what I intended. I think it should create a temporary object passing item as argument to the constructor.

I would like to know the section from the Standard which deals with this issue!


Other interesting error is this:

error: no matching function for call to ‘adder<char [21]>::adder()’

Which indicates that the compiler is looking for default constructor? But I'm wondering why is the compiler looking for it when in fact my code doesn't use it at line 6?


Code at ideone : http://www.ideone.com/jrdLL

解决方案

The key to understanding what is happening is to realize that: adder(item); is a definition of a local variable named item and having type adder; the parentheses are superfluous, but perfectly permissable. If you want to call the constructor, you'll have to disambiguate, by writing it in some way that cannot be interpreted as a data definition, say: adder((item)); (I'm not sure what use this may be. It constructs a temporary object of adder, then destructs it at the end of the expression.)

The actual error messages should be clear(er) once the statement is understood as a data declaration: function parameters are treated as if they were defined in the top level block of the function, so adder(item) is a duplicate (and contradictory) definition, and adder doesn't have a default constructor, so you can't define an instance of it without providing arguments.

这篇关于GCC的两个不寻常的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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