声明为 void 的变量或字段

发布于 2024-07-10 23:43:10 字数 291 浏览 7 评论 0原文

我有一个名为:的函数

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

难理解 2024-07-17 23:43:10

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

void initializeJSP(unknownType Experiment);

尝试使用 std::string 而不仅仅是 string (并包含 标头) 。 C++ 标准库类位于命名空间 std:: 内。

It for example happens in this case here:

void initializeJSP(unknownType Experiment);

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

鸢与 2024-07-17 23:43:10

这实际上并不是函数“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 just string.

a√萤火虫的光℡ 2024-07-17 23:43:10

或者像我的情况一样,解决方案只是在 main.cpp 中声明合适的标头,而不是在 function.cpp 中声明标头,尝试包含该标头。

...

#include"header.h" //instead of "function.cpp"
int main() 

并且在 function.cpp 中,

#include"header.h"
void ()

这样编译和链接就可以正常工作...

or like in my case the solution was just to declare the fitting header in the main.cpp instead of the header in the function.cpp, trying to include that one..

...

#include"header.h" //instead of "function.cpp"
int main() 

and in function.cpp

#include"header.h"
void ()

this way compiling and linking just works fine ...

花之痕靓丽 2024-07-17 23:43:10

其他答案给出了非常准确的答复,我不完全确定您的问题到底是什么(如果只是由于您的程序中的未知类型,那么您会得到更多明确的错误以及您提到的错误),但要添加有关更多信息,如果我们在调用函数时将函数类型添加为 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";
}

错误:

 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)) ===|

因此,正如我们从这个示例中看到的,此原因也可能导致“变量或字段声明为 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:

#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.

墨离汐 2024-07-17 23:43:10

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

initializeJSP(Experiment);

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);
时光清浅 2024-07-17 23:43:10

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

例如:

void something(int x){
    logic..
}

int main() {

    **void** something();

    return 0;

}

如果是这样,你应该删除最后一个void。

Did you put void while calling your function?

For example:

void something(int x){
    logic..
}

int main() {

    **void** something();

    return 0;

}

If so, you should delete the last void.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文