【发布时间】:2011-08-25 05:21:35
【问题描述】:
#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 不,那个代码是“我的”,我想了解错误的原因。