Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用字符串调用方法时出错:初始化类型为‘;的非常量引用无效;尺寸(t& ;;_C++_String - Fatal编程技术网

C++ 使用字符串调用方法时出错:初始化类型为‘;的非常量引用无效;尺寸(t& ;;

C++ 使用字符串调用方法时出错:初始化类型为‘;的非常量引用无效;尺寸(t& ;;,c++,string,C++,String,我正在尝试调用该方法 bool someMethod(char const * begin, char const * end, size_t & count); 只有一根绳子的 std::string sString = "test"; if (someMethod(....)) { std::cout << "working!"; } std::string sString=“test”; if(someMethod(…){ std::cout这是

我正在尝试调用该方法

bool someMethod(char const *    begin, char const *     end, size_t & count);
只有一根绳子的

std::string sString = "test";
if (someMethod(....)) {
   std::cout << "working!";
}
std::string sString=“test”;
if(someMethod(…){
std::cout这是一个“腰带和吊带”函数签名,因为它同时要求结束指针和字符计数

如果你是作为练习来做的,你需要知道以下几点才能完成:

  • 通过调用
    C_str()
  • 通过调用
    size()
  • 有了初始指针和大小,就可以通过将两者相加得到结束指针
请注意,由于大小引用是非常量的,因此在进行调用之前需要将其放入变量中:

size_t count = s.size();
const char *start = s.c_str();
someFunction(start, start+count, count);

可能会有帮助。欢迎这么做,亚历克斯姆!-你到底尝试了什么,你犯了什么错误?请相应地编辑你的问题。咨询作者。只有他们知道为什么要这样做(对我来说是多余的)
count
参数参考。感谢您的快速评论和提示。我编辑了这个问题。我怀疑
count
只是一个“out”参数,返回
begin
end
之间的“used”字符数,但您应该查阅文档、作者或代码以确定答案。太棒了!非常感谢!再问一个问题:我是否必须通过delete myPointer使用*start指针后删除它;//释放内存myPointer=NULL;//指向悬挂ptr为NULL?@AlexM,因为您通过
c_str()获得指针)
不是字符串内部缓冲区的副本,而是内部缓冲区本身,您不应该对从
c\u str
获取的指针调用
delete
。字符串将在销毁时调用delete。
error: invalid initialization of non-const reference of type ‘size_t& {aka long unsigned int&}’ from an rvalue of type ‘std::basic_string<char>::size_type {aka long unsigned int}’
size_t count = s.size();
const char *start = s.c_str();
someFunction(start, start+count, count);