我试图在一个类中定义一个模板成员函数,但每次我尝试构建这段代码时 MSVC 都会崩溃。我不确定这是否是 Visual Studio 2008 中的错误。这是一个最小的示例。
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>
我在 MSVC 2008 中运行它,每当我尝试构建这段代码时,我都会遇到以下崩溃:
最佳答案
模板变量是 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 您可以报告编译器错误,但他们不会修复旧版本。
关于c++ - Microsoft C++ 优化编译器已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113386/