Incorrect conversion from 'int' to 'const char *'

I use gcc for code blocks, and I would like to write a function that uses an array of entries.

However, I keep getting the error:

 invalid conversion from 'int' to 'const char*' 

The code:

 #include <iostream> #include <string> using namespace std; struct rendeles { string nev; int mennyiseg; }; struct teaceg { string nev; int mennyiseg; }; int szam; struct rendeles rendelt [100]; struct teaceg cegek [100]; int h; int hanyadikceg (string cegnev) { for (int i=0;i<szam;i++) { if (cegek[i].nev==cegnev) { return i; } } return -1; } int main() { cout << "Hány db rendelés lesz összesen?"; cin >> szam; if (szam > 100) { cout << "Hiba: túl nagy a rendelések száma! (100 a maximum)"; return -1; } for (int i=0;i<szam;i++) { cout << "A(z) " << i+1 <<". cég neve:"; cin >> rendelt[i].nev; cout << "A(z) " << i+1 <<". rendelés mennyisége:"; cin >> rendelt[i].mennyiseg; } cout << endl; h = hanyadikceg('Lipton'); //the problem is in this line cout << "Hanyadik cég a xyz:" << h; for (int i=0;i<szam;i++) { cout << "A(z) " << i+1 << ". rendelés: " << rendelt[i].nev << " " << rendelt[i].mennyiseg << endl; } return 0; } 

What causes this error?

+4
source share
4 answers

For string literals, you need to use double quotes ( " ), not single quotes ( ' ).

+12
source

You use single quotes ( 'Lipton' ). Single quotes for char literature.

Use "Lipton" for the lite const char* .

+4
source

Change 'Lipton' to "Lipton" in the problem line and the compilation error will disappear.

In C / C ++, double-quoted expressions are strings (or, technically, string literals) and are allowed to be of type char * or const char * , while single-expression expressions are characters (or character literals) and are permitted by char (which can be implicitly executed on int ). This explains your mistake: the compiler cannot convert the integer char to const char * , which requires a function signature.

+2
source
 h = hanyadikceg('Lipton'); 

it should be

 h = hanyadikceg("Lipton"); 
+2
source

Source: https://habr.com/ru/post/1401374/


All Articles