Using a template class

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; 
	}

}
Remove line 8.

Change line 13 to void Homework<T>::SetName(T val).
I tried it but im still getting the same error.
1
2
3
4
5
6
7
8
9
template <typename T>
void Homework<T>::SetName(T val)
{   if(val == "")
	{   name = " ";
	}
	else
	{   name = val; 
	}
}

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
You should still have line 9 in your code.
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;
	}
}
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
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.
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>.
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.