C++无法将“const char*”转换为“std::string*”

bprjcwpo  于 2023-02-06  发布在  其他
关注(0)|答案(8)|浏览(678)

我有下面的代码,我得到编译时的错误:
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;
    }
}
46qrfjad

46qrfjad1#

不要将您的参数作为string *,尝试使用const string &代替
编辑:
std::stringconst char*是不同的类型。std::string已经有从字符串文字的转换(例如:"Cool")到实际的字符串对象,所以通过传入字符串字面量"Cool",在某种意义上你传入的是一个std::string对象,而不是指向一个对象的指针。
我之所以选择使用const string &,主要是因为我个人的编程习惯,这样可以最小化堆栈内存的使用,而且由于传入的是常量字符串,所以不需要参数是可修改的。
另外,不要忘记如果您从string *进行更改,则不再需要在cout中解引用它:

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

woobm2wo2#

为了解释问题到底是什么...
虽然编译器很乐意通过适当的std::string构造函数将char */C-string“转换”为std::string,但这并不是您所要求的。
您请求了一个指向 existingstd::string对象的 * 指针 。根据定义,传递给函数的必须是已经存在的std::string(或后代)对象的地址。
您必须将指针理解为一种独特的类型--您的函数接受pointer-to-std::string“对象”。虽然可以通过指向std::string的指针访问std::string,但指针本身 * 不是 * std::string,也不能“转换”为std::string,也不能将其视为指向char的指针(反之亦然)。
在本例中,最简单的替代方法实际上是对std::stringconst std::string &). const的const引用,因为您没有做任何修改字符串的操作,如果您做了,那就是另一回事了,您必须仔细考虑是否希望调用者看到您的更改。
通过这样做,您可以说您需要一个std::string对象(记住,对object * 的引用就是那个object
,请特别参阅C++ FAQ 8.5),它允许编译器在使用char *(const或not)调用函数时调用适当的构造函数来为您创建一个std::string。
同时,如果有人传递给你一个 actualstd::string,构造函数就被避开了,你得到的效率就像你得到了一个pointer-to-std::string一样。
当然,你也可以只取一个普通的std::string,但是在这种情况下,你总是会得到一个传入字符串的副本,不管它是一个C字符串还是一个std::string。有时候这是可取的,有时候不是。在你的例子中,你除了打印字符串之外什么都不做,这样就不需要额外的开销了。

ryevplcw

ryevplcw3#

变化

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

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

oxf4rvwz4#

您可以从const char *转换为string,但不能转换为string *
也许您希望您的sillyFunction采用const引用?

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

sr4lhrrt5#

应该是

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

6ie5vjzr6#

如果你要为你的函数使用指针,你必须在某处声明字符串。内存需要被分配。所以要么声明一个变量,要么调用new来为字符串创建内存。

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

ghhkc1vu7#

您可以通过将原型更改为以下内容来实现此目的:

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

char const *可以隐式转换为临时的std::string,而临时的std::string又可以通过引用(std::string const &)传递。没有隐式转换为指针(std::string *),这就是为什么会出现错误。

ylamdve6

ylamdve68#

我有一个非常简单的解决方案,使用字符串复制
字符s[20]字符串(s,常数字符 *p);
然后你有了s中 *p所指的字符串...它起作用了...

相关问题