I have a function like this:
void getSprite(string *spriteLines[SPRITE_YSIZE]);
And then I have a calling to the function like this:
int main() {
string *spriteLines[SPRITE_YSIZE];
getSprite(spriteLines);
By here, everything OK. But I decided to declare spriteLines as a string instead of a pointer so I changed the code to this:
int main() {
string spriteLines[SPRITE_YSIZE];
getSprite(&spriteLines);
And an error shows up:
error: cannot convert ‘std::__cxx11::string (*)[5] {aka std::__cxx11::basic_string<char> (*)[5]}’ to ‘std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}’ for argument ‘1’ to ‘void getSprite(std::__cxx11::string**)’ getSprite(&spriteLines);
Do someone of you knows why? I can't understand this.
Extra data: I'm using Eclipse Oxygen v1 and GNU G++.
In the first example, you declared an array of string pointers. In the second example you declared an array of strings. Both arrays are passed your function as a pointer. Your error occurs because the are different "types".
In the second example you would need to change your getSprite function to:
void getSprite(string spriteLines[SPRITE_YSIZE])
Related
I am returning the *res (which is value resultant string after concatenating strings) in the function. When I call this function and store the result in the char array then it gives me error [Error] incompatible types in assignment of 'char' to 'char [40]'
.I want to concatenate two strings in the function and return the concatenating string from function.Kindly help to solve to this problem
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
char strConcat(char *,char *);
char input1[10],input2[12],resultantString[40];
cout<<"Enter the 1st String=";
cin.getline(input1,10);
cout<<"Enter the 2nd String=";
cin.getline(input2,10);
//faulty code
resultantString=strConcat(input1,input2);
cout<<resultantString;
}
// st1 means String1 and St2 means string2 which we want to concat.
char strConcat(char *st1,char *st2)
{
char *res;
while(*st1!='\0')
{
*res=*st1;
*st1++;
*res++;
}
while(*st2!='\0')
{
*res=*st2;
*st2++;
*res++;
}
*res='\0';
return *res;
}
First off, remove using namespace std;, it's a bad habit.
Next up, move the function declaration void strConcat(char *,char *); out of main and make it the same type as the definition, this is your error, you declare strConcat to return void first but then you define it to return char, the compiler still thinks it returns void and thus when you're trying to assign something to it the compiler complains.
Next up, make main return int, your current definition isn't valid.
Next, indent your code so not only the compiler can read it but other humans too.
And the most important tip here, remove all of those static-sized arrays and that homebrewed strCat function and use std::string and it's operator+ for concatenation.
I am trying to get fingerprint data (which I created and stored as a text file in another code) which I have to compare with a new fingerprint in this code. The problem is that the fingerprint API requires the fingerprint data to be passed as char pointer. I'm using the following code :
std::ifstream infile("timestamp.txt");
char* text_stream;
std::string line;
if (infile.is_open()){
while ( getline (infile,line)){
if(text_stream){
*text_stream = malloc (1 + strlen (line));
strcpy(text_stream,line);
}
else{
fprintf (stderr, "malloc failure!");
}
}
infile.close();
}
I have also tried using other codes for the same purpose but I'm getting this kind of compilation error everytime:
verifiy.cpp: In function ‘int main()’:
verifiy.cpp:29:47: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
*text_stream = malloc (1 + strlen (line));
^
verifiy.cpp:30:31: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘char* strcpy(char*, const char*)’
strcpy(text_stream,line);
^
BTW, if you are just reading the whole file, you can write something like this:
#include <string>
#include <fstream>
#include <streambuf>
std::ifstream t("timestamp.txt");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
(If you care about the efficiency of the code above, there is a more efficient version here: Read whole ASCII file into C++ std::string)
That way, you don't need to create an explicit string buffer yourself. After that, you can just use str.c_str() to get a "const char*".
While using that, please beware that the resulting pointer can be invalid if you keep editing the string. In that case, you might need to make another copy of the string. If your function needs a "char*" instead of a "const char*", you might need to do that too. More information can be found here: How to convert string to char array in C++?
You actually have two errors in the code, first the one you show in your question (it's the same error in two different places), the second error is worse and will lead to undefined behavior.
The first error is that std::string is not a char*, so you can't use the old C functions with it. Reading this reference should help you a little.
The second problem is that text_stream is a pointer to char, meaning *text_stream will give you what text_stream points to, i.e. a single char, or text_stream[0]. This is problematic because of two reasons: The first because text_stream is uninitialized, its contents is indeterminate and dereferencing will not give you a valid pointer leading to undefined behavior. The second problem with that is that *text_stream is not a pointer, assigning to *text_stream will not change text_stream so text_stream will still be uninitialized while you overwrite some random memory. If the compiler doesn't shout warnings at you for this assignment, then you need to enable more warnings. Warnings from the compiler are just as important as compiler errors, and they often indicate places you do something which is not technically wrong but will lead to other problems when running the program.
The solution to both these problems is to stop using C function in C++. Use std::string exclusively, and when you need to call a function which needs a const char * argument, just use std::string::c_str to get such a pointer.
This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}
I get the following error when this code is run:
syslog(LOG_ERR | LOG_USER, "%s",errorString);
cannot convert ‘const string {aka const std::basic_string}’ to ‘const char*’ for >argument ‘2’ to ‘void syslog(int, const char*, ...)’ inServer.cpp /PeCounter
line 478 C/C++ Problem
I am daemonizing the program and the errorString value prints just fine when outputted to stdio using cout, but it will not print when using a syslog call.
Any way to get std::basic_string(char) into the form of 'const char'.
I found that std::basic_string has an item access method c_str() which seems to fix the compiling issue.
Here is a site with more information: http://en.cppreference.com/w/cpp/string/basic_string
The c_str() or data() member function provides a pointer to the first element of an array of char_type which contains your string. It's valid as long as the string object itself remains valid and unchanged (but beware that operations that may cause reallocations may invalidate the pointer -- best not to store it).
I'm trying to learn ncurses, and I'm reading the terrific guide here, but the example at user pointers does not compile. I get this error when I try to compile.
menu.cpp: In function 'int main()':
menu.cpp:44: error: invalid conversion from 'void (*)(char*)' to 'void*'
menu.cpp:44: error: initializing argument 2 of 'int set_item_userptr(ITEM*, void*)'
menu.cpp:70: error: invalid conversion from 'void*' to 'void (*)(char*)'
Also, you probably need to add cstdlib and cstring for that to compile with strlen and calloc.
I don't know much about void pointers, so some help fixing the example would be very appreciated.
Thanks
From reading the manual page:
#include <menu.h>
int set_item_userptr(ITEM *item, void *userptr);
void *item_userptr(const ITEM *item);
DESCRIPTION
Every menu item has a field that can be used to hold application-specific data (that is, the menu-driver code leaves it alone). These functions get and set that field.
userptr is user-specific data that you should supply to set_item_userptr(). If you don't want to supply any data, you should supply NULL. Looks like you are calling set_item_userptr() with a pointer to a function as its second argument. It is not guaranteed that you can convert a function-pointer to void * and back portably, either in C or in C++ (C++ requires a cast when converting a void * to any other pointer type). Without knowing what you are trying to do, if you really need to pass a function pointer, you should cast it to the appropriate type:
int ret = set_item_userptr(item, reinterpret_cast<void *>(ptr));
void (*pf)(char*);
pf = reinterpret_cast<void (*)(char *)>(item_userptr(item));
but it's not guaranteed to work. You should do something else, like having a struct that contains the function pointer, and passing a pointer to the struct to set_item_userptr().
You are using a C++ compiler, so you will need to do:
set_item_userptr(my_items[i], (void *)func);
and again,
p = (void*)item_userptr(cur);