"template requires arguments" ERROR

closed account (NCRLwA7f)
I am having some problems with a line of code.

I have set up a stack class using a template

in my header file, I have

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename myType>
class stack{
	private:
	list<myType> myStack;
	
	public:
	void push(typename list<myType>::myType v);
	void pop();
	bool empty();
	int size();
	void print(ostream& outFile);
	typename list<myType>::myType top();
};


my functions all look fine with no errors, but when I go into my main program
and do the following:

1
2
3
4
5
6
7

stack Alpha; //Use of class template 'stack' requires template arguments

//so I tried this

stack<string> Alpha; //Use of class template 'stack' requires template arguments


I still get the same error, I was under the impression that all I had to do was to put in the < > what type of data I wanted, so stack<string> A, or stack<int> B should work but I don't understand the error.


Thank you for the help



***UPDATE
adding the following code got rid of the error, but now I'm wondering if that's even the same thing I'm trying to do
 
stack<string>::stack Alpha;
Last edited on
The following does compile without errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <list>

template <typename myType>
class stack{
	private:
	std::list<myType> myStack;
	
	public:
	void push(typename std::list<myType>::value_type v) {}
	void pop() {}
	bool empty() {return true;}
	int size() {return 0;}
	void print(std::ostream& outFile) {}
	typename std::list<myType>::value_type top() {return myStack.front();}
};	

int main()
{
  stack<int> foo;
}

You, however, have something more complex, don't you?

For example, std::list does not have 'myType'.


EDIT:
Note: The interface of the class has a design flaw: it reveals unnecessary details about the implementation. Why should the user of the 'stack' know about 'list'? The value_type of stack and list is the same, is it not? Furthermore, some const-correctness, perhaps?
Therefore:
1
2
3
4
5
6
7
8
9
10
11
12
template <typename myType>
class stack {
private:
	// whatever
public:
	void   push( myType v );
	void   pop();
	bool   empty() const;
	int    size() const;
	void   print( ostream& outFile ) const;
	myType top() const;
};
Last edited on
It would be good if you'd provide a small compilable example of the problem (like keskiverto did).
I agree, please provide example of the problem please.
closed account (NCRLwA7f)
I think I'm making the program worse messing with it.

The problem might be the way I set up the template in my header file.
The full document is at the link below in case anyone wants to have a look.
The document originally started off as a list class, then it was changed to have typedef so we could use other data types, later stacks, and queues were implemented. Finally, the typedef was supposed to be replaced with a template.

https://www.dropbox.com/s/u2vg89n6ph4p5pu/Template.zip?dl=0

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

#ifndef SNQ_hpp
#define SNQ_hpp

#include <stdio.h>
using namespace std;

template <typename myType>
class list
{
	
	public:
		
		typedef int size_type;
		//Constructors
		list();
		list(list &L1);
		//setter
		void insertBefore(myType value);
		void insertAfter(myType value);
		void insert(myType value);
		void setPos(size_type set_position);
		void replace(myType value);
	
		//getter
		bool empty();
		void first();
		void last();
		void prev();
		void next();
		size_type getPos();
		myType getElement();
		size_type	size();
		void erase();
		void clear();
	
		list operator = (list &a);
		bool operator == (list &a);
	
	private:
		myType nullVar;
		size_type CAPACITY = 200;
		myType* my_array;
		int pos;
		int ndx;
};

template <typename myType>
class queue{
	private:
	list<myType> myQueue;
	
	public:
	void inqueue(typename list<myType>::myType v); // no type name 'myType'
	void dequeue();
	int Qsize();
	bool Qempty();
	void print(ostream& outFile);
	typename list<myType>::myType front(); // no type name 'myType'
};

template <typename myType>
class stack{
	private:
	list<myType> myStack;
	
	public:
	void push(typename list<myType>::myType v); // no type name 'myType'
	void pop();
	bool empty();
	int size();
	void print(ostream& outFile);
	typename list<myType>::myType top(); // no type name 'myType'
};


//-------------------
template <typename myType>
ostream& operator << (ostream &outFile, list<myType> &a);



#endif /* SNQ_hpp */



now doing what keskirverto did stack<int> foo; works but I'm getting errors in the header file.

That is still not a compilable example. (We are too lazy to download a zip and all, if we could get errors from online compiler with shorter example code.) You could at least include the "errors" verbatim. Are we entitled to respond "we know the answer" to your "I have an error", just to match the level of detail?


1. Line 5: #include <stdio.h> . For what? Nothing in this header file requires it. Avoid unnecessary includes.

2. You do have ostream, which presumably means std::ostream. For that you need something.
Simple #include <iosfwd> would suffice in principle, for all you need here is forward declaration of std::iostream.

3. Line 6: using namespace std;. No. Anyone, who includes this header, is now entitled to file a #metoo rape charge. This using directive has valid, safe uses, but header is not one of them.
Explicitly write std::iostream in the header.

4. There is no list::myType. The list declares only one type: the size_type.
I already did add to my previous post a note about why queue and stack should not mention list. They have their own myType, which should be what is needed anyway.


5. Your template member functions do not have implementations here. One has to provide template implementations with the definitions/declarations for each translation unit.

Lets say you have them separate. You somehow compile the implementations ... for which types? Now I include your header and instantiate list<FUBAR> gaz; Where is the objective code for gaz's methods? If I don't see your implementation templates, then I cannot generate code and you cannot provide precompiled binary, for you cannot possibly know the fine details of class FUBAR.

Now that we know that you have to include implementations, you will need <iostream>. You could have that and <iosfwd>, but why bother?

You cannot require the users to #include <iostream> before they include your templates. That is rude.
Last edited on
closed account (NCRLwA7f)
I am not able to recreate the problem with a shorter piece of code, so I do apologize for that. The problem seems to be with the combination of the classes and the functions as a whole.

I don't think you guys are too lazy, truth is, just like everyone else, there are more important things to do, family....work..etc. I get it no harm done here

I cannot ask what I do not know since I am not proficient in coding, I am unaware of my errors. Being a teacher, mentor or advisor is not easy and most of the time frustrating. I understand my errors are trivial to most but that's the nature of the beast.

keskiverto #3 LMFAO! this was a good laugh, and I agree but I haven't gotten that far in the book yet.

You have already given me more feedback than my professor has all semester.

Really, guys, I do apologize. I did not realize it was a dick move to post a link to my file. I originally started with one little error, but then something else broke and well the Topic had already started and I wanted to take advantage of the feedback. I'm going to revise my code and take it a portion at a time and see if I can find the culprit.
Topic archived. No new replies allowed.