invalid conversion from 'int' to 'QString*' - c++

Basically im returning trying to return the integer counter to my main program however I get the error message of:
invalid conversion from 'int' to 'QString*' [-fpermissive]
return counter;
even though counter is an integer. This is probably a very simple fundamentals problem and I apologize for that but any input/explanations would be greatly appreciated.
function:
QString* MainWindow::sort(QString* a, int n, QString na)
{
int yes = 1, i;
int counter=0;
int j=0;
for (i = 0; i < n; ++i)
{
if (a[i] == na)
{
counter++;
qDebug() << "Found a duplicate of " << a[i];
yes = 0;
}
}
if (yes)
{
qDebug() << "No duplicates";
}
qDebug() << counter;
return counter;
}

In order to return a pointer to QString you can create a QString on a free store using copy constructor which will take a QString created from your counter which is int:
QString* MainWindow::sort(QString* a, int n, QString na)
{
int yes = 1, i;
int counter=0;
//...
return new QString( QString::number( counter));
}
Your code couldn't compile because of lack of possibility to implicitly convert int to QString. You shouldn't however pass pointers to QString, just return a QString.
QString MainWindow::sort(QString* a, int n, QString na)
{
int yes = 1, i;
int counter=0;
//...
return QString::number( counter);
}

The problem is simple; your function is supposed to return a QString * (according to QString* MainWindow::sort( ... )), but you're trying to return counter, which is an int (according to int counter = 0;).
C++ won't implicitly convert an int to a QString * - if that's really what you want to do (in this case, this isn't what you want to do), you'd need to cast it explicitly, with something like static_cast<QString *>(counter);.
However, in your case, you've just got a mismatch, and you should decide - do you want to return counter, or do you want to return the string? In the first case, you'd change your function declaration to int MainWindow::sort( ... ) (This is what you said you wanted to do in your OP.).
In the second case, instead of saying return counter;, you need to say return a; or similar. However, since the function is operating on the string in-place (i.e., not on a copy of it), this isn't really neccessary.

Your function is declared as returning QString* and you're returning an integer. This is a mismatch.
I think the real 'fix' is to correct the function prototype to return an integer int MainWindow::sort(QString* a, int n, QString na) or if this isn't under your control, then you're not implementing the function right.

An integer expression (except a constant integer expression with value 0) may not be implicitly converted to a pointer. And the error message says about this restriction.
Also it is totally unclear why you are goint to convert the counter to a QString pointer/

Related

cannot convert 'double(_cdecl*)()' to 'double'

As an assignment I have to write a code that takes user inputs, performs an operation with them, then prints them to the screen. However, I keep getting an error on line 18 where I call FunctionMultiply saying that the function cannot convert 'double(_cdecl*)()' to 'double'. I searched for this type of problem but it seems like all of them have to do with arrays which aren't in my code. How can I fix this?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int GetInt(void);
double GetDouble();
char GetLetter(void);
double FunctionMultiply(int, double);
int FunctionCharacter(char);
int main()
{
GetInt();
GetDouble();
GetLetter();
FunctionMultiply(GetInt, GetDouble);
FunctionCharacter(GetLetter);
printf("%f", FunctionMultiply);
printf("%c", FunctionCharacter);
return 0;
}
int GetInt(void)
{
int integer;
printf("Enter an integer\n");
scanf("%i", &integer);
return integer;
}
double GetDouble()
{
double dub;
printf("Enter a floating point number\n");
scanf(" %lf", &dub);
return dub;
}
char GetLetter(void)
{
char letter;
printf("Enter a letter\n");
scanf(" %c", &letter);
return letter;
}
double FunctionMultiply(int arg1, double arg2)
{
double product = arg1 * arg2;
return product;
}
int FunctionCharacter(char letter)
{
if (toupper(letter) <= 'M')
{
return 0;
}
else
{
return 1;
}
}
I think you are confusing function identifiers with storage. You have just called a bunch of functions at the beginning and not stored their results in anything.
It appears that you expect using the function identifier on its own will give you the result of the last call to that function. But it does not.
Here is how you would store the return values and use them later:
int my_int = GetInt();
double my_double = GetDouble();
char my_char = GetLetter();
double multiply_result = FunctionMultiply( my_int, my_double );
char char_result = FunctionCharacter( my_char );
printf( "%f", multiply_result );
printf( "%c", char_result );
Modify your main() like this:
int main()
{
int i = GetInt();
double d = GetDouble();
char c = GetLetter();
double a = FunctionMultiply(i, d);
char c = FunctionCharacter(c);
printf("%f", a);
printf("%c", c);
return 0;
}
Your problem is that you are passing function names rather than calling them. i.e. GetInt instead of GetInt().
It looks like you weren't paying attention to the lessons or examples showing how to use functions.
GetInt();
This calls the GetInt function, and ignores its return value.
GetDouble();
This calls the GetDouble function, and ignores its return value.
GetLetter();
This calls the GetLetter function, and ... you know the score by now.
FunctionMultiply(GetInt, GetDouble);
This is just nonsense. You're trying to call the FunctionMultiply function, passing the functions GetInt and GetDouble as arguments. You need to pass it an int and double, but you don't have an int and a double because you didn't store the results of GetInt and GetDouble anywhere.
You should have done this:
int i = GetInt();
double d = GetDouble();
char l = GetLetter();
Now you have variables i, d and l that hold the results of those function calls, so you can pass them in to other functions:
FunctionCharacter(i, d);
You seem to be under the impression that the name of a function magically changes to the result of the call, after you've called the function once.
It doesn't. The function call expression itself is the result of the call.
Instead of
ReturnADouble();
// call ^ and value v somehow separated? Why did you ever think that?
double result = ReturnADouble;
But according to the language rules, ReturnADouble is still the name of a function, and the compiler righteously complains when you give the name of a function when you should be giving a numeric value.
Your code should read more like
double result = ReturnADouble();
// ^ this call results in a value

g++ compiler is not recognizing my function

I'm just starting to code, and am learning about arrays right now. I am trying to write a program that takes in a list of arrays, and tells me if the first or last number is a 2. To do this, I'm using a function.
My code looks like:
#include <iostream>
using namespace std;
const int size = 6;
bool firstlast(int array[size]);
int main()
{
int array[size];
for (int index = 0; index < size; index++)
{
cout << "Enter value for array[" << index << "]\n";
cin >> array[index];
}
bool check = firstlast(array[size]);
if (check)
cout << "The array either starts or ends in 2!\n";
else
cout << "The array does not start or end with 2.\n";
return 0;
}
bool firstlast(int array[size])
{
if (array[0] == 2)
return true;
if (array[size - 1] == 2)
return true;
return false;
}
What am I doing wrong?
The compiler gives me the error:
candidate function not viable: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with and
The compiler is recognising your function fine.
The problem is in the manner your code calls the function
bool check = firstlast(array[size]);
which attempts to pass array[size] (a non-existent element of array) to a function expecting a pointer.
The call, presumably, should be
bool check = firstlast(array);
since arrays are implicitly converted to pointers when passed to functions.
This code
bool check = firstlast(array[size], size);
tries to pass the sizeth element of array not the array itself. In C++ arrays are passed by pointer, even if you write the function parameter with array syntax.
To avoid confusing yourself, change firstlast to
bool firstlast`(int* array, int size)`
and call it with
bool check = firstlast(array, size);

cannot convert from 'std::string' to 'char'

Changed completely due to suggestions from other member. Most problems solved, still having problems. Now won't output any names from the array in main. Not sure if I'm passing them back correctly from function.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void bubblesort(string[], const int);
int sub = 0;
int main()
{
const int maxsize = 100;
string friendArray[maxsize];
ifstream friends;
friends.open("myFriends.dat");
while (sub < maxsize)
{
getline(friends, friendArray[sub]);
sub++;
}
bubblesort(friendArray, maxsize);
cout<<friendArray[0]<<" "<<friendArray[1]<<" "<<friendArray[2];
system("pause");
return 0;
}
void bubblesort(string *array, const int size)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 1; count < (size - 1); count++)
{
if(array[count-1] >array[count])
{
temp = array[count-1];
array[count-1] = array[count];
array[count] = temp;
swap = true;
}
}
}
while(swap);
}
Your problem isn't necessarily that temp inside bubblesort is not a char, the problem is that array is declared as a string and not a string[].
The reason you're getting the error is because array[count+1] is of type char, and temp is of type string. std::swap expects two elements of the same type.
However, that may be the least of your problems, your code doesn't compile for quite a few reasons. Not just that but you're passing in maxsize to bubblesort at each iteration. There's a flaw in both your logic and your syntax.
EDIT: Since you're still having trouble getting the sorting to work, here's a working modification of your code:
#include <iostream>
void bubblesort(std::string array[], size_t size)
{
bool bSwapped;
std::string temp;
do
{
bSwapped = false;
for (size_t count = 1; count < size; count++)
{
if(array[count-1] > array[count])
{
std::swap(array[count-1], array[count]);
bSwapped = true;
}
}
}
while(bSwapped);
}
int main(void)
{
std::string array[] = { "def", "ghk", "abc", "world", "hello" };
bubblesort(array, sizeof(array)/sizeof(*array));
for (size_t i = 0; i < sizeof(array)/sizeof(*array); ++i)
std::cout << array[i] + " ";
std::cout << std::endl;
return 0;
}
bubblesort could also be written as: void bubblesort(std::string *array, size_t size). There's no difference in this case since, when passed to a function, arrays decay into pointers.
Since arrays are passed by reference, a pointer to the first element, any modifications made to array inside of bubblesort will actually be modifying your array in main. So that's how arrays are "returned".
std::vector is a good alternative to the standard array, since it automatically resizes and obviously contains the length of the internal array so that you don't have to pass the size everywhere you pass an std::vector. You can also use it the same way as a regular array.
temp is a string, array[count] is a char (since an std::string is a vector of char elements.) I'm not sure what you're trying to do here, but the compiler is correct - you can't assign a char to a string.
You could change temp to be a char, since all you do with it is assign a char to it, and then assign it back to an element of array, which is also a char.
You need to declare temp as char. You can use std::swap to avoid such mistakes in the future:
std::swap(array[count], array[count+1]);
This would make your code compile, but it would not do what you're trying to do (bubblesort). The problem is that you are passing a single string (which is also an "array" of characters) instead of an array of strings, which is, in a very lose sense, "an array of arrays of characters". Your bubblesort needs to accept string *array as its first parameter.

