没有构造函数的实例与参数列表匹配
no instance of constructor matches the argument list
我是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;
}
相关文章:
- 概念中的cv限定符需要表达式参数列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 错误 没有与参数列表匹配的重载函数"getline"实例
- std::vector 没有重载函数的实例与参数列表匹配
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 构造函数/函数声明参数列表中的统一初始化
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- C++-将具有引用的长参数列表重构为结构
- 缺少别名模板C++参数列表
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- "extern"声明以及带有和不带参数列表的类模板实例的后续定义
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 我可以使用宏自动构建参数列表吗?
- 类成员函数参数列表是否可以依赖于模板参数?