【问题标题】:Don't understand Insert function too well, getting an error I can't seem to get past不太了解插入函数,出现错误我似乎无法通过
【发布时间】:2013-05-13 00:27:47
【问题描述】:

我正在尝试将用户输入的名称插入到预先存在的字符串中,但我不断收到一条错误消息:

"[错误] 没有匹配函数调用 'std::basic_string::insert(std::basic_string::iterator, std::string&)'" 第 25 行

代码:

#include <iostream>
#include <string>

using namespace std;
string custName = " ";
string firstName = " ";
string midInitial = " ";
string lastName = " ";
string getFullName = "";
string comma = ",";

int getName()
{
    cout<<"Please enter your first name \n";
    cin>>firstName;
    cout<<"Please enter your middle initial \n";
    cin>>midInitial;
    cout<<"Please enter your surname \n";
    cin>>lastName;

    getFullName = firstName+" "+midInitial+". "+lastName;
}

int addName()
{
    custName.insert (custName.end(), ',');
    custName.insert(custName.end(), getFullName);
}

int main ()
{
    getName();
    custName = getFullName;
    getName();
    addName();
    getName();
    addName();
    getName();
    addName();
    cout<<custName
}

【问题讨论】:

标签: c++ insert


【解决方案1】:

std::string::insert 没有函数签名,例如:

basic_string& insert( iterator pos, const basic_string& str );

更新

custName.insert(custName.end(), getFullName);

到:

custName.insert(custName.end(), getFullName.begin(), getFullName.end());

或

custName.insert(distance(custName.end(), custName.begin()), getFullName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多