Hi,
I created a very simple program using a template class named Foo (code below). All it does is accept input and then print it out. However, when I attempt to define the class in the main function Foo<char> or Foo<char[]> or even Foo<char*> I receive a variety of errors.
I think that this is because I am attempting to pass in a char array of indeterminate length. So, the question become, how do I set up a template to handle string data?
Thanks in advance!!
I created a very simple program using a template class named Foo (code below). All it does is accept input and then print it out. However, when I attempt to define the class in the main function Foo<char> or Foo<char[]> or even Foo<char*> I receive a variety of errors.
I think that this is because I am attempting to pass in a char array of indeterminate length. So, the question become, how do I set up a template to handle string data?
Thanks in advance!!
Code:
-------> Class Foo < ---------
template<typename T>
class Foo
{
public:
T Data;
Foo(T NewData);
void PrintFoo();
};
template<typename T>
Foo<T>::Foo(T NewData)
{
strcpy( Data, NewData);
}
template<typename T>
void Foo<T>::PrintFoo()
{
printf("%s \n", Data);
}
----- > Function Main < --------
#include "string"
#include "main.h"
#include "Foo.h"
using namespace std;
int main(int argc, const char* argv[])
{
char msg[] = "Hello again from Foo!";
Foo<char[]> NewFoo(msg);
printf("Hello World! \n");
NewFoo.PrintFoo();
}
Last edited by a moderator: