C++: invalid use of 'void' [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code should print i = 35 as result but somehow it doesn't even compile. Why ?
#include <iostream>
using namespace std;
void increment(int &p){
p = p +10;
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}

No it shouldn't! increment has void as return type, that means that an expression call to this function has no value. If you want that call to be able to be used on the left part of an assignment, it must return a left-value.
Basically, when you write a=b a denotes a container but b a value.
You can try:
int &increment(int &p){
p = p +10;
return p; // return the reference passed as argument...
}
int main()
{
int i = 10;
increment(i) += 15;
cout<<"i = " <<i<<endl;
return 0;
}

Related

I have made a program to implement stack but it is not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
The code is not throwing any error but it is not taking the values that we pass in the enqueue function.
Here is the code:
#include <bits/stdc++.h>
#include <climits>
using namespace std;
struct Queue{
int *arr;
int front , rear;
int cap;
Queue(int c){
cap = c;
front = -1;
rear = -1;
arr = new int[cap];
}
void enqueue(int x){
if(rear == cap-1){
cout<<"The array is full";
}
rear++;
arr[rear] == x;
cout<<arr[rear]<<endl;
if(front == -1){
front = 0;
}
}
int dequeue(){
int data;
if(front == -1){`your text`
cout<<"Array is empty";
return INT_MIN;
}
data = arr[front];
arr[front] = 0;
if(front == rear){
front = rear = -1;
}
else{
front++;
}
return data;
}
};
int main() {
Queue q(3);
q.enqueue(24);
q.enqueue(30);
q.enqueue(42);
cout<<q.dequeue();
return 0;
}
the enqueue function is taking some garbage value instead of the integer value that we are passing in the argument.
Hi and welcome to Stackoverflow.
The problem is that you ignored your compiler warnings. Under https://godbolt.org/z/Pn1Mf115T i have thrown your code in an online compiler and it tells me/you:
<source>:20:19: warning: equality comparison result unused [-Wunused-comparison]
arr[rear] == x;
~~~~~~~~~~^~~~
<source>:20:19: note: use '=' to turn this equality comparison into an assignment
arr[rear] == x;
^~
=
1 warning generated.
Compiler returned: 0
So the compiler tells you that you comparing instead of assigning the values. Thats the reason why your queue takes garbage values, it just never gets data assigned and the output is the uninitialized memory from your C-style array.
Rule of thumb: Do not ignore compiler warnings.

I get a warning when I try to run the recursion program with return keyword [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I was expecting 1 2 3 as output, but when I try to run this code:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
return fun(x-1);
cout<<x<<endl;
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
I get this warning:
warning: control reaches end of non-void function
Why doesn't it return the value and call fun(x-1)?
But the below code works perfectly. I get 3 2 1 as output.
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
cout<<x<<endl;
return fun(x-1);
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
Once a function has return'ed, it can't execute any more code:
if (x>0){
return fun(x-1);
cout<<x<<endl; // <-- NEVER EXECUTED
}
The warning is because your function has a non-void return type, but is not return'ing any value when x is <= 0, thus causing undefined behavior.
Try this instead:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
int ret = fun(x-1);
cout << x << endl;
return ret;
}
return 0;
}
int main()
{
fun(3);
return 0;
}
Online Demo

Any idea why this user defined literals fails? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following code which implements the adler32 checksum:
constexpr uint32_t adler32(std::string_view sv)
{
constexpr const uint32_t MOD_ADLER= 65521;
uint32_t rv= 0, a= 1, b= 0;
for (unsigned char c:sv)
{
a= (a+c)%MOD_ADLER;
b= (b+a)%MOD_ADLER;
}
rv= a|(b<<16);
return rv;
}
//----------------------------------------------------
constexpr uint16_t operator ""_csum(const char* str,long unsigned len)
{
return adler32(std::string_view(str,len));
}
and the following test routine:
#include "adler32.h"
using easyUtils::operator""_csum;
#include <iostream>
using namespace std;
int main()
{
auto i= easyUtils::adler32("hello");
auto j= "hello"_csum;
auto k= easyUtils::adler32("hello");
cout << i << '\t' << j << '\t' << k << endl;
return 0;
}
Which gives the following output when compiled for std=c++17 using either clang or g++ under Linux:
./test/adlerTest
103547413 533 103547413
I would have expected 103547413 three times. Any ideas why this is not so?
constexpr uint16_t operator ""_csum
^^
And
103547413L % 65536L == 533L

Error when passing function as an argument in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I encountered this strange thing with with C++ when i tried to pass a function as an argument to another one. the problem here is that it works but not giving me the expected result.
here's my code (msvc2013):
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <iostream>
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
using namespace std;
#include "stdafx.h"
uint32_t random_color()
{
uint8_t r = rand() % 255;
uint8_t g = rand() % 255;
uint8_t b = rand() % 255;
uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);
return rgb;
}
void print_rgb(uint32_t(*color_generator)() = &random_color)
{
std::cout << color_generator << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 5; i++)
{
srand(time(NULL));
print_rgb();
}
system("PAUSE");
return 0;
}
the purpose of this code is more complicated, but this is a minimal example.
Question : although as you see, there was an srand(time(NULL)); in order for rand() to change values, it dosen't !
so , at the 5 times, I got the same value !
Is there any reason for this ? Am I missing something ?
looks like you are printing value of pointer to function, your code should be:
std::cout << color_generator() << std::endl;
Nothing weird is going on. This code prints out the address passed into it, and the address isn't random.
void print_rgb(uint32_t(*color_generator)() = &random_color)
{
std::cout << color_generator << std::endl;
}
You either need to call the function, here or, if your intention was that appending it to the stream would call it, instead implement something similar to a stream manipulator.

Object Value Passing in C++ functions [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class cal
{
int x, y;
public:
void set(int a, int b)
{
x = a;
y = b;
}
cal add(cal c1, cal c2)
{
cal temp;
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
void display()
{
cout << x << y; //display output
}
};
int main()
{
cal c1, c2, c3, c4;
c1.set(10, 30);
c2.set(20, 40);
c4 = c3.add(c1, c2);
c4.display();
}
I have tried this code in Xcode. but its not working. I'm not getting any error either.
I'm getting "c4.display Thread1:breakpoint 1.1"
Can please anyone tell me what I'm doing wrong here?
What I'm doing is adding two objects First and Second value together and Display.
And Also I checked info at c4.display()
and im getting this
Printing description of c4: (cal) c4 = (x = 30, y = 70) (lldb)
If you add #include <iostream> to the top it will compile and run just fine.