Template for STL in .h File: Error Fix

  • Comp Sci
  • Thread starter anonim
  • Start date
  • Tags
    C++
In summary, the conversation is about a code written in a .h file that involves creating a template class called BAdapter which inherits from another class called AbstractB. The BAdapter class has a constructor and a function called setSize that resizes a board and resets it. It also has a virtual function called loadGame that takes in a file name, a player character, and a game mode and loads the game accordingly. The code also includes a variable called Board which is of type T<T<char>> and a function called clear() which clears the board. However, the conversation ends with an error message about T not being a template and a suggestion to declare T somewhere.
  • #1
anonim
40
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
  • #2
Maybe declare T somewhere?
 

1. What is a template for STL in .h file?

A template for STL in .h file is a pre-defined structure or format used to organize and store data in a Standard Template Library (STL) container. This template can be used to create various types of containers, such as vectors, lists, and maps, which can then be used to store data in a specific way.

2. How can I fix errors in a template for STL in .h file?

To fix errors in a template for STL in .h file, you can start by identifying the specific error and its cause. Common errors in templates for STL include syntax errors, missing headers, and type mismatch. Once you have identified the error, you can make the necessary changes to the template code or include the missing headers to resolve the issue.

3. What are the benefits of using a template for STL in .h file?

Using a template for STL in .h file offers several benefits, including code reusability, improved efficiency, and reduced development time. Templates allow you to create generic code that can be used for different data types, making it easier to maintain and modify your code. Additionally, STL containers are highly optimized for performance, making them a preferred choice for data storage and manipulation.

4. Can I customize a template for STL in .h file?

Yes, you can customize a template for STL in .h file to suit your specific needs. Templates are designed to be flexible and allow for customization, such as specifying the data type, size, and other parameters. You can also create your own custom templates to meet your unique requirements.

5. Are there any best practices for using a template for STL in .h file?

Yes, there are some best practices to keep in mind when using a template for STL in .h file. These include using descriptive names for your templates, avoiding unnecessary code duplication, and properly documenting your code. It is also recommended to test your templates with different data types to ensure they are functioning correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
984
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
896
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top