| | |
compile problem for template class with copy ctor and cast to class of base template
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
compile problem for template class with copy ctor and cast to class of base template
0
#1 Nov 27th, 2008
Hello,
I have a problem creating a template class with copy ctor and cast operator to class of base template (using gcc 4.2). The program compiles if only one of them is defined but is not compiling if both (copy ctor and cast operator) are defined.
The compiler error is
error: no matching function for call to 'Vector<float>::Vector(Vector<float>)'
note: candidates are: Vector<F>::Vector(Vector<F>&) [with F = float]
and is referring to the assignment:
Vector<float> pf = pd;
Here is simple example of the class:
vector.h:
vector.cpp
main.cpp
I have a problem creating a template class with copy ctor and cast operator to class of base template (using gcc 4.2). The program compiles if only one of them is defined but is not compiling if both (copy ctor and cast operator) are defined.
The compiler error is
error: no matching function for call to 'Vector<float>::Vector(Vector<float>)'
note: candidates are: Vector<F>::Vector(Vector<F>&) [with F = float]
and is referring to the assignment:
Vector<float> pf = pd;
Here is simple example of the class:
vector.h:
cpp Syntax (Toggle Plain Text)
template<class F> class Vector { public: Vector(F x1, F y1, F z1):x(x1),y(y1),z(z1) { } // This copy constructor is ok if cast operator is not defined Vector(Vector<F>& source) { } // This cast operator is ok if copy constructor is not defined template<class D> operator Vector<D> () const; F x,y,z; };
vector.cpp
cpp Syntax (Toggle Plain Text)
template <class F> template<class D> Vector<F>::operator Vector<D> () const { return Vector<D>(x,y,z); }
main.cpp
cpp Syntax (Toggle Plain Text)
void TestVector(Vector<double> v) { } int main() { Vector<double> pd(5.6,3.4,2.4); // use the cast operator works if copy ctor is not defined Vector<float> pf = pd; // use the copy ctor works if cast operator is not defined TestVector(pd); }
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Re: compile problem for template class with copy ctor and cast to class of base template
0
#2 Nov 27th, 2008
Your main.cpp needs to have visibility of the definition of the conversion (or cast) operator. In other words, the implementation of the operator needs to be inlined into the vector.h: otherwise the compiler cannot instantiate the template.
Unrelated to your problem: it is usually a good idea for copy constructors to have their arguments const: copying an object usually does not change the original.
Unrelated to your problem: it is usually a good idea for copy constructors to have their arguments const: copying an object usually does not change the original.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: compile problem for template class with copy ctor and cast to class of base template
0
#3 Nov 27th, 2008
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: compile problem for template class with copy ctor and cast to class of base template
0
#4 Nov 27th, 2008
Re: compile problem for template class with copy ctor and cast to class of base template
0
#5 Nov 27th, 2008
I can't compile your code with VC++ 9 because it's an example of ill-formed C++ program.
If you define (wrong) copy constructor with
The only possible way to do that is:
1. Convert Vector<double> object to temporary Vector<float> object.
2. Use Vector<float> copy constructor with
No such constructor: you define your own copy constructor with non-const reference.
If you discard user-defined copy constructor, default (compiler generated) copy constructor will be used, that's OK.
If you discard conversion operator, you can't compile the program again (no copy constructor Vector<float> with
Summary: always define copy constructors with const arguments
(except special cases when you know what happens
)...
If you define (wrong) copy constructor with
Vector<F>& argument, it's impossible to initialize Vector<float> variable with Vector<double> initializator.The only possible way to do that is:
1. Convert Vector<double> object to temporary Vector<float> object.
2. Use Vector<float> copy constructor with
const Vector<float>& parameter, because C++ forbid to bind non-const reference to temporary objects.No such constructor: you define your own copy constructor with non-const reference.
If you discard user-defined copy constructor, default (compiler generated) copy constructor will be used, that's OK.
If you discard conversion operator, you can't compile the program again (no copy constructor Vector<float> with
const Vector<double> argument).Summary: always define copy constructors with const arguments
c++ Syntax (Toggle Plain Text)
Vector(const Vector<F>& source)
)... Last edited by ArkM; Nov 27th, 2008 at 5:41 pm.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: compile problem for template class with copy ctor and cast to class of base template
0
#6 Nov 28th, 2008
Hello Mr ArkM
The code posted in the article was created to prove the problem. I already agreed that copy constructor should have const parameter.
The problem is not related to converting from float to double. You can use if you like pd as vector<int> and pf as vector<double> and same problem will occur.
The problem is that if I have both the copy constructor and the cast operator there will be a compilation error in gcc 4.2 compiler and I want to know if there is an workaround for this, since I need to have both copy constructor and cast operator. Also as additional test with visual studio 2005 the code is compiled.
The code posted in the article was created to prove the problem. I already agreed that copy constructor should have const parameter.
The problem is not related to converting from float to double. You can use if you like pd as vector<int> and pf as vector<double> and same problem will occur.
The problem is that if I have both the copy constructor and the cast operator there will be a compilation error in gcc 4.2 compiler and I want to know if there is an workaround for this, since I need to have both copy constructor and cast operator. Also as additional test with visual studio 2005 the code is compiled.
Re: compile problem for template class with copy ctor and cast to class of base template
0
#7 Nov 28th, 2008
Re: compile problem for template class with copy ctor and cast to class of base template
0
#8 Nov 28th, 2008
c++ Syntax (Toggle Plain Text)
void TestVector(Vector<double> v) { } int main() { Vector<double> pd(5.6,3.4,2.4); // use the cast operator works if copy ctor is not defined Vector<float> pf = pd; // use the copy ctor works if cast operator is not defined TestVector(pd); }
In the above example, Vector<float> pf = pd won't work because the immediate assignment given a storage type before the identifier is the call to the constructor for the given type. Because it is a call to the constructor, the compiler will do a lookup for a constructor that accepts the given type. You might be successful if your class took more than just one type as a template parameter, and allow a call to the constructor such that your assignment would be legal.
For example,
Vector<float, double> pf = pd In which the second template argument would be used as a means of resolving the other type.
You could either do this, or (possibly) throw pf on the stack without initializing it, then calling the operator () which is what you probably intended to do--
c++ Syntax (Toggle Plain Text)
void TestVector(Vector<double> v) { } int main() { Vector<double> pd(5.6,3.4,2.4); Vector<float> pf; // throwing pf on the stack pf = pd<float>(); // calling overloaded operator () in pd TestVector(pd); }
-- then again it's just a theory to help push you in the right direction.
Last edited by Alex Edwards; Nov 28th, 2008 at 6:49 pm.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
Re: compile problem for template class with copy ctor and cast to class of base template
0
#9 Nov 29th, 2008
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need sample code for Text File Compression!!!
- Next Thread: Find all subsets of a given set - Optimization?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






