use of class template requires template argument list

I'm getting this error on line 7 in Source.cpp ,

use of class template requires template argument list

Coord<T> operator - (const Coord& b1, const Coord& b2)

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

Header:

#pragma once


template <typename T> 
class Coord {

private:
	T x_;
	T y_;

public:
	
	// 2 args constructor 
	Coord(T x = 0.0 , T y  = 0.0) : x_(x) , y_(y) {}

	// allows operator - to access private fields outside of class 
	friend Coord operator - (const Coord& b1, const Coord & b2);

	// does not work 
	//friend ostream & operator << (ostream & os,  const Coord & c);

	int getX()
	{
		return x_;
	}

	int getY()
	{
		return y_;
	}

	double& magnitude( const Coord & c )
	{
		double magnitude = c.x_ * c.x_ + c.y_ * c.y_; 
		magnitude = sqrt(magnitude); 
		return magnitude; 
	} 

	// unary minus operator 
	Coord operator-()
	{
		this->x_ = (-1) * x_;
		this->y_ = (-1) * y_;

		return *this; 
	}




};


Source.cpp file:


# include <iostream>
# include "coord.hpp"
using namespace std;


template <typename T>
Coord<T> operator - (const Coord&  b1, const Coord& b2) {
	Coord b3;
	b3.x_ = b1.x_ - b2.x_;
	b3.y_ = b1.y_ - b2.y_;
	return b3;
}

template <typename T>
ostream & operator << (ostream & os,  Coord <T> & c)
{
	os << "(" << c.getX() << "," << c.getY() << ")" << endl; 
	
	return os; 

}



