How do you pass an argument with a template class?
I have a templated class in a .h and then a main.cpp
and in my main.cpp I am getting an error that says the parameter of my main.cpp function is not declared.
this is the code I have for the .h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template < class T >
class sequence
{...}
#endif
#include "sequenceTest.cpp"
namespace cs3358Fall2009Assign04
{
template < class T >
sequence< T >::sequence()
{
used = 0;
current_index = 0;
}
and this is main.cpp where the problem is in the function header.
Uh... You have to name the parameter in the function definition in order to use it. How else do you expect the compiler to know what you're talking about?
That works except I have to comment out the #include "sequenceTest.cpp"
in the .h and I get errors that say expected init-declarator before size and i am not sure why.
this is my function definition in the .h under my template class
Okay, so it's supposed to return sequence< T >::size_type. So why did you put int there?
template < class T > int sequence< T >::size_type size() const
I did that because it says I need some type of constructor, destructor, or type conversion before "size" so I thought it had to be something and I figured size was a number?
No. Remove them. A function must always have exactly one return type in front of its name.
As for the error, is sequence in the cs3358Fall2009Assign04 namespace? The fact that you didn't get any other errors would seem to suggest that it is, but if that's the case, then I have no idea what the problem could be.
Why don't you try moving your function definitions inside the class?
ARGH! Stupid cancel button! Let me see if I can retype it right:
As for the "constructor, destructor, or type conversion" stuff, it is because you need to explictly instruct the compiler that you are using a type -- as it is possible that it isn't.
1 2 3 4 5 6
template < class T >
typename sequence< T >::size_type
size() const
{
return used;
}
As for the "char to double" problem, you haven't given us enough information to solve that one... is charHold a double?
int main(int argc, char *argv[])
{
sequence < double > s1; // A sequence of double for testing
sequence < char > s2 ; // A sequence of char for testing
int objectNum; // A number to indicate selection of s1 or s2
double numHold; // Holder for a real number
char charHold; // Holder for a character
char choice; // A command character entered by the user
case'R':
objectNum = get_object_num();
if (objectNum == 1)
{
if ( s1.is_item() )
{
numHold = s1.current();
s1.remove_current();
cout << numHold << " removed from s1." << endl;
}
else
cout << "s1 has no current item." << endl;
}
else
{
if ( s2.is_item() )
{
charHold = s2.current();
s2.remove_current();
cout << charHold << " removed from s2." << endl;
}
else
cout << "s2 has no current item." << endl;
}
break;
EDIT:
s2.current returns the ascii value of the char entered. I enter f and get 102. so somehow data [] is not returning the letter but the value of the letter.