Look at the simple code below:
int main()
{
int a;
a = SDL_Init(SDL_INIT_JOYSTICK);
a = SDL_NumJoysticks();
for (int i=0; i<a; i++)
cout << SDL_JoystickName(i);
return 0;
}
I'm using SDL library, It seems there is nothing wrong with the code, I'm trying to get names of connected joysticks but It gives me the error below:
error C2664: 'SDL_JoystickName' : cannot convert parameter 1 from 'int' to 'SDL_Joystick *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Where is the problem?
The documentation states that SDL_JoystickName takes a SDL_Joystick* as parameter (The compiler says the same)
You can retrieve the SDL_Joystick* via SDL_JoystickOpen which takes an int as parameter.
see https://wiki.libsdl.org/SDL_JoystickName
EDIT: As Joachim Pileborg said, if you just want to retrieve the names, SDL_JoystickNameForIndex is the way to go
Related
Hello to all the handsome people who want to save me from my pain,
I am new to c++, and after having done the usual intro stuff like strings, loops, arrays, pointers that w3schools tells you, I decided to write some code. I decided to start with a simple challenge that asks you to write some loops that will sort a few ints from highest to lowest or the other way around, without using the sort function(I was told there was something like this).
And the code is still not working. But this is normal for me as long I can understand why is it not working. (Pls don't answer why it doesn't work, just continue reading)
#include <iostream>
using namespace std;
int age[4] = { 26, 14, 6, 43};
int num = 4;
int a = 0;
double previous = 0;
int chosen = age[a];
void first() {
while (result[0] || result[num] == 0){
for (int i = 0; a != num; i++) {
chosen = age[a];
previous = age[i];
if (chosen < (previous + 0.01) ) {
chosen = result[a];
a++;
previous = age[0];
i = 0;
}
}
}
}
void last()
{
for (int i = 0; i < num; i++) {
cout << result[i] << "\n";
}
}
int main() {
first();
last();
}
So I tried to see if my main "if statement" was properly working and I added an already assigned value of result[0] where the variables were introduced.
int a = 0;
double previous = 0;
result[0] = 20;
int chosen = age[a];
And then when I ran the program this errors occurred:
error C2466: cannot allocate an array of constant size 0
(18,14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(18,16): error C2440: 'initializing': cannot convert from 'int' to 'int [0]'
(18,12): message : There are no conversions to array types, although there are conversions to references or pointers to arrays
(line 18 is where I assigned result[0])
When I saw this I was like "I guess I am stupid enough and I don't know how to assign arrays", but then I checked if this was the case and I think this is not the case.
And then I understood the first error because I tried changing result[0] to result[4] and it disappeared, but I had no idea what the second error was saying.
So, as one wise man probably said, "If you have coding problems that google cannot answer, go to stack overflow".
And so I did. But the article I found was with code that was equal to my knowledge of Chinese. And if you are still wondering, I do not know a single Chinese word. And I didn't find a better article that could explain how to fix the problem in pure coding for beginners. I just started coding c++ in the pandemic and I don't understand anything other than English yet.
And I would really appreciate if someone could tell me what does this error mean and how is it possible to disappear with his other error friends waiting below him.
You haven't declared result. C has the idea that using an undeclared variable causes the compiler to assume that the variable is declared extern int. Some C++ compilers support this for backwards-compatibility, but it's not part of standard C++.
In this case, the compiler seems to support this so it assumes extern int result; since it hasn't seen a declaration for this name.
This results in two diagnostics:
The compiler is warning you about the "default int" declaration and that this is deprecated: "missing type specifier - int assumed"
Then the compiler is telling you that you can't subscript an int (result[0] makes no sense if result is an int): "cannot convert from 'int' to 'int [0]'"
Add an array declaration for result to fix this.
I have this code, which is giving me a single error that indicates two problems.
int healthyConst = 0;
int sickConst = 1;
int recoveredConst = 2;
GraphMatrix<int, double> graph (100);
for (int i = 0; i < sampleSize; i++)
{
if(std::rand() % 2 > 0.05) graph.setVertexInfo(i, sickConst); //Error
else graph.setVertexInfo(i, healthyConst);
}
The error is:
error: no matching function for call to GraphMatrix::setVertexInfo(int&, int*)
And the function in question is declared as follows in the source:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
First, i should not be a reference. This seems nonsensical to me, yet I can not fix this. If I try to outsmart the compiler and type for(int* i = 0...) the error now complains of setVertexInfo(int&*, int*), and I don't even understand what this means.
Second, sickConst is not a pointer. It is just an int. Now I realize the method, as written, accepts VertexObject&, not VertexObject, but *sickConst also causes the compiler to complain of invalid type argument of 'unary *'. I've also tried &sickConst, which the compiler not unexpectedly interprets as a pointer.
Also note, identical errors are thrown for the second line of the for loop, presumably for the same reasons.
The question is: why am I getting these errors, and how do I fix them?
You stated that your function declaration within the source is as follows:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
However in your for loop you are passing it a type of int. Either change your function declaration & definition to accept a type of int or change the type that you are passing to your function as a VertexObject.
I have to use void** in a program. I am writing the following code. please guide me where I am wrong.
struct kdnode
{
kdnode* lch;
int k;
void **dataptr;
kdnode* rch;
};
then I am assigning
kdnode rt;
rt.dataptr=new void*[k];
rt.dataptr[0]=new int;
there was also this dereferencing involved:
*(rt->dataptr[0])=n; //n is an initialized integer value.
basically I want to assign the elements of the array of void pointers to pointers of different datatypes. As the compiler is throwing error :
void* is not a pointer-to object type
Please guide me what to do.
I can reproduce this error only if I add something like
*rt.dataptr[0] = 1;
With the addition, g++ complains:
main.cpp:13:14: error: ‘void*’ is not a pointer-to-object type
*rt.dataptr[0] = 1;
You can't dereference a void *. Cast it back to the original type (int * in this case) if you want to dereference it.
This question already has answers here:
how to avoid changing value of const in C
(4 answers)
Closed 8 years ago.
In an interview, I was asked to change constant value in CPP, but I said in CPP it is not possible but in c it is possible using pointer.
Interviewer said that using CPP it is possible and asked me to try but I couldn't and I came back to my room and tried again but what I figured out that I was able to change in C but same code was getting error when compiled as C++.
#include<stdio.h>
main()
{
const int i=5;
int *p;
p=&i;
*p=8;
printf("%d",i);
}
This code is changing the constant value of i in c but when I compile in CPP then
I get an error:
invalid conversion from 'const int*' to 'int*'
Given your error, the actual program must have been the following:
#include<stdio.h>
main()
{
const int i=5;
int *p;
p=&i;
*p=8;
printf("%d",i);
}
This produces a warning with gcc:
warning: assignment discards 'const' qualifier from pointer target type
and an error with g++:
error: invalid conversion from 'const int*' to 'int*'
So, let's change the title of your question to a better one:
Why does C allow conversion from const int * to int *, but C++ doesn't?
The reason why one gives a warning and another gives an error is not because one allows you to discard const qualifier and the other doesn't. It's merely because the C standard leaves such incorrect actions as undefined behavior, while the C++ standard specifically marks it as an error. Either way, doing this is wrong.
You can read this similar question asking why this is possible in C.
What I think interviewer wanted this:
int n = 0;
int const *p = &n;
The expression &n has type “pointer to int.” The declaration for p converts &n to type “pointer to const int,” adding a const qualifier in the process. This is a valid qualification conversion. This conversion in no way invalidates n’s declaration. The program can still use n to alter the int object, even if it can’t use *p for the
same purpose.
*p = 5; // wrong
But
n = 5; // OK
now *p is 5 although it is const type!
Now try to run this code in GCC or g++, it will work:
#include<stdio.h>
int main()
{
int n = 0;
const int *p;
p=&n;
printf("%d\n",*p);
n = 5;
printf("%d\n",*p);
return 0;
}
EDIT: The only way to change the value of const qualified object in C and C++ both is, change the value in the initialization statement:
const int i = 5 ---> const int i = 8
This is why const_cast exists, I believe the interviewer asked this because they have to deal with poorly designed library code they have no control over, however normally you shouldn't have to resort to using it in production.
I am interested in Judy Arrays and try to use it. But i had unable to do any useful thing using it. Every time it gives me casting errors.. Sample c++ code and the error given below.
#include "Judy.h"
#include <iostream>
using namespace std;
int main()
{
int Rc_int; // return code - integer
Word_t Rc_word; // return code - unsigned word
Word_t Index = 12, Index1 = 34, Index2 = 55, Nth;
Word_t PValue; // pointer to return value
//Pvoid_t PJLArray = NULL; // initialize JudyL array
Pvoid_t JudyArray = NULL;
char String[100];
PWord_t _PValue;
JSLI( JudyArray, _PValue, (uint8_t *) String);
return(0);
} // main()
This gives me the error
m.cpp: In function ‘int main()’:
m.cpp:19: error: invalid conversion from ‘long unsigned int**’ to ‘void**’
m.cpp:19: error: initializing argument 1 of ‘void** JudySLIns(void**, const uint8_t*, J_UDY_ERROR_STRUCT*)’
Please anyone help me to figure out what is the error what i'm doing..
Thanks
According to the documentation, you have the _PValue and JudyArray parameters reversed. Make your call look like this:
JSLI( _PValue, JudyArray, (uint8_t *) String);
Also, try not compiling it as C++ code. So far, your test uses no C++ features. I bet it will compile as C code. It looks like JudyArray relies on the fact that C will do certain kinds of implicit conversions between void * and other pointer types.
If this is the case, I'm not sure what to do about it. The error messages you're getting tell me that JSLI is a macro. In order to fix the error message you have in the comments on this answer, you'd have to reach inside the macro and add a typecast.
These kinds of implicit conversions are allowed in C because otherwise using malloc would always require ugly casts. C++ purposely disallows them because the semantics of new make the requirement that the result of malloc be cast to the correct type unimportant.
I don't think this library can be used effectively in C++ for this reason.
It seems that, you pass JudySLIns(void**, const uint8_t*, J_UDY_ERROR_STRUCT*) a wrong parameter, the first one, you'b better check it!
For integer keys there is a C++ wrapper at http://judyhash.sourceforge.net/