C++: convert int to string - c++

I have an int value out of a slider and want to write that value into a label with this expression
ui->label_1->setText(std::to_string(ui->verticalSlider->value()));
but i get this error:
ambiguous call to overloaded function
I think i have to typecast the value, but don't know how.
ui->label_1->setText(std::to_string(static_cast<int>(ui->verticalSlider->value())));
also didn't work.

ui->label_1->setText needs QString and not std::to_string.
Try this:
ui->label_1->setText(QString::number(ui->verticalSlider->value()));

Related

Why do I get "Non-standard syntax; use '&' to create pointer to member" and "no overloaded function takes 2 arguments" errors?

I'm getting these two errors in my code:
Error C3867 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': non-standard syntax; use '&' to create a pointer to member 59
Error C2661 'Product::Product': no overloaded function takes 2 arguments 59
It seems like when I'm trying to call my non-default constructor it's only getting 2 arguments even though I'm trying to pass it 4. This is just speculation, but I suspect maybe I need to add some NULL checkers or something? but I don't see how any of the arguments I'm passing it could be NULL so I'm stuck.
Here's my declaration and definition for my non-default constructor:
Product(bool restocking, string name, int quantity, double price); //Declaration
Product::Product(bool restocking, string name, int quantity, double price):InventoryItem(restocking), quantity_(quantity), price_(price) { } //Definition
Product is a derived from InventoryItem
Here's the troublesome piece of code:
void InventorySystem::BuildInventory(void) {
int i{ 0 };
string name_buffer;
string quantity_buffer;
string price_buffer;
ifstream fin("in_inventory.txt");
if (fin) {
while (getline(fin, name_buffer, ';') && i < g_kMaxArray) {
getline(fin, quantity_buffer, ';');
getline(fin, price_buffer, '\n');
p_item_list_[i] = new Product(false, name_buffer, atoi(quantity_buffer.c_str), atof(price_buffer.c_str)); \\ Error on this line
i++;
item_count_++;
}
}
else {
cout << "Error: Failed to open input file." << endl;
}
fin.close();
}
cstr() is a function, so make sure you call it to get the result (rather than treating as a member variable)
p_item_list_[i] = new Product(false, name_buffer, atoi(quantity_buffer.c_str()), atof(price_buffer.c_str()));
Use empty parentheses to call a member function with no parameters:
... atoi(quantity_buffer.c_str()) ...
If the compiler sees c_str without parentheses, it makes a very unreasonable assumption that you want to refer to the function itself, using a pointer to it. This is a rarely-used feature.
To complicate the matters even more, there are two possible syntaxes for pointer to member function, and one of them is non-standard. This is what the compiler complains about. You don't need any of this, so add parentheses to tell the compiler that you want to call the function and not take a pointer to it.
The pair () is the function call operator. Without it you only get the function pointer, no calls are made
But don't use atoi(). See the reason why it should be avoided. Use stoi() instead, and use stod() to get a double
p_item_list_[i] = new Product(false, name_buffer,
stoi(quantity_buffer), stod(price_buffer));
As you can see, the code is much cleaner because there's no .c_str() everywhere, as the sto* family receives std::string directly (which is much better than receiving a const char*)
Another note: don't use such a long lines. No one likes horizontal scrolling

How to pass string to external function in LLVM Pass?

So I am trying to send the names of functions called through call instructions I find in a program to an external function as a string. So the declaration of my external function is: void func(string s); In my LLVM pass I am trying to pass a value to the parameter s. I am stuck at adding the function declaration of func using getOrInsertFunction, here is a code snippet:
Function * func;
Constant * funcDec = M.getOrInsertFunction("func",
Type::getVoidTy(M.getContext)), ???);
I am confused about what to put in place of ???.
As an example Type::getInt32Ty(M.getContext()) is used if the parameter is int. I know LLVM doesn't have std::string. So how can I achieve passing a string to an external function?
Thanks!
String is basically character pointer. Char is 8 bit. So you can put
Type::getInt8PtrTy(M.getContext()) in place of ???.

Switch statement on char variable

A library we are using defines constants and we have:
const char field[] = "666"
and I would like to:
switch(an_int){
case field:
Is there a way to achieve this? I get a compiler error saying field is not usable in a constant expression (GCC 5.2).
I would really wish to avoid modifying the declaration of field if possible.
field is an array. It is not a single value you can switch on.
You're trying to switch on the contents of a character array, when it is interpreted as an integer value.
Use atoi(), or a helper std::istringstream's operator >>, to convert the array into an int variable, then switch on it.
No, It is not possible to have switch statement on char array/string. In case you are sure that the field will contain numeric value then convert char array to integer.

C++ atof invalid use of void bug

Please Help I'm so close to finishing this huge project.
I don't understand an error I'm receiving
score-=(double)atof(kitty.pop_front());
!invalid use of void expression
kitty is a deque type string. Score is a long double. I'm not good with type conversions but the error seems really unrelated?
I tried other forum's solutions but they are all relatively unrelated.
dequeue::pop_front() returns void:
void pop_front();
So you can't use it in an expression that way.
You could instead do:
score-=(double)atof(kitty.front().c_str());
kitty.pop_front();
If the value in the deque is std::string, the atof function call should be:
score -= atof(kitty.front().c_str());
The reason is that atof expects a const char* parameter, and std::string::c_str() returns a const char* that represents the string.
Second, the front() function returns the value that is at the front of the deque. If you want to remove the value from the front, then you call kitty.pop_front().

Problems with overloaded method call in C++

I am trying to get the Aruco AR library working by trying out the simple test in my code.
For some reason I cannot get the call to detect() to work. My code is as follows:
cv::Mat image(480,640,CV_8UC3, mimFrameRGB.data());
MarkerDetector mDetector;
std::vector<Marker> markers;
CameraParameters cParams();
float markerSize = 0.1f;
mDetector.detect(image,markers,cParams,markerSize);
The compiler complains that there is no overloaded function that matches my input parameters. Specifically that parameter 3 should be of type cv::Mat.
Looking at the header file for the MarkerDetector, the following two method calls are found:
void detect(const cv::Mat &input,std::vector<Marker> &detectedMarkers,cv::Mat camMatrix=cv::Mat(),cv::Mat distCoeff=cv::Mat(),float markerSizeMeters=-1) throw (cv::Exception);
void detect(const cv::Mat &input,std::vector<Marker> &detectedMarkers, CameraParameters camParams,float markerSizeMeters=-1) throw (cv::Exception);
I am trying to call the second one, however it chooses the first one and gives me a compile error. What is going wrong? are my input parameters not matching either case?
I think the issue is this line:
CameraParameters cParams();
This does not declare a variable of type CameraParameters, but instead is a function prototype for a function called cParams that takes no parameters and returns a CameraParameters. This is an extremely annoying part of the C++ language, since the code is legal but doesn't do what you want.
Because cParams is actually a function prototype and not a variable declaration, the C++ overload resolution mechanism is getting confused about the types of the arguments and is failing to correctly select the oveload you'd like. Removing the parentheses on this line and having it just read
CameraParameters cParams;
should fix this problem.
Hope this helps!
The problem is; when you declare cParams like CameraParameters cParams(); you are actually declaring a function named cParams which returna a CameraParameters. It should be CameraParameters cParams; (Remove paranthesis).
I'm betting it's because:
CameraParameters cParams();
which actually declares a function cParams taking no arguments and returning a CameraParameters.
So when you call the method and pass it cParams, it interprets that as a function pointer, which is probably why it chooses the first variant.
Replace it with:
CameraParameters cParams;