Ciao a tutti.
Sto provando a scrivere un template di classi che rappresenterebbero dei vettori nel senso matematico del termine. Questa classe eredita direttamente da std::vector<T>.
Tutto sembra andare per il meglio quando compilo con un main() vuoto, ma appena cerco di instanziare ed utilizzare la mia classe ottengo un errore strano:
codice:
Undefined symbols for architecture x86_64:
"Vector<int>::Vector(unsigned long)", referenced from:
_main in testVector.o
"std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, Vector<int> const&)", referenced from:
_main in testVector.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testVector] Error 1
macbook-di-rocco-meli:Simulation roccomeli$ make clean
rm -f *.o
macbook-di-rocco-meli:Simulation roccomeli$ make
g++gcc-4.6.x -std=c++0x -c -o testVector.o testVector.cc
g++gcc-4.6.x -std=c++0x -c -o Vector.o Vector.cc
g++gcc-4.6.x testVector.o Vector.o -o testVector
Undefined symbols for architecture x86_64:
"Vector<int>::Vector(unsigned long)", referenced from:
_main in testVector.o
"std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, Vector<int> const&)", referenced from:
_main in testVector.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testVector] Error 1
È come se il compilatore non trova l'implementazione della classe ma davvero non capisco come mai.
Ecco il mio codice:
codice:
CC = g++gcc-4.6.x
CXX = g++gcc-4.6.x
CXXFLAGS = -std=c++0x
all: testVector
# --- TEST VECTOR ---
testVector: testVector.o Vector.o
testVector.o: testVector.cc Vector.h
# --- CLASSES ---
Vector.o: Vector.h Vector.cc
clean:
rm -f *.o
codice:
#include "Vector.h"
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
#include <initializer_list>
using std::initializer_list;
int main()
{
vector<double> V({0,1,2,3,4,5});
initializer_list<double> I({5,4,3,2,1,0});
// --- TEST CONSTRUCTEURS ---
Vector<int> v1(6);
cout << v1 << endl;
/*
Vector<int> v2(0.1,1.1,1.2);
cout << v2 << endl;
Vector< > v3(V);
cout << v3 << endl;
Vector< > v4(I);
cout << v4 << endl;
Vector< > v5(v3);
cout << v5 << endl;
// --- TEST OPERATEURS ---
cout << v2[0] << ' ' << v2[1] << ' ' << v3[2] << endl;
v5 = v4;
cout << v5 << endl;
if(v5 == v4){cout << v5 << " == " << v4 << endl;}
else{cout << v5 << " != " << v4 << endl;}
if(v5 != v3){cout << v5 << " != " << v4 << endl;}
else{cout << v5 << " == " << v4 << endl;}
*/
return 0;
}
codice:
#include "Vector.h"
#include<stdexcept>
#include<algorithm>
#include<iterator>
// *** PUBLIC ***
// --- CONSTRUCTORS ---
// Empty vector
template<typename T>
Vector<T>::Vector(size_t size)
: std::vector<T>(size, 0)
{}
// 3D vector
template<typename T>
Vector<T>::Vector(T x, T y, T z)
: std::vector<T>({x, y, z})
{}
template<typename T>
Vector<T>::Vector(const std::vector<T> & V)
: std::vector<T>(V)
{}
template<typename T>
Vector<T>::Vector(const std::initializer_list<T>& V)
: std::vector<T>(V)
{}
template<typename T>
Vector<T>::Vector(const Vector<T>& V)
{
if(*this != V)
{
*this = V;
}
}
// --- INNER OPERATORS ---
template<typename T>
bool Vector<T>::operator==(const Vector<T>& V) const
{
bool b(true);
range_exception(V, "");
for(auto i : *this)
{
if(*this[i] != V[i])
{
b = false;
break;
}
}
return b;
}
template<typename T>
bool Vector<T>::operator!=(const Vector<T>& V) const
{
range_exception(V, "");
return !(*this == V);
}
// --- EXTERNAL OPERATORS ---
template<typename T>
std::ostream& operator<<(std::ostream& out, const Vector<T>& V)
{
std::copy(V.begin(), V.end(), std::ostream_iterator<T>(out, " "));
return out;
}
// *** PRIVATE ***
template<typename T>
void Vector<T>::range_except(const Vector<T>& V, const std::string& s) const
{
if(*this.size() != V.size())
{
throw std::range_error(s);
}
}
codice:
#ifndef VECTOR_H
#define VECTOR_H
#include <vector>
#include <initializer_list>
#include <iostream>
#include <string>
template<typename T = double>
class Vector : public std::vector<T>
{
public:
// --- CONSTRUCTORS ---
Vector(size_t);
Vector(T, T, T);
Vector(const std::vector<T> &);
Vector(const std::initializer_list<T>&);
Vector(const Vector<T>&);
// --- INNER OPERATORS ---
bool operator==(const Vector<T>&) const;
bool operator!=(const Vector<T>&) const;
private:
// --- METHODS ---
void range_except(const Vector<T>&, const std::string&) const;
};
// --- EXTERNAL OPERATORS
template<typename T>
std::ostream& operator<<(std::ostream&, const Vector<T>&);
#endif