本文介绍了为什么g ++错误消息如此令人生畏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我已经开始使用C ++了。语言本身很好,

但是我真的不喜欢的是我从

g ++获得的错误消息。


g ++错误消息通常没有帮助。


有时我只是拿错误的行号并尝试自己想出什么可能是什么?是错的。或者我只记得这个错误信息

并且已经知道:如果g ++说错误X它真的意味着错误Y.


例如,请使用以下代码:


#include< iostream>

class接口{

public:

virtual std: :string& getText()const = 0;

};

class实现:public Interface {

std :: string text;

public:

Implementation :: Implementation(std :: string text):text(text){}

std :: string& getText ()const {

返回文字;

}

};


bash-2.05b $ g ++ -c test.cpp

test.cpp:在成员函数`virtual std :: string&

Implementation :: getText()

const'':

test.cpp:13:无法将`this-> Implementation :: text''转换为

`std :: string&''


getText()的返回类型不能是std :: string,而是const

std :: string。但为什么g ++只是这么说呢?它可以说:可以

不返回对此的引用 - > ...来自const方法...


对于初学者,它还有一个令人生畏的错误消息是

从STL使用中填充几个屏幕

std :: vector< std :: string>测试;

std :: cout<<测试;

整个候选人都......无论如何,有26个条目的列表

不可读...应该有一个更好,更易读的方式来表示

错误。


Markus

I have started with C++ a few months ago. The language itself is nice,
but what I really don''t like are the error messages that I get from
g++.

g++ error messages are often just not helpful.

Sometimes I just take the line number of the error and try to figure
out myself what might be wrong. Or I just remember this error message
and know already: if g++ says error X it really means error Y.

For example, take the following code:

#include <iostream>
class Interface{
public:
virtual std::string &getText() const = 0;
};
class Implementation : public Interface{
std::string text;
public:
Implementation::Implementation(std::string text):text(text){}
std::string &getText() const{
return text;
}
};

bash-2.05b$ g++ -c test.cpp
test.cpp: In member function `virtual std::string&
Implementation::getText()
const'':
test.cpp:13: could not convert `this->Implementation::text'' to
`std::string&''

The return type of getText() must not be std::string, but const
std::string. But why doesn''t g++ just say that? It could say: "Could
not return reference to this->... from a const method"...

For beginners, it''s also intimidating to get an error message that
fills several screens from the STL usage
std::vector<std::string> test;
std::cout << test;
The whole "candidates are..." list with 26 entries is anyway
unreadable... There should be a better, more readable way to indicate
an error.

Markus

推荐答案





g ++(GCC)3.3.3 20040412(红色Hat Linux 3.3.3-7)


尝试gnu.g ++。帮助新闻组。

另外,尝试比较GNU C ++诊断消息

带有其他编译器生成的诊断消息。

我认为你会发现它们比较有利。

编译器诊断不要期望太多。

编译器无法知道你打算写什么*

它没有拿起理解来自评论

或您选择的类型和变量名称。

诊断几乎受限于规则

的C ++计算机编程语言

,不幸的是,并不总是帮助你理解

你做错了什么。


g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

Try the gnu.g++.help newsgroup.
Also, try comparing the GNU C++ diagnostic messages
with the diagnostic messages produced by other compilers.
I think that you will find that they compare favorably.
Don''t expect too much from compiler diagnostics.
The compiler can''t know what you *intended* to write.
It doesn''t pick up "understanding" from comments
or your choice to type and variable names.
Diagnostics are pretty much constrained to the rules
of the C++ computer programming language
which, unfortunately, don''t always help you understand
what you did wrong.


这篇关于为什么g ++错误消息如此令人生畏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:36