I need to pass a single element of a char array from int main to a function. This should pass a single char to the array. Not the whole array or anything. I've tried converting it into a single char (IE, char herps = charArray[1];) and then passing it to the function as herps, but it actually gives the same error, even when I know it's one piece of it.
The function must be string herp(char); . The array contains spaces and whatnot.
This gives me the error 'invalid conversion from 'char' to 'const char*''
Source code:
#include <iostream>
#include <string>
using namespace std;
string toMorse(char);
string toMorse(char letter){
return "herp";
}
int main()
{const int SIZE = 10;
char line[SIZE];
cin.getline(line, SIZE);
int count=10;
string toMorse(line[count]);
return 0;}
If your array is declared as
char charArray[SOME_SIZE];
then calling a function declared as
string myFunc(char c);
via
myFunc(charArray[index]);
Will not give you an error.
In your main(),
string toMorse(line[count]);
you are not calling toMorse function, you are actually creating a string instance named toMorse, constructor gets called and looks for const char * as argument. Refer std::string .
You are creating a char array of size 10 and try to read 11th index, which is also wrong.
Modified Code:
int main()
{
const int SIZE = 10;
char line[SIZE];
cin.getline(line, SIZE);
int count=9; //Any whole number between 0 and SIZE-1 is acceptable.
toMorse(line[count]);
return 0;
}
In the second last statement in your code that is posted, function call syntax is incorrect.
string toMorse(line[count]); needs to be replaced with string returnString = toMorse(line[count]); Or may be you want to just pass char than remove the "string" from that line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.