Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   Template constructor of a inherited class
  2. #include <iostream>
  3. #include <list>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class A{
  9.   public:
  10.     int x;
  11.     int y;
  12.     int first(){
  13.       return x;
  14.     }
  15.     int second(){
  16.       return y;
  17.     }
  18. };
  19.  
  20. class C{
  21.   public:
  22.     float a,b;
  23.     C(){
  24.       a = 0.0f;
  25.       b = 0.0f;
  26.     }
  27.     template<class T>
  28.       C(T t){
  29.         a = t.first();
  30.         b = t.second();
  31.       }
  32. };
  33.  
  34. class D: public C{
  35.   public:
  36.     float area(){
  37.       return a*b;
  38.     }
  39. }
  40.  
  41. int main(){
  42.   A a;
  43.   a.x = 6;
  44.   a.y = 8;
  45.   C c(a);
  46.   D d(a);
  47.   cout<<c.a<<" "<<c.b<<" "<<d.area()<<endl;
  48. }
  49.        
  50. test.cpp: In function ‘int main()’:
  51. test.cpp:56:8: error: no matching function for call to ‘D::D(A&)’
  52. test.cpp:56:8: note: candidates are:
  53. test.cpp:44:7: note: D::D()
  54. test.cpp:44:7: note:   candidate expects 0 arguments, 1 provided
  55. test.cpp:44:7: note: D::D(const D&)
  56. test.cpp:44:7: note:   no known conversion for argument 1 from ‘A’ to ‘const D&’
  57.        
  58. class D : public C {
  59. public:
  60.     template <typename T> D (T t) : C(t) {}
  61.     float area () { /* ... */ }
  62. };
  63.        
  64. test.cpp:56:8: error: no matching function for call to ‘D::D(A&)’
  65.        
  66. D d(a);
  67.        
  68. test.cpp:44:7: note: D::D()
  69. test.cpp:44:7: note: D::D(const D&)