Defining a new Object of a Class within a Template in C++, Error: Missing Template Arguments -
have simple program, can insert string statically defined array of strings of size 20.
this program worked fine, until assigned change use templates, code (with modificaiton) support intergers or strings.
using class "shelf" in included header, can no longer declare following object int main(), "shelf book;" --as compiler tells me book has not been declared , i'm missing template arguments.
#include<iostream> #include<string> #define shelfsize 20 template<class t> class shelf{ public: shelf();//default constructor void insert(t&); private: string bookshelf[shelfsize]; int counter; }; template< class t> shelf<t>::shelf(){ for(int i=0; <shelfsize; i++) bookshelf[i]=""; counter=0; } template< class t> void shelf<t>::insert(t &booknum){ bookshelf[counter] = booknum; counter++; } int main(){ shelf book; string isbn=""; cout<<"enter isbn number wish enter array: "<<endl; getline(cin, isbn); book.insert(isbn); return 0; }
obviously, watered down program , want focus on what's giving me issue.
so said following errors:
missing template arguements before "book"; expect ";" before "book". "book" undeclared.
you need specify parameter t. templates need instantiated datatype passed parameter, so:
shelf<std::string> book;
Comments
Post a Comment