Problème avec le constructeur de copie quand mon paramètre n'est pas un const
Bonjour à tous,
voilà, dans mon code, j'ai une classe dont j'ai surchargé le constructeur de copie. Le truc, c'est que mon constructeur reçoit en paramètre une reference non constante car il doit modifier l'objet d'origine.
Voici un code simplifier histoire d'illustrer cela :
Code:
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
|
#include <iostream>
template <typename T>
struct floating_ptr
{
T* value;
floating_ptr(T* const pointer) : value(pointer)
{}
floating_ptr(floating_ptr& other) : value(other.release())
{}
T* release()
{
T* res = value;
value = NULL;
return res;
}
T& operator*()
{
return *value;
}
floating_ptr& operator=(floating_ptr& other)
{
value = other.release();
return *this;
}
};
floating_ptr<int> allocate_int(int value)
{
floating_ptr<int> res(new int(value));
return res;
}
int main(int argc, char** argv)
{
floating_ptr<int> p_argc = allocate_int(argc);
std::cout << "Number of parameters : " << *p_argc;
return 0;
} |
Maintenant, si j'essaie de compiler, cela me donne ceci :
Citation:
g++ -c pointer.cpp
pointer.cpp: In function ‘int main(int, char**)’:
pointer.cpp:36:46: error: no matching function for call to ‘floating_ptr<int>::floating_ptr(floating_ptr<int>)’
pointer.cpp:36:46: note: candidates are:
pointer.cpp:9:2: note: floating_ptr<T>::floating_ptr(floating_ptr<T>&) [with T = int, floating_ptr<T> = floating_ptr<int>]
pointer.cpp:9:2: note: no known conversion for argument 1 from ‘floating_ptr<int>’ to ‘floating_ptr<int>&’
pointer.cpp:7:2: note: floating_ptr<T>::floating_ptr(T*) [with T = int]
pointer.cpp:7:2: note: no known conversion for argument 1 from ‘floating_ptr<int>’ to ‘int*’
Quelqu'un a une idée d'ou vient le problème?
Pour info, j'ai essayé de comparer ma classe avec la classe auto_ptr, mais je ne parvient pas à voir ce qui cloche dans mon code.:(