没有已定义函数的匹配函数

时间:2014-07-06 17:38:28

标签: c++ string

我有一个代码,由于错误我无法编译,代码"error: no matching function for call to 'asd'"中的if (asd( checkline[i], alphabet)==true)。我不知道我做错了什么,因为asd()是在exname()之前定义的。感谢您的帮助。

#include <string>
#include <iostream>
const std::string alphabet="йцукенгшщзхъфывапролджэёячсмитьбю";
using namespace std;

bool asd(string two, string one)
{
    size_t pos=one.find(two);
    if(pos!= string::npos)
    {
        return true;
    }
    return false;
}

string exname(string checkline)
{
    string newstr;

    for (int i = 0; i < checkline.length(); ++i)
    {
        if (asd( checkline[i], alphabet)==true)
        {
            /* code */
        }
    }
    return newstr;
}

4 个答案:

答案 0 :(得分:4)

您将char中的checkline[i]视为字符串

bool asd(string two, string one)
//        ^^^^^ make this char
{
   //.....

}

答案 1 :(得分:0)

编译器正在寻找bool asd(char, string),您只定义了bool asd(string, string)。您需要修改asd的定义或更改您的调用方式。

答案 2 :(得分:0)

在函数调用中

if (asd( checkline[i], alphabet)==true)

第一个参数的类型为char,而函数的定义方式使其第一个参数的类型为std::string。类char中的对象没有隐式转换为类std::string中类型为std::string的对象。 因此,编译器会发出错误,因为它找不到名为asd的函数,该函数的第一个参数类型为char ..

答案 3 :(得分:0)

对函数调用asd函数的第一个参数可以更改为char,如前所述,可以使用 substr 来使用checkline创建仅包含相关字符的字符串的方法。我做的其他更改:1)在&#34之后定义const字符串;使用命名空间std&#34;,2)从const字符串的前面删除不必要的std :: 3)是int()cast checkline.length( )消除不必要的警告信息:

#include <iostream>
#include <string>

using namespace std;

const string alphabet="йцукенгшщзхъфывапролджэёячсмитьбю";

bool asd(string two, string one)
{
    size_t pos=one.find(two);
    if(pos != string::npos)
    {
        return true;
    }
    return false;
}

string exname(string checkline)
{
    string newstr;
    for (int i = 0; i < int(checkline.length()); ++i)
    {
        if (asd( checkline.substr(i, 1), alphabet) == true)
        {
            /* code */
        }
    }
    return newstr;
}
相关问题