声明为 void 的变量或字段
我有一个名为:的函数
void initializeJSP(string Experiment)
,在我的 MyJSP.h 文件中,我有:
2: void initializeJSP(string Experiment);
当我编译时,我收到此错误:
MyJSP.h:2错误:变量或字段initializeJSP声明无效
问题出在哪里?
I have a function called:
void initializeJSP(string Experiment)
And in my MyJSP.h file I have:
2: void initializeJSP(string Experiment);
And when I compile I get this error:
MyJSP.h:2 error: variable or field initializeJSP declared void
Where is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
例如,在这种情况下会发生这种情况:
尝试使用
std::string
而不仅仅是string
(并包含
标头) 。 C++ 标准库类位于命名空间std::
内。It for example happens in this case here:
Try using
std::string
instead of juststring
(and include the<string>
header). C++ Standard library classes are within the namespacestd::
.这实际上并不是函数“void”的问题,而是函数参数的问题。 我认为这只是 g++ 给出了无用的错误消息。
编辑:正如已接受的答案一样,修复方法是使用
std::string
而不仅仅是string
。This is not actually a problem with the function being "void", but a problem with the function parameters. I think it's just g++ giving an unhelpful error message.
EDIT: As in the accepted answer, the fix is to use
std::string
instead of juststring
.或者像我的情况一样,解决方案只是在
main.cpp
中声明合适的标头,而不是在function.cpp
中声明标头,尝试包含该标头。...
并且在
function.cpp
中,这样编译和链接就可以正常工作...
or like in my case the solution was just to declare the fitting header in the
main.cpp
instead of the header in thefunction.cpp
, trying to include that one.....
and in
function.cpp
this way compiling and linking just works fine ...
其他答案给出了非常准确的答复,我不完全确定您的问题到底是什么(如果只是由于您的程序中的未知类型,那么您会得到更多明确的错误以及您提到的错误),但要添加有关更多信息,如果我们在调用函数时将函数类型添加为 void,也会引发此错误,如下所示:
错误:
因此,正如我们从这个示例中看到的,此原因也可能导致“变量或字段声明为 void”错误。
Other answers have given very accurate responses and I am not completely sure what exactly was your problem(if it was just due to unknown type in your program then you would have gotten many more clear cut errors along with the one you mentioned) but to add on further information this error is also raised if we add the function type as void while calling the function as you can see further below:
Error:
So as we can see from this example this reason can also result in "variable or field declared void" error.
问题是,当你调用一个函数时,你不应该写函数的类型,这意味着你应该像调用函数一样
The thing is that, when you call a function you should not write the type of the function, that means you should call the funnction just like
你在调用你的函数时是否设置了void?
例如:
如果是这样,你应该删除最后一个void。
Did you put void while calling your function?
For example:
If so, you should delete the last void.