int main()
{
	Coord <double>  a(3, 3);
	Coord <double> b(2, 2);

	Coord <double> c;

	c = a - b; 

	cout << "c is " << c << endl; 

	int magnitude;
	magnitude = c.magnitude(c);

	cout << " The magnitude of c is " << magnitude << endl;

	Coord <double> d;

	d = -c; 

	cout << " d is " << d << endl; 


}
1
2
3
template <typename T>
// Coord<T> operator - (const Coord&  b1, const Coord& b2) {
Coord<T> operator - ( const Coord<T>&  b1, const Coord<T>& b2 ) {

Consider placing the definitions of these two functions in the header file.
See: https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
I have made the changes , but I am still getting a linking error :

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "class Coord<double> __cdecl operator-(class Coord<double> const &,class Coord<double> const &)" (??G@YA?AV?$Coord@N@@ABV0@0@Z) referenced in function _main CartesianCoord C:\Users\tomya\Desktop\Visual Studio\ObjectOrientedAnalysis\CartesianCoord\CartesianCoord\Source.obj 1



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Header:

#pragma once


template <typename T> 
class Coord {

private:
	T x_;
	T y_;

public:
	
	// 2 args constructor 
	Coord(T x = 0.0 , T y  = 0.0) : x_(x) , y_(y) {}

	// allows operator - to access private fields outside of class 
	friend Coord<T> operator - (const Coord  <T> &  b1, const Coord  <T> & b2); 

	// does not work 
	//friend ostream & operator << (ostream & os,  const Coord & c);

	T getX() const 
	{
		return x_;
	}

	 T getY() const 
	{
		return y_;
	}

	double& magnitude( const Coord & c )
	{
		double magnitude = c.x_ * c.x_ + c.y_ * c.y_; 
		magnitude = sqrt(magnitude); 
		return magnitude; 
	} 

	// unary minus operator 
	Coord operator-()
	{
		this->x_ = (-1) * x_;
		this->y_ = (-1) * y_;

		return *this; 
	}




};


Source.cpp:


# include <iostream>
# include "coord.hpp"
using namespace std;


template <typename T>
Coord<T> operator - (const Coord  <T> &  b1, const Coord  <T> & b2) {
	Coord <T> b3;
	b3.x_ = b1.x_ - b2.x_;
	b3.y_ = b1.y_ - b2.y_;
	return b3;
}



//template <typename T>
//Coord<T> operator - ( Coord  <T> &  b1,  Coord  <T> & b2) {
//	Coord <T> b3;
//	b3.x_ = b1.getX() - b2.getX();
//	b3.y_ = b1.getY() - b2.getY();
//	return b3;
//}
//



template <typename T>
ostream & operator << (ostream & os,  Coord <T> & c)
{
	os << "(" << c.getX() << "," << c.getY() << ")" << endl; 
	
	return os; 

}



int main()
{
	Coord <double>  a(3, 3);
	Coord <double> b(2, 2);

	Coord <double> c;

	c = a - b; 

	cout << "c is " << c << endl; 

	int magnitude;
	magnitude = c.magnitude(c);

	cout << " The magnitude of c is " << magnitude << endl;

	Coord <double> d;

	d = -c; 

	cout << " d is " << d << endl; 


}
Last edited on
Have you solved your issue, masterinex?

Coord.h:
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
#ifndef COORD_H
#define COORD_H

#include <cmath>
#include <iostream>

template <typename T>
class Coord {
private:
    T x_;
    T y_;

public:
    Coord(T x = 0.0 , T y  = 0.0) : x_(x) , y_(y) {}

    friend Coord<T> operator - (const Coord<T>& b1, const Coord<T>& b2)
    {
        Coord <T> b3;
        b3.x_ = b1.x_ - b2.x_;
        b3.y_ = b1.y_ - b2.y_;
        return b3;
    }

    friend std::ostream & operator << (std::ostream & os,  Coord <T> & c)
    {
        os << "(" << c.getX() << "," << c.getY() << ")" << '\n'; 
        return os; 
    }

    T getX() const { return x_; }
    T getY() const { return y_; }

    double magnitude( const Coord & c ) const 
    {
        return std::sqrt(c.x_ * c.x_ + c.y_ * c.y_);
    }

    // unary minus operator
    Coord operator-()
    {
        this->x_ = (-1) * x_;
        this->y_ = (-1) * y_;
        return *this;
    }
};

#endif // COORD_H 


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Coord.h"
#include <iostream>

int main()
{
    Coord <double> a(3, 3);
    Coord <double> b(2, 2);
    Coord <double> c = a - b; 
    std::cout << "c is " << c << '\n'; 
    int magnitude = c.magnitude(c);
    std::cout << "The magnitude of c is " << magnitude << '\n';
    Coord <double> d = -c; 
    std::cout << "d is " << d << '\n';
}

Output:
c is (1,1)

The magnitude of c is 1
d is (-1,-1)

thanks for the concern guys.

i did solved little bit different than u did , Enoizat. I have implemented setters and I am using setters in my overloaded "-" minus operator. I did not know that u could also use friend to bring in a function that is supposed to be outside the class header inside. That is really elegant way of solving the issue.

i managed to correct the magnitude , but i was just wondering if I am testing the overloaded version of the unary minus operator in the correct manner with the line

d = -c ?

So I don't have to overload the copy assignment operator ? why is that ?

Also why is that i don't have to template the unary minus operator ?
Last edited on
That is really elegant way of solving the issue.

Following the link JLBorges gave you, I found this example:
Another approach is to define the friend function within the class body at the same moment you declare it to be a friend. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    #include <iostream>
    template<typename T>
    class Foo {
    public:
      Foo(const T& value = T());
      friend Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs)
      {
        // ...
      }
      friend std::ostream& operator<< (std::ostream& o, const Foo<T>& x)
      {
        // ...
      }
    private:
      T value_;
    };

All I had to do was copying & pasting the code.

So I don't have to overload the copy assignment operator ? why is that ?

I think the explanation is here:
http://en.cppreference.com/w/cpp/language/copy_assignment
Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class.
Also why is that i don't have to template the unary minus operator ?

Well, you should in my opinion: your code would be more legible. Anyway, if you write ‘anything’ of a function (I mean both declaration and definition) inside the template class, it seems the compiler is able to produce a proper code. I suppose it’s connected with this:
http://en.cppreference.com/w/cpp/language/template_argument_deduction
Template argument deduction
In order to instantiate a function template, every template argument must be known, but not every template argument has to be specified. When possible, the compiler will deduce the missing template arguments from the function arguments.

Topic archived. No new replies allowed.