Template for STL in .h File: Error Fix

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The forum discussion addresses a compilation error encountered in a C++ template class implementation within a header file. The specific error message, ‘T’ is not a template, arises from the incorrect declaration of the member variable Board as T>. The solution involves changing the declaration to T, assuming T is a type that can be instantiated without additional template parameters. This adjustment resolves the compilation issue and allows the template class BAdapter to function correctly.

PREREQUISITES
  • Understanding of C++ template programming
  • Familiarity with class inheritance in C++
  • Knowledge of file handling in C++ using fstream
  • Basic understanding of the Standard Template Library (STL)
NEXT STEPS
  • Review C++ template syntax and usage
  • Learn about class member initialization in C++
  • Explore error handling techniques in C++ file I/O
  • Investigate the use of STL containers for dynamic data structures
USEFUL FOR

C++ developers, software engineers working with templates, and anyone troubleshooting compilation errors in C++ code.

anonim
Messages
39
Reaction score
2
Homework Statement
BAdapter: This class takes a template parameter and behaves like an adaptor class just like the stack or queue class of the STL. The parameter classes can be any STL class with a random access iterator.
Relevant Equations
-
in .h file I write like this:

C++:
namespace board{
    template <class T>
    class BAdapter : public AbstractB{
        public:
            BAdapter();
            BAdapter(int x){
                x=6;
                setSize(x);
            }
            virtual void setSize(int receivedSize){
                int i,j;
                Board.clear();
                size=receivedSize;
                Board.resize(getSize());
                for(i=0; getSize()>i; i++){
                    Board[i].resize(getSize());
                }
                reset();
            }
      
            virtual bool loadGame(string fileName,char player,int game_mode){
                fstream file;
                int size2;
                int i,j;
                string line;
                bool flag=false;
                char state;

                file.open(fileName);
        
                if(file.is_open()){
                    file>>size2;
                    file>>player;
                    Board.clear();
                    Board.setSize(size2);
                    for(i=0; size2>i; i++){
                        file>>line;
                        for(j=0; size2>j; j++){
                            if(line[j]=='.'){
                                state='.';
                            }
                            else if(line[j]=='x'){
                                state='x';
                            }
                            else if(line[j]=='o'){
                                state='o';
                            }
                            Board[i][j].setRow(i);
                            Board[i][j].setColumn(j);
                            Board[i][j].set_cellState(state);
                        }
                    }
                    flag=true;
                    size=size2;

                    file.close();
                
                }
                 return flag;
            }
        protected:
            T<T<char>>Board;

    };
}

Actually, I did not understand exactly what I was expected to do, but I get an error.
Error is:
‘T’ is not a template
T<T<char>>Board;
 
Last edited by a moderator:
Physics news on Phys.org
Maybe declare T somewhere?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K