variable or field declared void

C++StringVoid

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

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

Solution 5 - C++

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.

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEduardoView Question on Stackoverflow
Solution 1 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 2 - C++Paul PriceView Answer on Stackoverflow
Solution 3 - C++barpeView Answer on Stackoverflow
Solution 4 - C++Daniel MontañaView Answer on Stackoverflow
Solution 5 - C++RSSBView Answer on Stackoverflow
Solution 6 - C++user11353491View Answer on Stackoverflow