63

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?

jonsca
  • 10,218
  • 26
  • 54
  • 62
Eduardo
  • 19,928
  • 23
  • 65
  • 73
  • 2
    At what line of code is the compiler pointing when it reports that error? What language are you using? What other code is around the function in question? More information, please. – Rob Kennedy Dec 12 '08 at 21:34

6 Answers6

93

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • I had this same issue. Using ```std::string``` instead of simply ```string``` solved it for me. I found that I didn't have to include `````` in the header. Have I just got away with it or is `````` in the header not essential? In case it makes a difference, based on Paul Price's answer, I'm using g++. – Mehmet Karatay Jun 11 '20 at 07:50
45

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.

Paul Price
  • 2,657
  • 30
  • 26
  • 8
    What's the problem with the function parameters though? (I know the accepted answer explains it, I just don't find this particular answer particularly helpful). – Bernhard Barker Aug 17 '15 at 13:01
0

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

ewertonvsilva
  • 1,795
  • 1
  • 5
  • 15
barpe
  • 1
-1

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);
Brighter side
  • 392
  • 2
  • 14
-1

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.

RSSB
  • 144
  • 2
  • 4
  • 14
  • 1
    To clarify: this example produces a "declared void" error by adding an (erroneous) "void" preceding the function *call* in line 10. – Technophile Apr 09 '19 at 21:08
-2

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.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42