0

It might be a stupid question but I totally don't have any idea about the prompt.

The code is in a .cpp file

template <typename T> void foo2(T){}
template void foo2<int>(int);

VS2017 keeps telling me that: Function definition for function 'foo2' is not found. However, the code actually works in VS2017, no error message if I run it. I don't know if it is an IDE-specific problem or it is the code problem. As it is quite annoying, does anyone know why the prompt appears and how to fix it? Thanks!

=========Update===========

Here is the full code(Move the instantiation to .h file but still have the same problem):

test.h

#pragma once
template <typename T> void foo2(T);
template void foo2<int>(int);

test.cpp

#include "test.h"
#include "stdafx.h"
template <typename T>void foo2(T){}

main file

#include "stdafx.h"
#include "test.h"

int main()
{
    int a = 1;
    foo2(a);
}

I tend to believe that an IDE-specific question. If I ask VS to show the potential fix, it will create the following code in .cpp file:

template void foo2(int)
{
    return template void();
}

which is definitely wrong. Even cannot pass the compilation.

Jeff
  • 113
  • 1
  • 8
  • Is `foo2` forward declared? Templates need to be fully defined before they're instantiated, so that the compiler knows how to instantiate at all. See https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – alter_igel Sep 17 '18 at 03:21
  • Thanks for your reply, I did the declaration in the corresponding .h file and the header has been included in the .cpp file. The main function is in a separate .cpp files and foo2's .h is included. These three are the only files in the project. In fact, even if I put the instantiation in the .h file, I will get the same prompt. I asked VS to fix it, VS will create an empty function definition for it. I don't understand how this could happen.. – Jeff Sep 17 '18 at 04:23
  • Please include all your files in the question to help illustrate. Your full template _definition_ needs to be available _in_ or _included from_ the header file. – alter_igel Sep 17 '18 at 04:47
  • The code is OK, the IDE is acting funny. Ask Microsoft to fix it. – n. m. could be an AI Sep 17 '18 at 08:07
  • 1
    I'm pretty sure that the actual error message is different. MSVC may have some minor issues with templates, but it 100% certainly knows that `foo2` is **not** a function, so it won't complain that its definition can't be found. Now `foo2` is a function missing a definition, so I can see MSVC complain about that. – MSalters Sep 17 '18 at 08:15
  • @alterigel I have posted all the code, it gives me the same prompt even I instantiate the template in .h file. The potential fix from VS is also posted. Thx. – Jeff Sep 18 '18 at 09:23
  • @MSalters Well, if that is the case, it is hard for me to believe they can make the instantiation of the template class correct... – Jeff Sep 18 '18 at 09:27

1 Answers1

0

With the expanded code, I get the idea.

You actually want extern template void foo2<int>(int); in the .h file. There should be only one instantiation, in test.cpp. extern template is new in C++11, so while your book might not yet cover it it's certainly understood by VS2017.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks for the answer, I tried your suggestion, but got the same prompt. So sad it does not work. I have reported it to MS. Let's see how they will respond. – Jeff Sep 18 '18 at 10:52