#1 Le 10/08/2011, à 15:04
- boutor
[C++] création opérateur pour std::vector
Bonjour à tous,
Je n'arrive pas à créer des opérateurs pour std::vector.
Ci-dessous mon code :
template<typename T>
std::vector< T >& std::vector<T>::operator*=(const T a)
{
for(int i =0 ; i < *this.size(); i++) {
*this[i] *= a;
}
return *this;
}
template<typename T>
std::vector< T > std::vector<T>::operator*(const std::vector< T > B, const T a)
{
B *= a;
return B;
}
Le but est de multiplié un vecteur par un double en utilisant B = A / a ou B /= a.
Des recherches sur les erreurs que me sort le compilateur ne me mènent à rien. Je n'utilise probablement les bons mots pour la recherche.
Dernière modification par boutor (Le 10/08/2011, à 20:37)
Hors ligne
#2 Le 10/08/2011, à 20:38
- boutor
Re : [C++] création opérateur pour std::vector
J'ai essayé ça sans succès:
template<typename T1, typename T2>
std::vector< T1 >& operator*=(const T2 a)
{
for(int i =0 ; i < *this.size(); i++) {
*this[i] *= a;
}
return *this;
}
Hors ligne
#3 Le 11/08/2011, à 10:14
- boutor
Re : [C++] création opérateur pour std::vector
Ce bout de code fonctionne, mais me renvoit un WARNING qui me déplaît :
D:\vector_overload\main.cpp||In function 'std::vector<T>& operator*(const T&, std::vector<T>) [with T = int]':|
D:\vector_overload\main.cpp:35|13|instantiated from here|
D:\vector_overload\main.cpp|13|warning: reference to local variable 'B' returned|
||=== Build finished: 0 errors, 1 warnings ===|
Là je suis sur code::bloks (windows ).
#include <iostream>
#include <vector>
template< typename T>
void show(std::vector< T > A)
{
for(std::size_t i = 0; i < A.size(); ++i) {
std::cout << A[i] << std::endl;
}
}
template< typename T>
std::vector<T>& operator*(const T& a, std::vector<T> B)
{
for(std::size_t i = 0; i < B.size(); ++i) {
B[i] = a*B[i];
}
return B;
}
int main()
{
std::cout << "Hello world!" << std::endl;
std::vector<int> A(5), B;
for(std::size_t i = 0; i < A.size(); ++i) {
A[i] = (int)i;
}
std::cout << " A = " << std::endl;
show(A);
B = A;
A = 5 * B;
std::cout << " 5*A = " << std::endl;
show(A);
std::cout << " B = " << std::endl;
show(B);
return 0;
}
De plus, si je veux créer un opérateur *=, comment faire, je n'ai pas possibilité d'utiliser *this?
Hors ligne
#4 Le 11/08/2011, à 16:19
- Luc Hermitte
Re : [C++] création opérateur pour std::vector
Il faut le définir en libre également:
// non testé
template <class Ty, class Alloc>
std::vector<Ty,Alloc>& operator*=(std::vector<Ty,Alloc> & this_, Ty v_) {
for( Ty & v : this_) v *= v_; // C++11
return this_;
}
Quant à +, comme d'hab tu ne peux pas retourner une référence vers une variable locale. L'écriture canonique s'appuie sur +=.
Le tuto du sdz la donne.
Hors ligne
#5 Le 11/08/2011, à 16:43
- boutor
Re : [C++] création opérateur pour std::vector
Merci, ça m'a beaucoup aidé. J'obtiens ça :
template <class Ty, class Alloc, typename T>
std::vector<Ty,Alloc>& operator*=(std::vector<Ty,Alloc> & this_, T v_) {
for( std::size_t i = 0; i < this_.size(); ++i) this_[i]*= (Ty)v_; // C++11
return this_;
}
J'ai ajouté que la variable v_ puisse être d'un autre type.
Par contre l'operator* me bloque encore.
Hors ligne
#6 Le 11/08/2011, à 16:49
- boutor
Re : [C++] création opérateur pour std::vector
template <class Ty, class Alloc, class T>
std::vector<Ty,Alloc> operator*(std::vector<Ty,Alloc> & A, T& a)
{
std::vector<Ty,Alloc> tmp(A);
A *= a; // C++11
return tmp;
}
Le compilateur me dit :
|In function 'int main()':|
error: no match for 'operator*' in 'B * 5.0e+0'|
|note: candidate is: std::vector<_Tp, _Alloc> operator*(std::vector<_Tp, _Alloc>&, T&) [with Ty = double, Alloc = std::allocator<double>, T = double]|
||=== Build finished: 2 errors, 0 warnings ===|
Hors ligne
#7 Le 11/08/2011, à 17:11
- Luc Hermitte
Re : [C++] création opérateur pour std::vector
template <class Ty, class Alloc>
std::vector<Ty,Alloc> operator*(std::vector<Ty,Alloc> const& A, Ty const& v)
Tu ne peux pas matcher une constante dans une référence non constante. Ce qui est très logique.
Hors ligne