Using a template class
May 12, 2013 at 6:35am UTC
Im just learning using class template but I keep getting error unable to match function definition to an existing declaration
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
template <typename T>
class Homework
{
private :
string name;
public :
template <typename T>
void SetName(T val);
};
template <typename T>
void Homework::SetName(string val)
{
if (val == "" )
{
name = " " ;
}
else
{
name = val;
}
}
May 12, 2013 at 9:03am UTC
Remove line 8.
Change line 13 to void Homework<T>::SetName(T val)
.
May 12, 2013 at 7:39pm UTC
I tried it but im still getting the same error.
May 13, 2013 at 12:05am UTC
1 2 3 4 5 6 7 8 9
template <typename T>
void Homework<T>::SetName(T val)
{ if (val == "" )
{ name = " " ;
}
else
{ name = val;
}
}
May 13, 2013 at 4:26am UTC
yeah i have exactly that but i get this error.
1 2 3 4 5
c:\users\ruben_000\desktop\user\homework.h(134): error C2244: 'Homework<T>::SetName' : unable to match function definition to an existing declaration
1> definition
1> 'void Homework<T>::SetName(T)'
1> existing declarations
1> 'void Homework<T>::SetName(T)'
its all in my .h file. i dont have a homework.cpp
Last edited on May 13, 2013 at 5:00am UTC
May 13, 2013 at 8:24am UTC
You should still have line 9 in your code.
May 13, 2013 at 8:33am UTC
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
#include <string>
template <typename T>
class Homework
{
private :
T name;
public :
void SetName(T val);
};
template <typename T>
void Homework<T>::SetName(T val)
{
if (val == "" )
{
name = " " ;
}
else
{
name = val;
}
}
May 14, 2013 at 2:34am UTC
thanks @burgondaries
I have one more question
1 2 3 4 5 6 7 8 9 10 11 12
public :
const Homework& operator =(const Homework& obj) ;
template <typename T>
Homework& Homework<T>::operator =(const Homework obj) const
{
SetRating(obj.rating);
return *this ;
}
im getting the same error as before.
ive tried different combinations for the prototype but still no luck.
Last edited on May 14, 2013 at 5:33am UTC
May 14, 2013 at 5:50am UTC
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
template <typename T>
class Homework
{
private :
T name;
public :
void SetName(T val);
const Homework<T> &operator =(const Homework<T> &obj) const ;
};
template <typename T>
const Homework<T> &Homework<T>::operator =(const Homework<T> &obj) const
{
SetRating(obj.rating);
return *this ;
}
template <typename T>
void Homework<T>::SetName(T val)
{
if (val == "" )
{
name = " " ;
}
else
{
name = val;
}
}
Remember to match the declaration correctly, you missed a const at the end and at the beginning. In addition, one can't return Homework AFAIK, it must be Homework<T>, same for input.
May 14, 2013 at 6:56am UTC
You can write Homework without <T> inside the class definition and it will assume Homework<T> but outside the class definition you'll have to write Homework<T>.
May 14, 2013 at 7:14am UTC
You're right Peter87! I wonder if one can return a specific type of Homework like Homework<int>. Just tried, seems to work. Should we be explicit in our declaration of <T> or <int>? I guess it's up to each programmer if we're allowed to omit the type like you said...
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
template <typename T>
class Homework
{
private :
T name;
public :
void SetName(T val);
const Homework &operator =(const Homework &obj) const ;
};
template <typename T>
const Homework<T> &Homework<T>::operator =(const Homework &obj) const
{
SetRating(obj.rating);
return *this ;
}
template <typename T>
void Homework<T>::SetName(T val)
{
if (val == "" )
{
name = " " ;
}
else
{
name = val;
}
}
Topic archived. No new replies allowed.