• Register
1 vote
596 views

Problem:

Being a novice in c++ I have a question for all of the experts here. “Void value not ignored as it ought to be c++” What does this line actually mean? I am trying to compile my program but it’s not compiling at all and gives that error.

Here is the error portion of my sample code below:

#define ARRAY_SIZE 1024
void getdata(int arr[], int a){
    for (int p = 0; p < a; p++) {
        int n = srand(time(NULL));
        arr[p] = n;
    }
}
int main(void){
//Do something
}

Thanks for your solution

15.3k points
reopened by

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

Solution:

This is a GCC error message that means the return value of a function is 'void', but that you are trying to assign it to a non-void variable.

Look at the line 

        int n = srand(time(NULL));

The prototype for srand is void srand(unsigned int).
This means it returns nothing ... but you're using the value it returns. That’s the reason you are getting the error.

You may give it a try:

#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 1024
void getdata(int arr[], int a){
    for (int p = 0; p < a; p++) {
        int n = srand(time(NULL));
        arr[p] = n;
    }
}

int main(void){
    int arr[ARRAY_SIZE];
    srand(time(0));
    getdata(arr, ARRAY_SIZE);
}

Good day!

94.3k points
selected by
0 votes

Solution:

  int a = srand(time(NULL));

The prototype for srand is void srand(unsigned int) (provided you added <stdlib.h>).
This implies it returns nothing ..however you're using the value it returns (???) to assign, by initialization, to a.

Edit: this is what you require to do:

#include <stdlib.h> /* srand(), rand() */
#include <time.h>   /* time() */

#define ARRAY_SIZE 1024

void getdata(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        arr[i] = rand();
    }
}

int main(void)
{
    int arr[ARRAY_SIZE];
    srand(time(0));
    getdata(arr, ARRAY_SIZE);
    /* ... */
}

error: void value not ignored as it ought to be

This is a GCC error message that implies the return-value of a function is 'void', however that you are attempting to assign it to a non-void variable.

Example:

void myFunction()
{
   //...stuff...
}

int main()
{
   int myInt = myFunction(); //Compile error!

    return 0;
}

You aren't approved to assign void to integers, or any other type.

In the OP's situation:

int a = srand(time(NULL));

...is not approved srand(), according to the documentation, returns void.

    int a = srand(time(NULL))
        arr[i] = a;

Must be

 arr[i] = rand();

And put srand(time(NULL)) somewhere at the very starting of your program.

"void value not ignored as it ought to be" this error happens at the time function like srand(time(NULL)) does not return something and you are treating it as it is returning something. As in case of pop() function in queue ,in case you will store the popped element in a variable you will obtain the similar error since it does not return anything.

Check this line

result = getEdge(arr,toke);

You're attempting to capture the return value of a function for which the return type is void.

keep in mind, void is an incomplete type, and it's not compatible with any other type (in this instance, LHS is char), therefore the compiler properly complains.

That said,

*token = strtok(str, "_,");

Within the function also does not seem very accurate. Inspect the types there.

Here you are declaring a function without a return value:

void getEdge(char str[],char *token)

And here you are performing with a function which returns a char-value

char result;
result = getEdge(arr,toke);

So a solution could be to alter your function in:

char getEdge(char str[],char *token)
{
    ......
    return char_value;
}

printTemperature() is declared void (for example nothing returned) however it holds a return statement, and you've tried to assign the (void) value. You possibly imply to declare the function as 'float'.

31.7k points
edited by