-1

I am new to the C++ programming language. This code is a simple String Copy function i did in Visual Studio, but it's not working the way I expect.

// 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] != '\0')
    {
        destination[i] = source[i];

        i++;
    }
}

StringCopyFn::~StringCopyFn()
{
}

I'm getting the following error message:

no instance of constructor matches the argument list

How do I correct this error?

Wug
  • 12,956
  • 4
  • 34
  • 54
Jaff
  • 105
  • 1
  • 17

2 Answers2

2

This is the StringCopyFn constructor:

StringCopyFn::StringCopyFn()
{
}

Note that it takes zero parameters.

But based on this code:

StringCopyFn(destination, source);

It seems likely that you meant to call your StringCopy() function. It's not clear from the question why you created the StringCopyFn class.

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
1

Maybe, you have intended to something like the following:

#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] != '\0' ){
        destination[i] = source[i];

        i++;
    }
    destination[i] = '\0';
}

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 = %s\n", destination);
    scanf_s("Hello World"); 
    delete [] destination;
    return 0;
}

or

#include <cstdio>
#include <cstring>
#include <TCHAR.h>

struct StringCopyFn {
public:
    void operator()(char *destination, char *source){
        int i = 0;

        while( source[i] != '\0' ){
            destination[i] = source[i];

            i++;
        }
        destination[i] = '\0';
    }
};

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 = %s\n", destination);
    scanf_s("Hello World"); 
    delete [] destination;
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70