I have this code below and I’m getting the error upon compilation:
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;
}
}
Don’t take your parameter in as a
string *try just using aconst string &insteadEDIT:
std::stringandconst char*are different types. thestd::stringalready has a conversion from string literals (ex:"Cool") to the actual string object. So by passing in the string literal"Cool"you are in a sense passing in astd::stringobject, not a pointer to one.The reason I chose to use a
const string &is mostly from personal coding practices. This minimizes stack memory usage, and since you are passing in a constant string literal, there is no need for the parameter to be modify-able.Also don’t forget if you change from a
string *that you no longer need to dereference it in yourcout: