【问题标题】:No instance of overloaded function matches argument list.没有重载函数的实例与参数列表匹配。
【发布时间】:2016-04-22 03:45:53
【问题描述】:

我正在处理一个类项目,但不断收到错误消息:没有重载函数的实例与参数列表匹配。它引用了我的 String 类。我想要做的是创建一个 Copy、Concat 和 Count 函数而不使用字符串类。任何帮助将不胜感激。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>

using namespace std;

class String
{
private:
char str[100]; 
char cpy[100];
public:

static const char NULLCHAR = '\0';

String()
{
    str[0] = NULLCHAR;
    cpy[0] = NULLCHAR;
}

String(char* orig, char* cpy)
{
    Copy(orig, cpy);
}

void Display()
{
    cout << str << endl;
}

void Copy(char* orig, char* dest)
{

    while (*orig != '\0') {
        *dest++ = *orig++;
    }
    *dest = '\0';



}

void Copy(String& orig, String& dest) 
{
    Copy(orig.str, dest.cpy);
}

void Concat(char* orig, char* cpy)
{
    while (*orig)
        orig++;

    while (*cpy)
    {
        *orig = *cpy;
        cpy++;
        orig++;
    }
    *orig = '\0';

}

void Concat(String& orig, String& cpy)
{
    Concat(orig.str, cpy.cpy);
}

int Length(char* orig)
{
    int c = 0;
    while (*orig != '\0')
    {
        c++;
        *orig++;
    }
    printf("Length of string is=%d\n", c);
    return(c);

}
};

int main()
{
String s;

s.Copy("Hello");
s.Display();
s.Concat(" there");
s.Display();

String s1 = "Howdy";
String s2 = " there";
String s3;
String s4("This String built by constructor");
s3.Copy(s1);
s3.Display();
s3.Concat(s2);
s3.Display();
s4.Display();


system("pause");
return 0;
}

【问题讨论】:

  • CopyConcat 的参数数量不匹配。

标签: c++


【解决方案1】:

看起来您的CopyConcat 函数都接受了两个参数,但您将它们都传递给了一个参数。如果您想将它们复制到 String 对象中,您的代码应该看起来更像:

String Copy(char* orig)
{
    // Same copy logic you have, 
    // except copy into "*this"
}

【讨论】:

  • 您是否介意稍微扩展一下“*this”。我正在尝试进行更改,但收到一条错误消息,提示“表达式必须是可修改的左值。
  • 谷歌你的错误?我能想到的最好的就是这个stackoverflow.com/questions/6008733/…
【解决方案2】:

正如错误消息所述,您的 String 类没有采用单个参数的构造函数版本。您有一个默认构造函数和一个带有两个参数的构造函数。

你需要定义一个接受单个参数并初始化str

【讨论】:

    【解决方案3】:

    String s4("构造函数构建的这个字符串"); 这个语句需要构造函数

    字符串(char *);

    【讨论】:

    • 和字符串 s1 = "你好";字符串 s2 = "那里";还需要上面的构造函数。一旦你提供,错误应该被消除。
    猜你喜欢
    • 2019-07-16
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多