This question already has an answer here:
this code make error LNK2019
#pragma once
#include <iostream>
using namespace std;
template <typename T>
class SortedArray
{
public:
SortedArray();
SortedArray(const SortedArray& a);
SortedArray(size_t size);
~SortedArray();
SortedArray& operator=(const SortedArray& a);
size_t size() const
{
return size_;
}
const T& operator()(int idx) const;
void Reserve(int size);
void Sort();
void Add(const T& value);
int Find(const T& value);
private:
T* values_;
int size_, alloc_;
};
template <typename T>
istream& operator>>(istream& is, SortedArray<T>& a);
template <typename T>
ostream& operator<<(ostream& os, const SortedArray<T>& a);
this is header file
#include "sorted_array.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
SortedArray<T>::SortedArray()
{
}
template <typename T>
SortedArray<T>::SortedArray(const SortedArray& a)
{
*this = a;
}
template <typename T>
SortedArray<T>::SortedArray(size_t size)
{
values_ = new T[size];
}
template <typename T>
SortedArray<T>::~SortedArray()
{
free(values_);
}
template <typename T>
SortedArray<T>& SortedArray<T>::operator=(const SortedArray& a)
{
free(values_);
values_ = new T[a.size()];
for ( int i = 0; i<a.size(); ++i )
values_[i] = a(i);
return *this;
}
template <typename T>
const T& SortedArray<T>::operator()(int idx) const
{
return values_[idx];
}
template <typename T>
void SortedArray<T>::Reserve(int size)
{
values_ = new T[size];
}
template <typename T>
void SortedArray<T>::Sort()
{
for ( int i = 0; i<size_; ++i )
for ( int j = i + 1; j<size_; ++j )
{
if ( values_[i]>values_[j] )
{
T tmp;
tmp = values_[i];
values_[i] = values_[j];
values_[j] = values_[i];
}
}
}
template <typename T>
void SortedArray<T>::Add(const T& value)
{
values_[size_] = value;
size_++;
Sort();
}
template <typename T>
int SortedArray<T>::Find(const T& value)
{
int s = 0, e = size_ - 1;
while ( true )
{
int mid = ( s + e ) / 2;
if ( s == e )
return -1;
if ( values_[mid] == value )
return mid;
if ( values_[mid]<value )
s = mid + 1;
else
e = mid - 1;
}
}
template <typename T>
istream& operator>>(istream& is, SortedArray<T>& a){
int len;
is>>len;
for(int i=0; i<len; ++i){
T tmp;
is>>tmp;
a.Add(tmp);
}
return is;
}
template <typename T>
ostream& operator<<(ostream& os, const SortedArray<T>& a){
for(int i=0; i<a.size(); ++i)
os<<a(i)<<" ";
os<<endl;
return os;
}
template class SortedArray<int>;
template class SortedArray<double>;
template class SortedArray<string>;
this is sorted_array.cc
#include <iostream>
#include <string>
#include <map>
#include "sorted_array.h"
using namespace std;
typedef SortedArray<int> IntSortedArray;
typedef SortedArray<double> DoubleSortedArray;
typedef SortedArray<string> StringSortedArray;
int main()
{
map<string, IntSortedArray> int_arrays;
map<string, DoubleSortedArray> double_arrays;
map<string, StringSortedArray> string_arrays;
string cmd, str;
while ( cmd != "quit" )
{
cin >> cmd;
if ( cmd == "int" )
{
IntSortedArray array;
cin >> str;
cin >> array;
int_arrays[str] = array;
cout << str << " : ";
cout << array;
cout << endl;
}
else if ( cmd == "double" )
{
DoubleSortedArray array;
cin >> str;
cin >> array;
double_arrays[str] = array;
cout << str << " : ";
cout << array;
cout << endl;
}
else if ( cmd == "string" )
{
StringSortedArray array;
cin >> str;
cin >> array;
string_arrays[str] = array;
cout << str << " : ";
cout << array;
cout << endl;
}
else if ( cmd == "list" )
{
for ( map<string, IntSortedArray>::const_iterator
it = int_arrays.begin(); it != int_arrays.end(); ++it )
{
cout << "int " << it->first << " : " << it->second << endl;
}
for ( map<string, DoubleSortedArray>::const_iterator
it = double_arrays.begin(); it != double_arrays.end(); ++it )
{
cout << "double " << it->first << " : " << it->second << endl;
}
for ( map<string, StringSortedArray>::const_iterator
it = string_arrays.begin(); it != string_arrays.end(); ++it )
{
cout << "string " << it->first << " : " << it->second << endl;
}
}
else if ( cmd == "find" )
{
cin >> str;
if ( int_arrays.count(str) > 0 )
{
int value;
cin >> value;
cout << int_arrays[str].Find(value) << endl;
}
else if ( double_arrays.count(str) > 0 )
{
double value;
cin >> value;
cout << double_arrays[str].Find(value) << endl;
}
else if ( string_arrays.count(str) > 0 )
{
string value;
cin >> value;
cout << string_arrays[str].Find(value) << endl;
}
}
}
return 0;
}
and this is my main file
and error message in gcc undefined reference to 'operator<<(std::basic_ostream >&, SortedArray)' undefined reference to 'operator<<(std::basic_ostream >&, SortedArray)'
and also operator '>>'
I think this error is connected with template but i couldn't fix it. thank you.
Aucun commentaire:
Enregistrer un commentaire