Sscanf in a C++ (Read in values and assign simultaneously)

Can I do something like this in C++ ?
//int i,Data[];
//malloc Data[];
$sscanf(line,"Data[%d]=0x%d",i,Data[i]);
Data[] is an int array and I am trying to read in random indices according to the value of 'i' I read in in one shot.
You can do it this way:
int index, value;
sscanf(line, "Data[%d]=%d", &index, &value);
// Check if index is within bounds of the array here
Data[index] = value;
You need the '&' because sscanf takes pointers as arguments.
Why not try it yourself? Don't forget you need to pass pointers to your values i.e.
sscanf(line, "Data[%d]=0x%d", &i, &Data[i]);
Whether or not it works (and it for the record, it doesn't), it makes for much more readable / safe code to do the assignment with a separate call (what happens if i is greater than the end of the array?):
const int maxData = 10;
int Data[maxData];
int i;
int tmp;
sscanf(line, "Data[%d]=0x%d", &i, &tmp);
if( i >= maxData )
{
printf("Error! index was too big");
}
else
{
Data[i] = tmp;
}
[Edit] # the down-voters: I know it doesn't work I was just trying to encourage the OP to try it for his/her self

Input system reference trouble

I'm using SFML for input system in my application.
size_t WindowHandle;
WindowHandle = ...; // Here I get the handler
sf::Window InputWindow(WindowHandle);
const sf::Input *InputHandle = &InputWindow.GetInput(); // [x] Error
At the last lines I have to get reference for the input system.
Here is declaration of GetInput from documentation:
const Input & sf::Window::GetInput () const
The problem is:
>invalid conversion from ‘const sf::Input*’ to ‘sf::Input*’
What's wrong?
Is there a special reason why you want to have a pointer rather than a reference?
If not, you could try this:
const sf::Input & InputHandle = InputWindow.GetInput();
This will return you a reference to your Input handle.
Btw, this worked for me:
const int& test(int& i)
{
return i;
}
int main()
{
int i = 4;
const int* j = &test(i);
cout << *j << endl;
return 0;
}
Output : 4
Don't know why your compiler doesn't want you to point the reference.