no matching function for call to ‘std::basic_string<char>::assign(std::string&, int)

使用string中的assign赋值函数报错,代码为:

text0.assign(line,i+1);
其中text0与line都为string类型

最后发现assign函数的原型为

string &assign(const char *s,int n);

将代码改为以下即可

text0.assign(line.c_str(),i+1);

附  assign函数

string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

及  char*、char[]与string转换方式

1.  string --> char *

char *     =    string.c_str;

2.  char * --> string

//可以直接赋值。

string s;
char *p = "abcdef";
s = p;

不过,变为string后使用printf输出会出现问题,用cout没有问题。是因为“%s”要求后面的对象的首地址。但是string不是这样的一个类型。

3. string --> char[]

不可直接赋值,使用循环赋值

    string pp = "dagah";
    char p[8];
    int i;
    for( i=0;i<pp.length();i++)
        p[i] = pp[i];
    p[i] = ‘\0‘;
    printf("%s\n",p);
    cout<<p;

4.  char[] --> string

可以直接赋值,但会出现2中的问题

参考:  http://blog.csdn.net/cogbee/article/details/8931838

时间: 2024-08-07 11:16:03

no matching function for call to ‘std::basic_string<char>::assign(std::string&, int)的相关文章

error LNK2019: 无法解析的外部符号 &quot;class std::vector&lt;class std::basic_string&lt;char,struct std::char_traits&lt;cha

error LNK2019: 无法解析的外部符号 "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class

使用log4cplus时遇到的链接错误:无法解析的外部符号 &quot;public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string&lt;wchar_t,struct std::char_traits&lt;wchar_t&gt;,

#include "stdafx.h" #include <log4cplus/logger.h> #include <log4cplus/loggingmacros.h> #include <log4cplus/configurator.h> #include <log4cplus/fileappender.h> #include <log4cplus/win32debugappender.h> #include <l

iOS出现 Undefined symbols for architecture armv7 std::basic_string&lt;char, std::char_traits&lt;char&gt;

Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) 如果真机调试就是 undefined symb

实战c++中的string系列--std:vector&lt;char&gt; 和std:string相互转换(vector to stringstream)

有时候也会遇到std:vector与转std:string 相互转换的情况. 首先看一下vector<char>如何转string: std::vector<char> *data = response->getResponseData(); std::string res; //方法一 for (int i = 0;i<data->size();++i) { res+=(*data)[i]; } res+='\0'; std:cout << res;

undefined reference to `std::__cxx11::basic_string&lt;char,

centos上编译报错,部分信息如下: /usr/local/lib/libprotobuf.so.9: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find_first_of(char, unsigned long) [email protected]_3.4.21' /usr/local/lib/lib

std::vector中assign resize reserve的区别

1.assign 用于初始化 2.resize 改变capacity,与size 3.reserve 改变capacity,不改变size 超过size的capacity不可直接访问,可通过push_back追加,若size < capacity, push_back操作不尽心内存分配. 测试代码: #include <stdio.h> #include <iostream> #include <vector> #include <string> vo

error: no matching function for call to &#39;std::exception:exception(const char[16])&#39;

环境:codeblocks 语言:C++ 在执行:throw new exception("queue is empty.");时 遇到问题:error: no matching function for call to 'std::exception:exception(const char[16])' 解决办法:修改为 std::logic_error e("xxx."); throw std::exception(e); error: no matching

[c++]no matching function for call to ‘sort(…)......

[问题] 在做LeetCode的Merge Intervals时用到c++的sort函数,一直出这个错误,甚是郁闷.最后终于找到了问题所在. [代码] #include <iostream> #include <algorithm> #include <vector> using namespace std; struct Interval { int start; int end; Interval() : start(0), end(0) {} Interval(i

C++异常:no matching function for call to "Matrix(Matrix&amp;)"

C++异常:no matching function for call to "Matrix(Matrix&)" 我定义了一个类叫Matrix,其中构造函数explicit Matrix(const Matrix& source); 也写了一个方法: Matrix Matrix::myFun(const Matrix &source){  ...    return *this;} 编译报出上面的异常来,原因是explicit关键字抑制隐式转换,当我返回*thi