2014-11-24 36 views
1

我想在類中定義模板成員函數,並且每次嘗試構建此代碼時MSVC崩潰。我不確定這是否是Visual Studio 2008中的一個錯誤。下面是一個簡單的例子。微軟的C++優化編譯器已停止工作

testTemp.h頭文件:

#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); 
}; 

這裏是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; 
    } 

和stdafx.h中是:

#pragma once 

#include "targetver.h" 

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

我在2008年MSVC運行此,每當我嘗試構建此代碼,我得到以下崩潰: enter image description here

+0

stdafx.h中是否有任何內容? – 2014-11-24 20:17:40

+0

@Tim請參閱編輯 – Samer 2014-11-24 20:19:06

+3

是的。編譯器不應該*崩潰*,獨立代碼的好壞。也許重新安裝等幫助。 – deviantfan 2014-11-24 20:19:08

回答

5

模板變量在C++ 14中是新增的。 VS2008當然不會實現它們。

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

可能應

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; 
}; 

我建議,因爲這直列實現:Why can templates only be implemented in the header file?


PS你可以報一個編譯器錯誤,但他們不會修復那個舊版本。

+2

即使在C++中,類範圍中的14個變量模板也必須是靜態的。 – 2014-11-24 21:02:26

+1

@Tim我今天又學到了一些新東西:)它當然有很多意義,但我從未使用過模板變量 – sehe 2014-11-24 21:30:55