• Register
2 votes
172 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,240 points

Related questions

0 votes
1 answer 22 views
22 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 9.1k points
0 votes
1 answer 10 views
10 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 67 views
67 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 5 views
5 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 9.1k points
1 vote
1 answer 23 views
23 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 90 views
90 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 181 views
181 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 98 views
98 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 5.8k points
0 votes
1 answer 25 views
25 views
Problem: What does this error in Valgrind mean? I've looked at a lot of other posts but I still don't understand what the error actually means. Is there a problem with my Dragon destructor function? ==48500== Invalid read of size 8 ==48500== at 0x40AF66: Dragon::~Dragon() (in ... () (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test) ==48500== by 0x401EE1: main (in /u3/nsah/cs246/1149/a5/cc3k/cc3k/test)
asked Dec 26, 2020 Mashhoodch 9.1k points
0 votes
1 answer 16 views
16 views
Problem: I have the following function : void getdata(int arr[], int n) { for (int i = 0; i < n; i++) { int a = srand(time(NULL)); arr[i] = a; } } And I call it in main: getdata(arr, 1024); I get "void value not ignored as it ought to be" but I don't understand what is wrong.Why do I get this error?
asked Dec 21, 2020 Mashhoodch 9.1k points