4

I was on codepad and I was trying to build up my skills using C++. I'd never used templates much before, so I tried looking into how to use them. The code below is the result, and unfortunately, it does not work. I did try to search for solutions to my problem, but since I don't have much experience using templates, I couldn't make any connections between my problem and other problems. So, I decided to ask for help.

template <class A>
class Vector2 {
public:
    A x,y;
    Vector2(A xp, A yp){
        this->x = xp;
        this->y = yp;
    }
};

template <class B, class A>
class rayToCast {
public:
    rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
        this->RAngle = angle;
        this->Point1 = point1;
        this->Point2 = point2;
    }
private:
    B RAngle;
    Vector2<A> point1,point2;
};

int main(){
    rayToCast<short int, float> ray(45, Vector2<float>(0.0, 0.0), Vector2<float>(-10.0, -3.0), Vector2<float>(5.0, 7.0));
    return 0;
}

Here's the output:

t.cpp: In constructor 'rayToCast<B, A>::rayToCast(B, Vector2<A>, Vector2<A>, Vector2<A>) [with B = short int, A = float]':
t.cpp:26:   instantiated from here
Line 14: error: no matching function for call to 'Vector2<float>::Vector2()'
compilation terminated due to -Wfatal-errors.

Any help is appreciated.

awsumpwner27
  • 143
  • 2
  • 4

6 Answers6

6

The rayToCast constructor tries to initialize point1 and point2 by calling Vector2's default constructor. But it doesn't have one.

You either have to provide a default constructor for the vector class, or explicitly initialize the members of rayToCast. One way is to do like this:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle), point1(point1), point2(point2)
{ }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
3

The error is not about the templates.

Your Vector2 class has no default constructor, but you want to create it with a default constructor in the constructor of rayToCast.

Use a member initializer list in the constructor of rayToCast or create a default constructor in Vector2.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Csq
  • 5,775
  • 6
  • 26
  • 39
3

You have two issues in your code:

Vector2 has no default constructor, default constructor will be called when pass Vector2 to rayToCast constructor.

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)

You need to add default constructor to Vector2 and initialize x,y to default value:

template <class A>
class Vector2 {
public:
    A x,y;
    Vector2() : x(), y() {}  // default constructor
    Vector2(A xp, A yp){
        this->x = xp;
        this->y = yp;
    }
};

Also, you have typo, should be Point1, Point2 instead of point1/point2.

class rayToCast {
public:
    rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
        this->RAngle = angle;
        this->Point1 = point1;
        this->Point2 = point2;
    }
private:
    B RAngle;
    Vector2<A> Point1;    // capital P
    Vector2<A> Point2;    // capital P
};
billz
  • 44,644
  • 9
  • 83
  • 100
  • The issue is the lacking default constructor. The capitalization is completely unrelated, and not necessarily a problem. – Chad Jan 14 '13 at 20:50
  • the main issue is default constructor but when I see it, why not point out? – billz Jan 14 '13 at 20:52
  • So, it is :p Missed where they are incorrectly used in the constructor. Apologies. – Chad Jan 14 '13 at 20:56
2

When you don't declare a constructor, you get the default constructors auto-generated for you (default constructor and copy constructor).

When you do declare a constructor, you no longer get the auto-generated constructors (so you have to manually define the others if you want them).

Because you defined Vector2(A xp, A yp), the compiler no longer auto-generates Vector2() for you, and you have to define Vector2() yourself if you want to use it.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
1

Take this part of your code:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2){
    this->RAngle = angle;
    this->Point1 = point1;
    this->Point2 = point2;
}

To the uninitiated C++ programmer it looks like this initialises Point1 and Point2, but it does not. Initialisation is either done by default (which cannot happen here, since Vector2<A> has no default constructor), or in the member initialisation list (which you didn't use); your assignments in the constructor body are just that: after-the-fact assignments.

You probably have your reasons for not giving Vector<A> a default constructor, so fix rayToCast by having its constructor properly initialise those Point1/Point2 members from the constructor arguments:

rayToCast(B angle, Vector2<A> origin, Vector2<A> point1, Vector2<A> point2)
   : RAngle(angle)
   , point1(point1)
   , point2(point2)
{}

I'd also recommend tidying up your variable and argument names, as they confused me a little (caps here, not there, nowhere, everywhere, where, there!).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

As other's have already pointed out, the error is that you lack a default constructor. Use the syntax that Bo Persson suggested.

Another error is that variable names are case-sensitive, e.g., in the constructor of rayToCast you write:

this->Point1 = point1;

However, you named the class attribute point1. Note that it is not the same as:

this->point1 = point1;
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239