variable or field declared void - c++

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?

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

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.

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

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

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.

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.

Related

Visual Studio Code doesn't work with headers in the same project folder [duplicate]

I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.

Why am I getting an undefined reference error while using separate .cpp and .h files for a class?

I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.

g++ compiler not generating error/warning for undefined methods

I have a class that has a declared method but not defined/used anywhere. I expected this piece of code to generate linking error but it did not. Looks like compiler is smart enough to remove dead code. Which default optimization is doing this? How can I explicitly disable it to generate the error?
#include <iostream>
using namespace std;
class Base{
public:
int x;
string name;
void set(int val){ x = val;};
int get(){ return x;}
void Init();
};
int main() {
Base base;
base.set(10);
cout << base.get() << endl;
return 0;
}
EDIT1: Here Init() function is not defined and neither used anywhere. So, I expected compiler to complain about this not defined. But don't see any error/warning.
Thanks in advance.
Generally the linker will only produce errors for undefined symbols that are used. As you never call Init there is no error.
Looks like compiler is smart enough to remove dead code.
The compiler is not even "smart" here. There is no code using a function so that function is not needed to produce an executable program.
The function is not even "ODR used" so technically the compiler would be wrong to require a definition.

Function Overloading in Visual c++

I was writing a program of function overloading in Visual C++ 2010 .
Following is my code
// overload.cpp : Defines the entry point for the console application.
#include<Windows.h>
#include<iostream>
#include<conio.h>
using namespace std;
//abs is overloaded in 3 types
int abs(int i);
double abs(double d);
long abs(long f);
void main()
{
cout<<abs(-10)<<"\n";
cout<<abs(-11.0)<<"\n";
cout<<abs(-9L)<<"\n";
getch();
}
int abs(int i)
{
cout<<"using integer abs()\n";
return i>0? -i:i;
}
double abs(double d)
{
cout<<"using double abs()\n";
return d>0? -d:d;
}
long abs (long l)
{
cout<<"using long abs()\n";
return l>0?-l:l;
}
I am having problems in double abs and long abs function that
1>c:\users\abc\documents\visual studio 2010\projects\overload\overload\overload.cpp(22): error C2084: function 'double abs(double)' already has a body
1>c:\users\abc\documents\visual studio 2010\projects\overload\overload\overload.cpp(26): error C2084: function 'long abs(long)' already has a body
Why this problem is coming?
I have changed the compilation from c to c++
but recently I ran an other program for overloading,it worked.I don't know how? here is the code.
#include<iostream>
#include<cstdio>
#include<conio.h>
#include<cstring>
using namespace std;
void stradd(char*s1,char*s2);
void stradd(char*s1,int i);
void main()
{
char str[80];
strcpy(str,"hello");
stradd(str,"there");
cout<<str<<"\n";
getch();
}
//concatenate a string with a "stringized "integer
void stradd(char*s1,int i)
{
char temp[80];
sprintf(temp,"%d",i);
strcat(s1,temp);
}
//concatenate 2 strings
void stradd(char*s1,char *s2)
{
strcat(s1,s2);
}
and output is hellothere
Your problem comes from a header in which abs is declared for some types such as double. You're not allowed to have to functions with exactly the same header (that is, same return type, same name, same list of parameters, same qualifiers such as const).
There are two ways of avoiding this:
Use the standard library: std::abs is good, you don't need to implement it yourself
Naming the method absoluteValue or myAbs or whatever you like, but not abs
A third way, namely removing using namespace std does not work according to your comment. This is because you include Windows.h. This itself includes a bunch of headers, probably including math.h. This gives a method called abs in the global namespace. Better don't include Windows.h and include cmath if you need to. Then, abs is only declared in namespace std, hence you can call it with std::abs and is different from abs.
When overload resolution cannot select one function as the unique best match, the call is ambiguous. An ambiguous call produces a compilation error.
In std there is already an abs() with the following signature:
int abs (int n);
So while you try to overload it with double and long it results in ambiguity for the compiler.
If you're a beginner learning about coding i suggest you to use function names not defined in libraries (at least the ones you have included).
stefan have already given the solution to it:
Remove using namespace std; and explicitly write std::cout
OR
Re name your function to absoluteValue or something else
OR
Use explicit namespaces in function declaration and calls. (Not tested, though it should work)
Put your function inside a class or namespace.
Maybe this would provide you with a little insight (From SO).
EDIT:
The second question's overloaded functions stradd() is not defined in any other library. That is why no Compilation Errors. The following function signature in your code will result an error: char * strcat ( char * destination, const char * source )
Your primary problem is that you use global namespace. Just declare your function in your own namespace and all name collisions will be gone.
Let me explain why you're getting those compile-time errors.
Somewhere in the headers you included there are double abs(double) and long abs(long) functions. Then you're creating functions with the same signatures by your own. So compiler just don't know what to use when you'll call one of them - there are 2 pairs of equal functions. So it refuses to compile that, and you're getting those errors.
So you have 2 choices - hope that every time you'll want to create a function you will choose an unique name, or just create a namespace and your function names should be unique only to another functions in your namespace.
And it's not about overloading - void func(int i) overloads void func(float f), but void func(int i) overrides void func(int i). You can override superclass member functions in subclasses, but you cannot override standalone functions like abs().
just change abs function name with another.
abs() is a keyword therefore it is showing errors.

Why is compiler not flagging this as an Error instead of warning?

#include <iostream>
using namespace std;
class test{
public:
test() { cout<<"CTOR"<<endl; }
~test() { cout<<"DTOR"<<endl; }
};
int main()
{
test testObj();
cout<<"HERE"<<endl;
}
Output:
HERE
Compiler skips the line "test testObj(); " and compiles the rest with warning and when run will generate the output. The warning is "prototyped function not called (was a variable definition intended?) in VC++ 2008. Why does it not throw an error?
Because it's not an error.
Your code has fallen foul of the most-vexing parse (in summary, test testObj(); doesn't define a variable, it declares a function).
Simply, because it's not an error to declare a function such as the one you declared. The warning should be useful enough, though.
Remove the () from the constructor call in Main
int main()
{
test testObj;
cout<<"HERE"<<endl;
}