Complier error: clang: error: linker command failed with exit code 1

when I compiled my code, I got error like this:
Undefined symbols for architecture x86_64:
"operator*(Matrix<float> const&, Matrix<float> const&)", referenced from:
_main in ex_6-dc353d.o
"operator+(Matrix<float> const&, Matrix<float> const&)", referenced from:
_main in ex_6-dc353d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <cassert>

using namespace std;

template <typename elemType>
class Matrix;

template <typename elemType>
Matrix<elemType> 
operator+( const Matrix<elemType>&, const Matrix<elemType>& );

template <typename elemType>
Matrix<elemType> 
operator*( const Matrix<elemType>&, const Matrix<elemType>& );


template < typename elemType >
class Matrix
{
	friend Matrix<elemType> 
    operator+( const Matrix<elemType>&, const Matrix<elemType>& );

    friend Matrix< elemType > 
    operator*( const Matrix<elemType>&, const Matrix<elemType>& );

protected:
	int _rows, _cols;
	elemType *_matrix;

public:
	Matrix ( int rows, int columns );
	Matrix ( const Matrix& );
	~Matrix(){ delete [] _matrix; };

	Matrix<elemType>& operator= ( const Matrix& );
	void operator+= ( const Matrix& );

	elemType& operator() ( int rows, int columns )
	{
		return _matrix[ rows * cols() + columns ];
	}

	const elemType& operator() ( int rows, int columns ) const
	{
		return _matrix[ rows * cols() + columns ];
	}

	int rows() const { return _rows; }
	int cols() const { return _cols; }

	bool same_size ( const Matrix &m ) const 
	{
		return rows() == m.rows() && cols() == m.cols();
	}

	bool confortable ( const Matrix &m ) const 
	{
		return cols() == m.rows() ;
	}

	ostream& print ( ostream& ) const;
};

template <typename elemType>
inline ostream&
operator << ( ostream& os, const Matrix<elemType> &m)
{
	return m.print ( os );
}

// Matrix.h

template <typename elemType>
Matrix<elemType>::
Matrix ( const Matrix & rhs )
	: _rows( rhs._rows ), _cols( rhs._cols )
{
	int mat_size = _rows * _cols;
	_matrix = new elemType[ mat_size ];
	for ( int ix = 0; ix < mat_size; ++ix )
		_matrix[ ix ] = rhs._matrix[ ix ];
}

template <typename elemType>
Matrix<elemType>::
Matrix ( int rows, int cols )
	: _rows( rows ), _cols ( cols )
{
	int mat_size = _rows * _cols;
	_matrix = new elemType[ mat_size ];
	for ( int ix = 0; ix < mat_size; ++ix )
		_matrix [ ix ] = 0;
}

template <typename elemType>
Matrix<elemType>& Matrix<elemType>::
operator= ( const Matrix &rhs )
{
	if ( this != &rhs )
	{
		_rows = rhs._rows;
		_cols = rhs._cols;
		int mat_size = _rows * _cols;
		delete [] _matrix;
		_matrix = new elemType[ mat_size ];
		for ( int ix = 0; ix < mat_size; ++ix )
			_matrix[ ix ] = rhs._matrix [ ix ];
	}
	return *this;
}


template <typename elemType>
Matrix<elemType>
operator+ ( const Matrix<elemType> &m1, const Matrix<elemType> &m2 )
{	
	assert( m1.same_size( m2 ) != 0 );

	Matrix<elemType> result ( m1 );
	result += m2;
	return result;
}

template < typename elemType>
Matrix<elemType>
operator* ( const Matrix<elemType> &m1, const Matrix<elemType> &m2 )
{	
	assert( m1.comfortable( m2 ) != 0 );

	Matrix <elemType> result ( m1. rows() , m2.cols() );
	for ( int ix = 0; ix < m1.rows(); ix++ )
	{
		for ( int jx =0; jx < m1.cols(); jx++ )
		{
			result ( ix, jx ) = 0;
			for ( int kx = 0; kx < m1.cols(); kx++ )
			{
				result ( ix, jx ) += m1 ( ix, kx ) * m2 ( kx, jx );
			}
		}
	}
	return result;
}

template <typename elemType>
void Matrix<elemType>::
operator+= (const Matrix &m )
{
	int matrix_size = cols() * rows();
	for (int ix = 0; ix < matrix_size; ++ix )
		*( _matrix + ix ) += ( *( m._matrix + ix ) );
}

template <typename elemType>
ostream& Matrix<elemType>::
print ( ostream &os ) const 
{
	int col = cols();
	int matrix_size = col * rows();
	for ( int ix = 0; ix < matrix_size; ++ix )
	{
		if ( ix % col == 0 ) os << endl;
		os << ( *( _matrix + ix ) ) << ' ';
	}
	os << endl;
	return os;
}

int main()
{
	ofstream log( "/Users/cabb/work/Cppcode/log.txt" );
	if (!log )
	{
		cerr << " cannot open log file!\n";
		return 0;
	}

	Matrix<float> identity(4,4);
	log << "identity: " << identity << endl;
	float ar[16] = { 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0.,
	                 1. };

	for ( int i = 0, k = 0; i < 4; ++i )
		for ( int j = 0; j < 4; ++j )
			identity( i, j ) = ar [ k++ ];

	log << "identity after set: " << identity << endl;

	Matrix<float> m ( identity );
	log << "m: memberwise initialized: " << m << endl;

	Matrix<float> m2( 8, 12 );

	log << "m2: 8x12: " << m2 << endl;

	m2 = m;
	log << "m2 agter memberwise assigned to m: "
		<< m2 << endl;
	float ar2[16] = { 1.3, 0.4, 2.6, 8.2, 6.2, 1.7, 1.3, 8.3,
					 4.2, 7.4, 2.7, 1.9, 6.3, 8.1, 5.6, 6.6};

	Matrix<float> m3( 4, 4 );
	for ( int ix = 0, kx = 0; ix <4; ++ix )
		for ( int j = 0; j < 4; ++j )
			m3 (ix, j ) = ar2[ kx ++];

	log << "m3: assigned random values: " << m3 << endl;

	Matrix<float> m4 = m3 * identity; 
	log << m4 << endl;

	Matrix<float> m5 = m3 + m4; 
	log << m5 << endl;

	m3 += m4; log << m3 << endl;
}


















On line 25
1
2
friend Matrix<elemType> 
operator+( const Matrix<elemType>&, const Matrix<elemType>& );

Does not declare a template. Later, you define the operator+ as a function template.

Since you declared a nontemplate function, using the name later doesn't trigger instantiation of the corresponding function template (since you've said a nontemplate with the right signature already exists.)

It's simplest to define the friend function in-line. See case 3, for example:

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
/// Case 1: 
template <typename T>
struct A {  
    // All instances of A<> are friends of every instance of f<>():
    template <typename U>
    friend void f(A<U> x) {} // define now or later
};

/// Case 2: 
template <typename T>
struct B;
template <typename T>
void g(B<T>) {} // declare now, define now or later.

template <typename T>
struct B {
    // Only g<T> is a friend of B<T>:
    friend void g<>(B<T>); 
};

/// Case 3:
template <typename T>
struct C { 
    // Define non-template friend in-line
    // Only h(C<T>) is a friend of C<T>.
    friend void h(C<T>) { } // define now
}; 

int main() { 
    A<int> x; f(x); 
    B<int> y; g(y);
    C<int> z; h(z);
}
Last edited on
I appreciate that your effort @mbozzi, rly help me a lot, this problem screw me the whole afternoon lol. thx
Topic archived. No new replies allowed.