-1

How do I call a template constructor whith objects of another class?

Example:

#include <iostream>
using namespace std;

class RGB{
    private:
    int _R;
    int _G;
    int _B;

    public:
        RGB(int a, int b, int c){
        _R=a;
        _G=b;
        _B=c;
    }
};

class Gray{
    int _G;
    public:
    Gray(int x){
        _G=x;
    }
};

template <class T1, class T2> 
class Pixel{
    public:

     Pixel(T1 i, T2 j, int a, int b){
         foo=i;
         bar=j;
         x=a;
         y=b;
     }

     void setPixel(T1 a, T2 b){
         foo=a;
         bar=b;
     }

     void getPixel();

     private:
        int x;
        int y;
        T1 foo;
        T2 bar;

};


int main(){

    Gray g(3);
    RGB rgb(4, 5, 6); 
    Pixel<Gray,RGB> pi(g, rgb, 10,27);


    return 0;
}

I can use the constructor of the class only with the default constructors of the two classes Bar1 and Bar2 but not with the two objects of the classes that I have created before(in this case bar1 and bar2).

Errors are:

main.cpp: In instantiation of 'Pixel::Pixel(T1, T2, int, int) [with T1 = Gray; T2 = RGB]':
main.cpp:62:37: required from here
main.cpp:35:37: error: no matching function for call to 'Gray::Gray()'
main.cpp:35:37: note: candidates are:
main.cpp:26:5: note: Gray::Gray(int)
main.cpp:26:5: note: candidate expects 1 argument, 0 provided
main.cpp:23:7: note: Gray::Gray(const Gray&)
main.cpp:23:7: note: candidate expects 1 argument, 0 provided
main.cpp:35:37: error: no matching function for call to 'RGB::RGB()'
main.cpp:35:37: note: candidates are:
main.cpp:16:9: note: RGB::RGB(int, int, int)
main.cpp:16:9: note: candidate expects 3 arguments, 0 provided
main.cpp:9:7: note: RGB::RGB(const RGB&)
main.cpp:9:7: note: candidate expects 1 argument, 0 provided

The errors go away if I add default values to the RGB and Gray constructors. Sorry for the mess, I'm new here...

W4lker
  • 51
  • 1
  • 7
  • 3
    Are you getting an `incomplete type` error? I cannot follow your explanation. – Jesse Good Sep 02 '13 at 21:30
  • 1
    Your constructor should probably be `public`, but other than that, if you provide the definitions of the ctor and the two classes `Bar1` and `Bar2`, your code works fine. [Live example](http://coliru.stacked-crooked.com/a/c887bf512118a71c) – dyp Sep 02 '13 at 21:35
  • You're passing copies of bar1 and bar2 to the Foo constructor. What isn't working? What are you expecting it to do? Can you construct an example that shows what you want it to do? – Alan Stokes Sep 02 '13 at 21:35
  • 1
    @AlanStokes I think wants the compiler to know that, though he declared the constructor private, he really *meant* public. – WhozCraig Sep 02 '13 at 21:36
  • @WhozCraig Good point. I read what I expected, not what was actually there. :-) – Alan Stokes Sep 02 '13 at 21:39
  • 4
    You really need to show us the actual errors you get from the actual code you've written. – Alan Stokes Sep 02 '13 at 21:40
  • 1
    Post *real* code that exhibits the problem you're experiencing; we're intelligent, not omniscient. Without seeing what you're *trying* to do we can only guess at best (albeit many of the guesses on this forum are better than most peoples *facts*). – WhozCraig Sep 02 '13 at 21:42
  • And [here is an SSCCE](http://coliru.stacked-crooked.com/a/532c7514f631fd9a) – Mooing Duck Sep 02 '13 at 22:01

1 Answers1

6

You need to learn about initialization lists:

Pixel(T1 i, T2 j, int a, int b) : x(a), y(b), foo(i), bar(j) {
}

foo=i;, etc. in the body of the Pixel constructor is assigning to an already existing object. When your Pixel constructor is called, it trys to call the default constructor for its subobjects, Gray and RGB, however, both those types do not have default constructors generated since you provide a user-defined constructor. The solution is to use initialization lists to initialize the subobjects in your Pixel class.

Also, note that subobjects of the class are constructed in the order they are declared inside the class definition, so you should always initialize them in the same order as they are declared in your constructor initialization list.

Community
  • 1
  • 1
Jesse Good
  • 50,901
  • 14
  • 124
  • 166