簡體   English   中英

在Visual Studio 2010上但不在VS 2015上編譯程序

[英]Program compiling on Visual Studio 2010 but not on VS 2015

我正在研究幾年前用VS 2010用C ++編寫的軟件,現在我想編譯它時會顯示錯誤。 我精確地說,如果您使用VS 2010,它仍然可以工作,但我只有2015年。

我編寫了一個簡單的代碼向您展示該錯誤,它涉及一個模板類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);
}

當我嘗試編譯它時,它在操作符()返回時向我顯示錯誤C3867C2100 但是這些似乎目前沒有任何理由彈出:_Myfirst是向量類的屬性,應該沒問題。

如何解決此問題(實際文件超過3000行,出現600個錯誤,始終為C3867和C2100),我可以在VS 2015和VS 2010之間以某種兼容模式工作嗎?

謝謝。

您的代碼依賴於std::vector類(即_Myfirst成員)的內部實現細節。

在VS2010和VS2015之間更改了std::vector的實現,因此_Myfirst是VS2010中的簡單指針,但是一個成員函數,該成員函數返回對VS2015中的指針的引用。

您應該編寫代碼,以便tab1D類僅使用公共的非內部接口連接std::vectorAPP_LINUX塊中的代碼可能已經存在)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM