没有构造函数的实例与参数列表匹配

no instance of constructor matches the argument list

本文关键字:参数 列表 实例 构造函数      更新时间:2023-10-16

我是c++编程语言的新手。这段代码是我在Visual Studio中做的一个简单的字符串复制函数,但它没有按我期望的方式工作。

// main file
int _tmain(int argc, _TCHAR* argv[])
{   
    char source[] = { "Computer" };
    int sourcelength = strlen(source);
    printf("Source = %s", source);
    char* destination = new char[sourcelength];
    StringCopyFn(destination, source); // error is on this line
    printf("Source = %s", source);  
    scanf_s("Hello World"); 
    return 0;
}
// String Copy
StringCopyFn::StringCopyFn()
{
}
void StringCopy(char* destination, char* source)
{
    int i = 0;
    while (source[i] != '')
    {
        destination[i] = source[i];
        i++;
    }
}
StringCopyFn::~StringCopyFn()
{
}

我得到以下错误信息:

没有构造函数实例匹配参数列表

如何纠正这个错误?

这是StringCopyFn构造函数:

StringCopyFn::StringCopyFn()
{
}

注意它没有参数

但是基于这段代码:

StringCopyFn(destination, source);

似乎你想调用你的StringCopy()函数。从问题中不清楚为什么创建StringCopyFn类。

也许,您打算做以下事情:

#include <cstdio>
#include <cstring>
#include <TCHAR.h>
class StringCopyFn {
public:
    static void StringCopy(char *destination, char *source);
};
void StringCopyFn::StringCopy(char* destination, char* source){
    int i = 0;
    while( source[i] != '' ){
        destination[i] = source[i];
        i++;
    }
    destination[i] = '';
}
int _tmain(int argc, _TCHAR* argv[]){
    char source[] = { "Computer" };
    int sourcelength = strlen(source);
    printf("Source = %s", source);
    char* destination = new char[sourcelength+1];
    StringCopyFn::StringCopy(destination, source);
    printf("ndestination = %sn", destination);
    scanf_s("Hello World"); 
    delete [] destination;
    return 0;
}

#include <cstdio>
#include <cstring>
#include <TCHAR.h>
struct StringCopyFn {
public:
    void operator()(char *destination, char *source){
        int i = 0;
        while( source[i] != '' ){
            destination[i] = source[i];
            i++;
        }
        destination[i] = '';
    }
};
int _tmain(int argc, _TCHAR* argv[]){
    char source[] = { "Computer" };
    int sourcelength = strlen(source);
    printf("Source = %s", source);
    char* destination = new char[sourcelength+1];
    StringCopyFn()(destination, source);
    printf("ndestination = %sn", destination);
    scanf_s("Hello World"); 
    delete [] destination;
    return 0;
}