D
D
Danil Vaskevich2021-06-09 10:53:56
C++ / C#
Danil Vaskevich, 2021-06-09 10:53:56

How to fix c++ class constructor error?

I have a concatenation function which returns my own String class. But when I call it in main with the copy constructor, it throws the error "no instances of the 'String::String' constructor matching the argument list." The code is attached.

Call line in main: The copy constructor itself:
String res(str.concat(str1));

String::String( String& str) {
  size_t size = strlen(str.string_) + 1;
  delete[]str.string_;
  str.string_ = new char[size];
  strcpy_s(str.string_, size, str.string_);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-06-09
@DaniVffgf

Give the entire compiler message in its entirety. It clearly states which constructor he is looking for. It seems that if you look closely at it, you will notice that the compiler needs a constructor with const String&, when, as you have, only String&.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question