itemtype
変数や関数total
内にアクセスするにはどうすればよいですか? increment
以下のコードでは、次のようなエラーが発生します
Counter2.h: メンバー関数 'int Counter::increment(T)':
Counter2.h:28:31: エラー: 'itemtype' はこのスコープで宣言されていません
Counter2.h:36:22: エラー: 'itemtype' はこのスコープで宣言されていません
Counter2.h:36:39: エラー: 'total' はこのスコープで宣言されていません
TがCounter<T> counter;
文字列やcounter.increment()
#include<string>
//#include<cstdlib>
#include<vector>
using std::vector;
using std::string;
template<class T>
class Record{
public:
T itemtype;
int total;
};
template<class T>
class Counter{
vector< Record<T> > data;
public:
int increment(T item);
int count(T item);
void printSummary();
};
template<class T>
int Counter <T> :: increment(T item){
bool check = false;
for(int i=0; i < data.size(itemtype); i++){
if(data[i].itemtype == item){
data[i].total++;
bool check = true;
break;
}
}
if(check == false){
data.push_back(itemtype = item, total = 1);
}
}
int main(){
Counter<string> counter;
counter.increment("orange");
counter.increment("orange");
return 0;
}