程序在 Visual Studio 2010 上编译,但不在 VS 2015 上编译
Program compiling on Visual Studio 2010 but not on VS 2015
我正在开发几年前用 C++ 和 VS 2010 编写的软件,现在当我想编译它时它显示错误。我精确地说,如果你使用 VS 2010 它仍然有效,但我的工作只有 2015。
我做了一个简单的代码给你看错误,它涉及到一个模板class tab1D,它继承自vector并重新定义了“()”等运算符。
这里是简化代码:
简单的主要内容:
#include <iostream>
#include "memory_tab.h"
using namespace std;
int main() {
cout << "Hello" << endl;
tab1D<int> t (2);
cout << "Initialization works fine" << endl;
cout << t[1] << endl;
cout << "Bracket operator works fine" << endl;
cout << t(1) << endl; // this line calls parenthesis operator which is overwritten in memory_tab.h. It does not compile.
cout << "Error C3867 & C2100" << endl;
int a;
cin >> a;
return 0;
}
memory_tab.h :
//includes and stuff
template <class T>
class tab1D : public vector<T>
{
public:
// //Constructors
// /*!
// * \brief Default constructor (set nbElem and tailleMem to 0)
// */
tab1D() : vector<T>() {};
tab1D(int _nbElem) : vector<T>(_nbElem) {}; // set all elements to 0
// //Operators
T& operator() (unsigned val);
T& operator() (unsigned val) const;
};
template <class T> T& tab1D<T>::operator() (unsigned val)
{
return *(_Myfirst + val);
}
template <class T> T& tab1D<T>::operator() (unsigned val) const
{
return *(_Myfirst + val);
}
当我尝试编译它时,它在运算符 () 的 return 处显示错误 C3867 et C2100。但是这些现在似乎没有任何理由弹出:_Myfirst是向量的一个属性class,应该没问题。
我该如何解决这个问题(实际文件超过 3000 行,有 600 个错误,总是 C3867 和 C2100),我可以在 VS 2015 和 VS 2010 之间以某种兼容模式工作吗?
谢谢。
您的代码依赖于 std::vector
class 的内部实现细节,即 _Myfirst
成员。
std::vector
的实现在 VS2010 和 VS2015 之间发生了变化,因此 _Myfirst
在 VS2010 中是一个简单的指针,但在 VS2015 中是 returns 对指针的引用的成员函数。
您应该编写您的代码,以便 tab1D
class 仅使用 public,std::vector
的非内部接口(您可能已经有了APP_LINUX
块中的代码)。
我正在开发几年前用 C++ 和 VS 2010 编写的软件,现在当我想编译它时它显示错误。我精确地说,如果你使用 VS 2010 它仍然有效,但我的工作只有 2015。
我做了一个简单的代码给你看错误,它涉及到一个模板class tab1D,它继承自vector并重新定义了“()”等运算符。
这里是简化代码:
简单的主要内容:
#include <iostream>
#include "memory_tab.h"
using namespace std;
int main() {
cout << "Hello" << endl;
tab1D<int> t (2);
cout << "Initialization works fine" << endl;
cout << t[1] << endl;
cout << "Bracket operator works fine" << endl;
cout << t(1) << endl; // this line calls parenthesis operator which is overwritten in memory_tab.h. It does not compile.
cout << "Error C3867 & C2100" << endl;
int a;
cin >> a;
return 0;
}
memory_tab.h :
//includes and stuff
template <class T>
class tab1D : public vector<T>
{
public:
// //Constructors
// /*!
// * \brief Default constructor (set nbElem and tailleMem to 0)
// */
tab1D() : vector<T>() {};
tab1D(int _nbElem) : vector<T>(_nbElem) {}; // set all elements to 0
// //Operators
T& operator() (unsigned val);
T& operator() (unsigned val) const;
};
template <class T> T& tab1D<T>::operator() (unsigned val)
{
return *(_Myfirst + val);
}
template <class T> T& tab1D<T>::operator() (unsigned val) const
{
return *(_Myfirst + val);
}
当我尝试编译它时,它在运算符 () 的 return 处显示错误 C3867 et C2100。但是这些现在似乎没有任何理由弹出:_Myfirst是向量的一个属性class,应该没问题。
我该如何解决这个问题(实际文件超过 3000 行,有 600 个错误,总是 C3867 和 C2100),我可以在 VS 2015 和 VS 2010 之间以某种兼容模式工作吗?
谢谢。
您的代码依赖于 std::vector
class 的内部实现细节,即 _Myfirst
成员。
std::vector
的实现在 VS2010 和 VS2015 之间发生了变化,因此 _Myfirst
在 VS2010 中是一个简单的指针,但在 VS2015 中是 returns 对指针的引用的成员函数。
您应该编写您的代码,以便 tab1D
class 仅使用 public,std::vector
的非内部接口(您可能已经有了APP_LINUX
块中的代码)。