943,685 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1342
  • C++ RSS
Nov 27th, 2008
0

compile problem for template class with copy ctor and cast to class of base template

Expand Post »
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:
cpp Syntax (Toggle Plain Text)
  1.  
  2. template<class F>
  3. class Vector
  4. {
  5. public:
  6. Vector(F x1, F y1, F z1):x(x1),y(y1),z(z1)
  7. {
  8. }
  9.  
  10. // This copy constructor is ok if cast operator is not defined
  11. Vector(Vector<F>& source)
  12. {
  13. }
  14.  
  15. // This cast operator is ok if copy constructor is not defined
  16. template<class D> operator Vector<D> () const;
  17.  
  18. F x,y,z;
  19. };

vector.cpp
cpp Syntax (Toggle Plain Text)
  1. template <class F> template<class D>
  2. Vector<F>::operator Vector<D> () const
  3. {
  4. return Vector<D>(x,y,z);
  5. }

main.cpp
cpp Syntax (Toggle Plain Text)
  1. void TestVector(Vector<double> v)
  2. {
  3. }
  4.  
  5. int main()
  6. {
  7. Vector<double> pd(5.6,3.4,2.4);
  8.  
  9. // use the cast operator works if copy ctor is not defined
  10. Vector<float> pf = pd;
  11.  
  12. // use the copy ctor works if cast operator is not defined
  13. TestVector(pd);
  14. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristinel is offline Offline
5 posts
since Nov 2008
Nov 27th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Nov 27th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

Thank you for looking into this.

I agree with const argument in copy constructor.
I've tested your idea with microsoft compiler (I'm at work now) and it worked.
I will test with gcc compiler at home to see if also works.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristinel is offline Offline
5 posts
since Nov 2008
Nov 27th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

it seems to be a problem with the gcc compiler
using microsoft compiler the code works, using the gcc is not working
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristinel is offline Offline
5 posts
since Nov 2008
Nov 27th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

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 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)
  1. Vector(const Vector<F>& source)
(except special cases when you know what happens )...
Last edited by ArkM; Nov 27th, 2008 at 5:41 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 28th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristinel is offline Offline
5 posts
since Nov 2008
Nov 28th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

It seems you don't understand me: the code IS NOT compiled with VC++ 2008 and I'm trying to explain you why. No need in any "workarounds", correct semantics error in the CODE (not in the g++): declare copy constructor as usually with const Vector<F>& parameter.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 28th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

c++ Syntax (Toggle Plain Text)
  1. void TestVector(Vector<double> v)
  2. {
  3. }
  4.  
  5. int main()
  6. {
  7. Vector<double> pd(5.6,3.4,2.4);
  8.  
  9. // use the cast operator works if copy ctor is not defined
  10. Vector<float> pf = pd;
  11.  
  12. // use the copy ctor works if cast operator is not defined
  13. TestVector(pd);
  14. }

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)
  1. void TestVector(Vector<double> v)
  2. {
  3. }
  4.  
  5. int main()
  6. {
  7. Vector<double> pd(5.6,3.4,2.4);
  8.  
  9. Vector<float> pf; // throwing pf on the stack
  10.  
  11. pf = pd<float>(); // calling overloaded operator () in pd
  12.  
  13. TestVector(pd);
  14. }

-- 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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 29th, 2008
0

Re: compile problem for template class with copy ctor and cast to class of base template

Sorry, you were right. When I've tested with Visual Studio 2005 I've tested the code with const parameter in copy constructor but when I've tested with gcc I tested with not const parameter in copy constructor and that's why failed.

Thank you for help, I can mark this as fixed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristinel is offline Offline
5 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need sample code for Text File Compression!!!
Next Thread in C++ Forum Timeline: Find all subsets of a given set - Optimization?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC