C++ 声明为 void 的变量或字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/364209/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:57:27  来源:igfitidea点击:

variable or field declared void

c++stringvoid

提问by Eduardo

I have a function called:

我有一个函数叫:

void initializeJSP(string Experiment)

And in my MyJSP.h file I have:

在我的 MyJSP.h 文件中,我有:

2: void initializeJSP(string Experiment);

And when I compile I get this error:

当我编译时,我收到此错误:

MyJSP.h:2 error: variable or field initializeJSP declared void

MyJSP.h:2 错误:变量或字段 initializeJSP 声明为无效

Where is the problem?

问题出在哪儿?

回答by Johannes Schaub - litb

It for example happens in this case here:

例如,在这种情况下发生在这里:

void initializeJSP(unknownType Experiment);

Try using std::stringinstead of just string(and include the <string>header). C++ Standard library classes are within the namespace std::.

尝试使用std::string而不仅仅是string(并包括<string>标题)。C++ 标准库类位于命名空间中std::

回答by Paul Price

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.

这实际上不是函数“void”的问题,而是函数参数的问题。我认为这只是 g++ 给出了无用的错误消息。

回答by Daniel Monta?a

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

问题是,当你调用一个函数时,你不应该写函数的类型,这意味着你应该像调用函数一样

initializeJSP(Experiment);

回答by RSSB

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:

其他答案给出了非常准确的答复,我不完全确定您的问题到底是什么(如果只是由于您程序中的未知类型,那么除了您提到的错误之外,您还会得到更多明确的错误)但要补充关于更多信息,如果我们在调用函数时将函数类型添加为 void,也会引发此错误,如下所示:

#include<iostream>
#include<vector>
#include<utility>
#include<map>
using namespace std;
void fun(int x);
main()
{
   int q=9;
   void fun(q); //line no 10
}
void fun(int x)
{
    if (x==9)
        cout<<"yes";
    else
        cout<<"no";
}

Error:

错误:

 C:\Users\ACER\Documents\C++ programs\exp1.cpp|10|error: variable or field 'fun' declared void|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

So as we can see from this example this reason can also result in "variable or field declared void" error.

因此,正如我们从这个例子中看到的,这个原因也可能导致“变量或字段声明为无效”错误。

回答by user11353491

Did you put void while calling your function?

你在调用你的函数时设置了 void 吗?

For example:

例如:

void something(int x){
    logic..
}

int main() {

    **void** something();

    return 0;

}

If so, you should delete the last void.

如果是这样,您应该删除最后一个空白。