为什么g ++错误消息如此令人生畏? [英] Why are g++ error messages so daunting?
问题描述
几个月前我已经开始使用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 ++ -c test.cpp
test.cpp:在成员函数`virtual std :: string&
Implementation :: getText()
const'':
test.cpp:13:无法转换`this->实现:: text''到
`std :: string&''
getText()的返回类型不能是std :: string,但是const
std :: string。但为什么g ++只是这么说呢?它可以说:可以
不返回对此的引用 - > ...来自const方法...
对于初学者,它还有一个令人生畏的错误消息是
从STL使用中填充几个屏幕
std :: vector< std :: string>测试;
std :: cout<<测试;
整个候选人都......无论如何,有26个条目的列表
不可读...应该有一个更好,更易读的方式来表示
错误。
Markus
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
Markus Dehmann写道:
Markus Dehmann wrote:
我几个月前开始使用C ++。
语言本身很好,但我真的不喜欢
是我从g ++得到的错误消息。
g ++错误消息通常没有帮助。
有时我只是拿错误的行号
并试着找出自己可能出错的地方。
或者我只记得这条错误信息并且已经知道了:
如果是g ++说错误X它确实意味着错误Y.
例如,请使用以下代码:
#include< iostream>
类接口{
public:
virtual std :: string& getText(void)const = 0;
};
class实现:public接口{
private:std :: string text;
public:
Implementation(std :: string text):text(text){}
virtual std :: string& getText(void)const {
返回文字;
}
};
bash-2.05b
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(void) const = 0;
}; class Implementation: public Interface { private: std::string text;
public:
Implementation(std::string text):text(text) { } virtual std::string& getText(void) const {
return text;
}
};
bash-2.05b
g ++ -c test.cpp
test.cpp:在成员函数`virtual std :: string&
Implementation :: getText()
const'':
test.cpp:13:could不将`this-> Implementation :: text''\
转换为`std :: string&''
getText()的返回类型不能是std :: string但是
const std :: string。但为什么g ++只是这么说呢?它可以说:
无法返回对此的引用 - >来自... const方法...
对于初学者来说,获得一个也是令人生畏的从STL用法填充多个屏幕的错误消息
std :: vector< std :: string>测试;
std :: cout<<测试;
整个候选人......无论如何,有26个条目的列表是不可读的......
应该有一个更好,更易读的方式来指示错误。
cat test.cpp
#include< iostream>
class Interface {
public:
虚拟
std :: string& getText(void)const = 0;
};
class实现:public Interface {
private:
std :: string text;
public:
实现(std :: string text):text(text){}
virtual
std :: string& getText(void)const {
返回文字;
}
};
g ++ -Wall -ansi -pedantic -c test.cpp
test.cpp:8:警告:`class Implementation''\
有虚函数但非虚析构函数
test.cpp:在成员函数中\
`virtual std :: string& Implementation :: getText()const'':
test.cpp:15:错误:无效初始化引用\
类型''std :: string&' 'from expression \
类型''const std :: string''g ++ --version
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.
cat test.cpp #include <iostream>
class Interface {
public:
virtual
std::string& getText(void) const = 0;
};
class Implementation: public Interface {
private:
std::string text;
public:
Implementation(std::string text): text(text) { }
virtual
std::string &getText(void) const {
return text;
}
};
g++ -Wall -ansi -pedantic -c test.cpp test.cpp:8: warning: `class Implementation'' \
has virtual functions but non-virtual destructor
test.cpp: In member function \
`virtual std::string& Implementation::getText() const'':
test.cpp:15: error: invalid initialization of reference \
of type ''std::string&'' from expression \
of type ''const std::string'' g++ --version
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 ++错误消息如此令人生畏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!