0

Trying to build function that gets unknown number of arguments.

Header.h:

void WriteHeaderToCSVFile() {}

template <typename FieldType, typename... Types>
void WriteHeaderToCSVFile(FieldType field1, Types...field2);

Source.cpp:

template <typename FieldType, typename... Types>
void CSVWriter::WriteHeaderToCSVFile(FieldType field1, Types... field2)
{
    mFileStream->Write(field1);
    mFileStream->Write(scComma);
    WriteHeaderToCSVFile(field2...);
}

void main()
{
   myFile2.WriteHeaderToCSVFile(L("no.record"), L("no.page"), L("values"));
}

Getting the error bellow:

error LNK2019: unresolved external symbol "public: void __cdecl CSVWriter::WriteHeaderToCSVFile<wchar_t const *,wchar_t const *,wchar_t const *>(wchar_t const *,wchar_t const *,wchar_t const *)" (??$WriteHeaderToCSVFile@PEB_WPEB_WPEB_W@XMPCSVWriter@@QEAAXPEB_W00@Z) referenced in function "void __cdecl TestCSVFileWriter(void)" (?TestCSVFileWriter@@YAXXZ)

1 Answers1

0

I suppose the problem is that you need an "empty" overload for WriteHeaderToCSVFile() as ground case for the recursion; I mean

void CSVWriter::WriteHeaderToCSVFile ()
{ }

I mean... myFile2.WriteHeaderToCSVFile(L("no.record"), L("no.page"), L("values")); call myFile2.WriteHeaderToCSVFile(L("no.page"), L("values")); that matches the variadic method (L("no.page") matches field1, L("values") matches field2...)

Then myFile2.WriteHeaderToCSVFile(L("no.page"), L("values")); call myFile2.WriteHeaderToCSVFile(L("values")); that matches the variadic method (L("values") matches field1, field2... matches as empty).

The myFile2.WriteHeaderToCSVFile(L("values")); call myFile2.WriteHeaderToCSVFile(); but nothing matches field1, for the variadic method.

So you need another WriteHeaderToCSVFile() that can match an empty list of argument.

Off Topic Unrequested Suggestion: define your templates (classes, functions, methods, variables, etc.) directly in the header files where they are declared.

The reason is well described in here

max66
  • 65,235
  • 10
  • 71
  • 111
  • Thank you Tried that and now the error is: error LNK2019: unresolved external symbol "public: void __cdecl CSVWriter::WriteHeaderToCSVFile(int,int,int)" Change the call in main to: myFile2.WriteHeaderToCSVFile(1,2,3); – טל היינה Feb 14 '22 at 11:47
  • @טלהיינה - Sorry but, without a complete example (possibly minimal), to reproduce the error, is very difficult to say where the problem is. – max66 Feb 14 '22 at 13:09