1

I am trying to define a template member function in a class, and MSVC crashes everytime I try to build this code. I am not sure if this is a bug in Visual Studio 2008. Here is a minimal example.

testTemp.h header file:

#pragma once
#include <vector>
#include <iostream>
class testTemp
{
public:
    testTemp(void);
    ~testTemp(void);
    template<typename T>
    std::vector<T> m_vMonitorVec;
    int MonitorSignal(T x, std::vector<T> vec, int len);
};

and here is testTemp.cpp:

  #include "StdAfx.h"
    #include "testTemp.h"

    testTemp::testTemp(void)
    {
    }

    testTemp::~testTemp(void)
    {
    }
    template<typename T>
    int testTemp::MonitorSignal(T inp, std::vector<T> monVec, int len)
    {

        return 0;
    }

and stdafx.h is:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

I am running this in MSVC 2008, whenever I try to build this code, I get the following crash: enter image description here

Samer
  • 1,923
  • 3
  • 34
  • 54
  • is there anything in stdafx.h? – Tim Seguine Nov 24 '14 at 20:17
  • 3
    And yes. A compiler shouldn´t *crash*, independent how good or bad the code is. Maybe reinstalling etc. helps. – deviantfan Nov 24 '14 at 20:19
  • 1
    `template std::vector m_vMonitorVec;` that looks fishy to me. I don't think you are allowed to do that for a class field. Does it work if you remove the `m_vMonitorVec` declaration? – Tim Seguine Nov 24 '14 at 20:19
  • In order to have parameterized fields it has to be a class template. You can parameterize methods but not fields. – Tim Seguine Nov 24 '14 at 20:25
  • Why don't you implement the method `MonitorSignal` in the header file? I've seen this kind of implementation cause strange behaviours in the compiler before. – user666412 Nov 24 '14 at 20:26
  • @user666412 why should I? I thought it should be ok this way – Samer Nov 24 '14 at 20:34
  • @Samer well the compiler has to be able to see the method body from every compilation unit that uses it in order to instantiate the template. This is not given if the definition is in the cpp file. If you want to define it outside of the class declaration, you should usually put that definition in the header file as well. – Tim Seguine Nov 24 '14 at 20:37
  • 1
    Why should you? Seriously? How about because your compiler is crashing and you're trying to figure out why. – indiv Nov 24 '14 at 20:43
  • @indiv isnt it suppose to work this way, I dont want to use it in the header could this functions is supposed to have a large algorithm inside it and it wont be suitable for me to put it in the header file. – Samer Nov 24 '14 at 20:46
  • @indiv , I tried putting the definition in the header and crash still exists – Samer Nov 24 '14 at 20:48
  • If it crashes, try to report it: https://connect.microsoft.com/VisualStudio – erenon Nov 24 '14 at 20:48
  • My comment already pointed out something that is definitely a syntax error in your compiler. Maybe handle that. – Tim Seguine Nov 24 '14 at 20:51

1 Answers1

5

Template variables are new in c++14. VS2008 certainly doesn't implement them.

template <typename T> std::vector<T> m_vMonitorVec;

should likely be

template <typename T>
class testTemp {
  public:
    testTemp(void) { }
    ~testTemp(void) { }
    int MonitorSignal(T x, std::vector<T> const& vec, int len) {
        return 0; 
    }
  private:
    std::vector<T> m_vMonitorVec;
};

I suggested inline implementation because of this: Why can templates only be implemented in the header file?


PS You could report a compiler bug but they won't fix that old version.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    Even in c++14 variable templates at class scope have to be static. – Tim Seguine Nov 24 '14 at 21:02
  • 1
    @Tim And I learnt something new today again :) It makes a lot of sense of course, but I didn't ever use template variables yet – sehe Nov 24 '14 at 21:30