C ++ cannot convert 'const char *' to 'std :: string *'

I have this code below and I get a compilation error:

error: cannot convert 'const char*' to 'std::string*' for argument '1' to 'void sillyFunction(std::string*, int)'

 #include <iostream> #include <string> using namespace std; int counter = 0; void sillyFunction(string * str, int cool=0); int main(){ sillyFunction("Cool"); sillyFunction("Cooler", 1); return 0; } void sillyFunction(string * str, int cool){ counter++; if (cool){ for (int i=0; i<counter; i++) cout << *str << endl; } else { cout << *str << endl; } } 
+7
source share
8 answers

Do not take your parameter as string * , try using const string & instead

EDIT:

std::string and const char* are different types. std::string already has a conversion from string literals (ex: "Cool" ) to the actual string object. Therefore, passing the string literal "Cool" , in a sense, you pass the object std::string , and not a pointer to one.

The reason I decided to use const string & is mainly from personal coding practice. This minimizes the use of stack memory, and since you are passing in a constant string literal, there is no need to be able to change the parameter.

Also do not forget if you change the value with string * , which you no longer need to play in cout :

 if (cool){ for (int i=0; i<counter; i++) cout << str << endl; } else { cout << str << endl; } 
+11
source

change

 void sillyFunction(string * str, int cool){ counter++; if (cool){ for (int i=0; i<counter; i++) cout << *str << endl; } else { cout << *str << endl; } } 

to

 void sillyFunction(const char* str, int cool){ counter++; if (cool){ for (int i=0; i<counter; i++) cout << str << endl; } else { cout << str << endl; } } 
+3
source

To explain that the problem is actually ...

While the compiler is happy to organize the conversion of a char * / C string to std::string using the appropriate std::string constructor, this is not what you requested.

You requested a pointer to an existing std::string object. By definition, what is passed to your function should be the address of an existing std::string (or stream) object.

You should understand pointers as a separate type - your function accepts a pointer-to-std::string object. While std::string can be obtained using a pointer to std::string , the pointer itself is not std::string , and it cannot be "converted" to std::string , and it cannot be considered as a pointer to a pointer, char (or vice versa).

The simplest option is a constant reference to std::string ( const std::string & ). const, in this case, because you are not doing anything to change the line. If you were, that would be a different matter, and you would need to carefully consider whether you want the caller to visit your changes.

Having done this, you say that you want the std::string object (remember, the reference to the object is an object, see C ++ FAQ 8.5 in particular), which allows the compiler to call the appropriate constructor to create std :: string for you, when the function is called using char * (const or not).

At the same time, if someone passes you the actual std::string , the constructor is avoided, and you get the same efficiency as if you took pointer-to-std::string . Win-win.

As an alternative, of course, you can just take a simple std::string , but in this case you always get a copy of the transmitted string, whether it be a C-string or std::string . Sometimes it is desirable, sometimes not. In your case, you do nothing except print a line, making unnecessary overhead.

+2
source

You can achieve this by changing the prototype to:

 void sillyFunction(string const &str, int cool=0); 

char const * can be implicitly converted to a temporary std::string , which, in turn, can be passed by reference ( std::string const & ). There is no implicit conversion to a pointer ( std::string * ), so you get an error.

+1
source

You can convert from const char * to string , but not to string * .

Perhaps you want your sillyFunction accept a constant reference?

 void sillyFunction(const string &str, int cool){ counter++; if (cool){ for (int i=0; i<counter; i++) cout << str << endl; } else { cout << str << endl; } } 
+1
source

Must be

 void sillyFunction(const string& str, int cool=0); 
+1
source

If you intend to use pointers for your functions, you must declare strings somewhere. memory should be allocated. therefore, either declare a variable or call new to create memory for the string.

 int main(){ string str = "Cool"; string str2 = "Cooler"; sillyFunction(&str); sillyFunction(&str2, 1); return 0; } 
+1
source

I got a different simple solution for this using string copying

char s [20] strcpy (s, const char * p);

ten u got the pointrd string to * p in s ... it works.

0
source

All Articles