Any idea why this user defined literals fails? [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 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

Related

No Matching Function To Call Error, and i don't know why [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 3 months ago.
Improve this question
I'm having an error that the function that I'm calling doesn't exist and I don't know why. It seems to have something to do with pointers but we haven't learned pointers. I call the function and wrote it and declared it (I'm mostly just typing so I can post this at this point)
Here is my code
/*
* Program to validate a color and show its index
*
* Name: Rebecca Sakson
* Date: November 13, 2022
*/
#include <iostream>
using namespace std;
const int NUM_COLORS = 5;
int findColorIndex (string findMe, string list[], int index[]);
int main()
{
string colors[NUM_COLORS] = { "red", "green", "blue", "yellow", "purple"};
int index[NUM_COLORS] = {0,1,2,3,4};
string findMe;
int colorIndex;
//int idkWhy = NUM_COLORS;
cout << "Color?" << endl;
cin >> findMe;
colorIndex = findColorIndex(findMe, colors, NUM_COLORS);
if (colorIndex <= 0)
{
cout << "Color is valid, found at " << colorIndex << endl;
}
else
{
cout << findMe << " is not valid";
}
return 0;
}
int findColorIndex (string findColor, string list[], int index[])
{
bool found = false;
int functionIndex = 0;
while ((!found) && (functionIndex < *index))
{
if (list[functionIndex] == findColor)
{
found = true;
}
else
{
functionIndex++;
}
}
if (!found)
{
functionIndex = -1;
}
return functionIndex;
}
I think you meant to type this:
findColorIndex(findMe, colors, index);
Instead of passing NUM_COLORS, which is an int, not an int array.

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

C++: invalid use of 'void' [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
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;
}

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.