C++ 错误:变量或字段“myfunction”声明为无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19948581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: variable or field ‘myfunction’ declared void
提问by jozxyqk
In the following, I haven't defined the type doesntexist
.
在下面,我没有定义 type doesntexist
。
void myfunction(doesntexist argument)
{
}
GCC 4.7.2 says "error: variable or field ‘myfunction' declared void
"
GCC 4.7.2 说“ error: variable or field ‘myfunction' declared void
”
My question is this: What's going through the compilers mind here to refer to the function name being void and not the argument type?
我的问题是:这里的编译器会想到什么来引用函数名称为 void 而不是参数类型?
[EDIT]
Before downvoting, be aware the answer to this issue is related to the order of the errors and -Wfatal-errors
stopping the more immediately relevant message from being printed. This is not simply me having a go at a slightly vague compiler message.
[编辑]
在投反对票之前,请注意此问题的答案与错误的顺序和-Wfatal-errors
停止打印更直接相关的消息有关。这不仅仅是我对稍微模糊的编译器消息的尝试。
采纳答案by jozxyqk
Thanks, @JoachimPileborg. The unedited error log didn't contain anything useful, and it should have! The comment lead me to the issue and solution... remove -Wfatal-errors
from my makefile.
谢谢,@JoachimPileborg。未经编辑的错误日志没有包含任何有用的东西,它应该有!该评论将我引向问题和解决方案...从我的 makefile 中删除-Wfatal-errors
。
19:17 >>> gcc -Wfatal-errors main.c
main.c:2:17: error: variable or field ‘myfunction' declared void
compilation terminated due to -Wfatal-errors.
and removing -Wfatal-errors
...
并删除-Wfatal-errors
...
19:18 >>> gcc main.c
main.c:2:17: error: variable or field ‘myfunction' declared void
main.c:2:17: error: ‘doesntexist' was not declared in this scope
Problem solved.
问题解决了。
For those who say "why use-Wfatal-errors
in the first place?": I usually don't want allthe errors as the first can trigger the rest. In this case it looks like the errors are given out of order, or at least in an unexpected order - I assume the compiler would run into the ‘doesntexist' was not declared
error first.
对于那些说“为什么-Wfatal-errors
首先使用?”的人。:我通常不想要所有的错误,因为第一个可以触发其余的错误。在这种情况下,错误似乎是乱序给出的,或者至少以意外的顺序给出 - 我认为编译器会首先遇到‘doesntexist' was not declared
错误。
回答by Sunil Bojanapally
Its definitely not the problem with the function being void
type, possibly g++
compiler poor error message schema when the function parameters consists unknown type.
它绝对不是函数void
类型的问题,g++
当函数参数包含未知类型时,可能编译器错误消息模式。
回答by GMasucci
Hi @jozxyqk you need to specify a type for the argument, if what you have in Coliruis properly representative of your code what you need to do is provide a valid datatype for the argument, something like void myfunc(string argument)
or void myfunc(int argument)
etc.
嗨@jozxyqk,您需要为参数指定类型,如果您在Coliru中拥有的内容正确代表您的代码,您需要做的是为参数提供有效的数据类型,例如void myfunc(string argument)
或void myfunc(int argument)
等。
There is a decent resource on datatypes here, and another here. It may be worth doing a Googlesearch for how to use data types in c++or similar so yu can find some reading material on them and their usage.
有数据类型上一个体面的资源在这里,另一个在这里。可能值得在Google 上搜索如何使用 C++或类似的数据类型,以便您可以找到有关它们及其用法的一些阅读材料。
Linked hereis a modified version which shows a string as a valid datatype for an argument and an overloaded version for an int argument.
此处链接的是一个修改版本,它显示一个字符串作为参数的有效数据类型,以及一个 int 参数的重载版本。
Let me know if you need more information:)
如果您需要更多信息,请与我们联系:)