• Register
2 votes
287 views

Problem:

Hello guys,

I am taking an Udemy online course in C++ recently. I joined a few days back and learning the language step by step. Today, I found a problem in my c++ code that’s stopping my code running. My program is giving back an error “Invalid use of void expression” while I am trying to execute my program. Could any of you here describe be the problem and the way to get rid of this? Please, find my sample code snippet below

#include <iostream>
using namespace std;

void function1(){
  cout<<"My First Function"<<endl;
}
int function2(int a){
  return (a+5);
}
int main(){
  int n = function2(function1());
  cout<<n<<endl;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:31: error: invalid use of void expression
  int n = function2(function1());
                               ^

 

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

Well, all you need to know is, the “invalid use of void expression” error occurs in C/C++ once programmers try to assign a call to a void function to a variable. It could have also occurred when a void function is cast to another data type.

So, how can you fix the error? You need to stop casting the void return type function function1() to any other data type. Hence, your function2() only accepts the integer data type. So, passing an integer type data type instead of void function would stop the error causing. Let’s re-write the program this way.

#include <iostream>
using namespace std;

void function1(){
  cout<<"My First Function" <<endl;
}
int function2(int a){
  return (a+5);
}
int main(){
  function1();
  int n = function2(50);
  cout<< n <<endl;
}

Now the program running without any error and producing an output of:

My First Function
55

Keep asking and exploring new things. Thanks 

13 9 6
94,260 points

Related questions

0 votes
1 answer 19 views
19 views
I&rsquo;m getting invalid operands in binary expression in c++. Any idea how to solve?
asked Dec 7, 2020 TeamScript 13.5k points
1 vote
2 answers 98 views
98 views
Problem: class Student{ public: int rolll; }; int main(){ Student x, y; x.roll = 33; y.roll = 3; if(x != y) { // do something } } I am looking for a solution to why I am getting the &ldquo;Invalid operands to binary expression c++&rdquo; error?
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 54 views
54 views
Problem: I am making a basic text based RPG sorry if my question is stupid because im new to c++. So basically I have a small combat class that i have to link back and forth to from the map class so I had to forward declare my map class and it comes up with the error. BTW sorry there aren't any comments. Here is the error: invalid use of incomplete type class Map
asked Dec 24, 2020 Mashhoodch 13k points
0 votes
1 answer 331 views
331 views
Problem: I am a na&iuml;ve to programming in C language. Please find below some relevant part of my code. I do not see any issues in my below code but still I keep on receiving below error: &ldquo;Invalid operands to binary expression ('double' and 'double').&rdquo; Please find ... is a double???? } printf ("%d\n", rem);          I need the modulo at any condition  but it is not working return 0;
asked Aug 13, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 6 views
6 views
Problem: I've just started using Visual Studio (I got VS 2012 from dreamspark, and it hasn't been long since I've started using Windows again) and I'm having some trouble. pPlease help?
asked Apr 6 Sana8989 8.9k points
0 votes
1 answer 5 views
5 views
Problem : Why a warning of "control reaches end of non-void function" for the main function?
asked Apr 9 padma1 34.8k points
0 votes
1 answer 16 views
16 views
Problem: I run the following C codes and got a warning: control reaches end of non-void function int main(void) {}what does it mean? and if there is anything wrong that I havent noticed, please point it out
asked Dec 21, 2020 Mashhoodch 13k points
1 vote
1 answer 32 views
32 views
Problem: I am trying to make a program that will convert the temperature from Celcius to Fahrenheit and vice-versa. The sample code is attached below: #include <iostream> using namespace std; void dialouge(); int c2f(); int f2c(); int c2f() { int c; int choice; ... reaches end of non-void function what does it mean? and if there is anything wrong that I haven't noticed, please point it out.
asked Mar 9, 2020 Gavin 15.3k points
0 votes
1 answer 127 views
127 views
Problem: I have written the following class: class Foo { private:     static float scale; public:     static float setScale(float scale); }; If I try to implement the setScale() function as shown below: float Foo::setScale(float scale) {    this->scale = ... still throws the same error. Now I am really confused about the above error message as my function in question is already marked as static.
asked Aug 13, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 14 views
14 views
Problem. Someone know why we write void in c++ code
asked Apr 10 Subhendu 4.5k points