#include <iostream>
#include <vector>
using namespace std;
template <typename nameOfTheVariableTypeA,
typename nameOfTheVariableTypeB> nameOfTheVariableTypeB functionX
(nameOfTheVariableTypeA argA,
(nameOfTheVariableTypeB argB)
{
nameOfTheVariableTypeA tempArgA;
nameOfTheVariableTypeB tempArgB;
argA.push_back(22);
tempArgA = argA;
cout << "\ntempArgA: " << tempArgA[0] << "\n";
tempArgB = argB;
cout << "\ntempArgB: " << tempArgB << "\n";
return tempArgB;
}
int main()
{
functionX (12, 12.4567);
vector <int> f;
functionX (f, 12.4567);
return 0;
}
從模板書: An attempt to instantiate a template for a type that doesn't support all the operations used within it will result in a compile-time error.
實例化一個模板矢量
我爲上述代碼接收到的錯誤是:
error: request for member ‘push_back’ in ‘argA’, which is of non-class type ‘int’
error: subscripted value is neither array nor pointer
我錯過了什麼觀點?
也許代碼不應該編譯?這可能僅僅是本書正在討論的內容的一個示範。 –
@In silico不,那個代碼是「我的」,我想了解錯誤的原因。 –