Errors in template class

I am trying to get it to work but I seem to be doing something wrong in the main. Both of my classes are correct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# include <iostream>
# include <string>
# include <vector>
# include <fstream>
# include <sstream>
# include <cmath> 

using namespace std;

 template < typename T>
 class Deque;

 template < typename T>
class Queue {
   vector <T>* elements;
   int frontSpace;
   int rearItem;
   int maxAdds;
   
   friend class Deque<T>;
   public:
   Queue(int maxAdds) :

   elements ( new vector <T> (maxAdds)),
   frontSpace  (0),
   rearItem (0),
   maxAdds( maxAdds)
   { }
   virtual bool isEmpty() { return frontSpace == rearItem; }
   virtual bool isFull() { return frontSpace == maxAdds; }
   virtual int size() { return frontSpace - rearItem; }
   virtual void pushFront(string newItem) {
      elements->at(frontSpace++) = newItem;
   }
   virtual T popRear() {
      if (isEmpty()) {
       cerr <<"Later we'll define and throw an EmptyQException" << endl;
       return "";
      }
        else
       {
         return elements -> at (rearItem++);
       }
   }
   virtual T toString()
     {
      T out("");
      for (int i = rearItem; i < frontSpace; i++)
     {
         out += elements ->at(i) + " ";
      }
      return out;
   }
};	
  template <class T>
  class Deque: public Queue<T>  {     
          friend class Queue<T>;	

  public: Deque<T> (int maxAdds) : Queue<T> (2*maxAdds)  
          {this->frontSpace = maxAdds;    
           this->rearItem = maxAdds ;			
          }  



          virtual void pushRear(T newItem) {
          if (this -> isFull()){
             cerr << "later we'll define and throw a FullDQException" << endl;
             cerr << this->toString() << endl;
          } else {					
            this-> elements->at(--(this->rearItem)) = newItem;
          }
        }   

          virtual T popFront() {	
          if (Queue<T>::isEmpty()) {
          cerr <<"Later we'll define and throw an EmptyQException" << endl;
          return "";
          }
           else {
          return this->elements -> at (--this->frontSpace);
          }
          } 

          virtual bool isFull() const {
          return(Queue<T>::isFull() || this -> rearItem ==0);
         
      }    
        
    
		

    }; 
  bool ed1(const string& x, const string& y){
     int xlen = x.length();
     int ylen = y.length();
     if (abs(xlen - ylen) >= 2) {return false;}	
     if (xlen == ylen) {
        int count = 0;
        for (int i = 0; i < xlen; i ++) {
           if (x.at(i) != y.at(i)) {count++;}
        }  
        return (count == 1);
     }
     else if (xlen == ylen +1) {
        for (int i= 0; i < ylen; i++) {
           if (x.at(i) != y.at(i)) {
              return (x.substr(i+1)== (y.substr(i)));
           } 
        } 
        return true;
     }

     else if (ylen == xlen +1) {
        for ( int i = 0; i< xlen; i++){
           if (y.at(i) != x.at(i)) {
              return( y.substr(i+1)==(x.substr(i)));  
           } 
        }  
        return true;
    }
}  
  
int main () {

   Queue* sq = new Queue(100);
   sq->pushFront("oh");
   sq->pushFront("say");
   sq->pushFront("can");
   sq->pushFront("you");
   sq->pushFront("see");
   cout << (sq->toString())<< endl;
   T pop2 = sq->popRear() + " " + sq->popRear();
   cout << (pop2 + ", did this print in the right order?")<< endl;
   sq->pushFront("me?");
   cout <<("Final queue: " + sq->toString())<< endl;

  Deque* sd = new Deque(100); 
   T infileName = "words.txt";    
   ifstream* INFILEp = new ifstream(infileName.c_str(), ios_base::in);
   T word;
   while((*INFILEp) >> word)    {
     if (sd->isEmpty()) {
        sd-> pushFront(word);
     }  
     else {
        frontWord = sd->popFront();
        sd->pushFront(frontWord);
        rearWord = sd->popRear();
        sd->pushRear(rearWord);
        if(ed1(word , frontWord)) {
cerr << "Pushing " << word << " on front " << endl;
           sd->pushFront(word);
        }
        else if(ed1(word , rearWord)) {
cerr << "Pushing " << word << " to rear " << endl;
           sd-> pushRear(word);
        }
        else if(ed1(word, rearWord) && ed1( word , frontWord)) {
           sd->pushFront(word);
        } 
        else {
        }  //don nothing, just skip word
     }

  }
  cout << "Final contents: " << sd->toString() << endl;
   
  return 0;
}

I keep getting these errors:

In function ‘int main()’:
141: error: missing template arguments before ‘*’ token
141: error: ‘sq’ was not declared in this scope
141: error: expected type-specifier before ‘Queue’
141: error: expected `;' before ‘Queue’
148: error: ‘T’ was not declared in this scope
148: error: expected `;' before ‘pop2’
149: error: ‘pop2’ was not declared in this scope
153: error: missing template arguments before ‘*’ token
153: error: ‘sd’ was not declared in this scope
153: error: expected type-specifier before ‘Deque’
153: error: expected `;' before ‘Deque’
154: error: expected `;' before ‘infileName’
155: error: ‘infileName’ was not declared in this scope
156: error: expected `;' before ‘word’
157: error: ‘word’ was not declared in this scope
162: error: ‘frontWord’ was not declared in this scope
164: error: ‘rearWord’ was not declared in this scope
Whenever you use a template you have to specify the template parameters. The right way would be Queue<string>* sq = new Queue<string>(100);. The same for Deque.
In some cases, template parameters can be deduced. This is not one of those cases, however.
When you write template<typename T> the T is only a valid identifier inside the class/function. In main this T is not known. Write string.
Is there a way for me to use the T inside the int main?
T is not a type, it's just a placeholder for a type. You need to specify what type you're talking about, as hamsterman said.
You could create another template function..
1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
void this_is_totally_not_main(){
   Queue<T>* sq = new Queue<T>(100);
   //...
   T word;
   //...
}

int main(){
   this_is_totally_not_main<string>();
   return 0;
}
Topic archived. No new replies allowed.