Templates

Started by
7 comments, last by markg 15 years, 10 months ago
I am currently working on learning about how templates work. There's only one thing I am a little fuzzy on.If you take an integer, and a floating ponit number it will not compile.I would have to overload the operator + function. I am not sure how to do that.Can anyone give me a little help?
Advertisement
Can you describe the kind of template you are trying to write, or show any code you have so far?
Could you provide some samples on how your using this template class? Alternatively, you can just cast one to the other, but you're losing information that way.

float j = 10.0f;int a = int(j);
------------Anything prior to 9am should be illegal.
i assume that you are trying to make a template to add an int and a float??
if yes then try this
template <class t1,class t2>
t2 sum(t1 a,t2 b)
{
return a+b;
}
//Here youll have to pass float as the second parameter to get a float return type
i guess you made the mistake by using only a single generic type?
Quote:Original post by markg
I am currently working on learning about how templates work. There's only one thing I am a little fuzzy on.If you take an integer, and a floating ponit number
it will not compile.I would have to overload the operator + function. I am not sure how to do that.Can anyone give me a little help?


Uh, ints and floats can already be added together without writing anything to do with templates. Just 'myint + myfloat' suffices.

What are you trying to do?
Quote:Original post by rip-off
Can you describe the kind of template you are trying to write, or show any code you have so far?


Yes I can here's the code I have a .h file and the main.cpp

Here's the .h code
code begin
// point.h


#ifndef POINT_H
#define POINT_H

template <typename type>
class CPoint
{
public:
CPoint(); // Default constructor

// Copy constructor
CPoint(type xVal, type yVal, type zVal);

// Set function
void set(type xVal, type yVal, type zVal);

CPoint<type> operator+(const CPoint&);

// This is our data
type x;
type y;
type z;
};

template<class type>
CPoint<type>::CPoint()
{
// Let's default to zero
x = 0;
y = 0;
z = 0;
}


// overloaded constructor.
template <class type>
CPoint<type>::CPoint(type xVal, type yVal, type zVal)
{

x = xVal;
y = yVal;
z = zVal;
}


void CPoint<type>::set(type xVal, type yVal, type zVal)
{

x = xVal;
y = yVal;
z = zVal;
}


template <class type>
CPoint<type> CPoint<type>::operator +(const CPoint &point)
{
CPoint answer;

// Caluclate answer
answer.x = x + point.x;
answer.y = y + point.y;
answer.z = z + point.z;
return answer;
}

#endif

code .h end




main.cpp
code begin

// main.cpp

#include "point.h"
#include <iostream>
using namespace std;

int main()
{
CPoint<int> p1; // Ust the default constructor
CPoint<int> p2(5,4,3);
CPoint<float> p3(2.0f, 3.0f, 4.0f); // Use the overloaded constructor
CPoint<double> p4(1.5, 2.5, 3.5); // Use the overloaded constructor

// Use the "set" function
p1.set(10, 11, 12);


CPoint<int> result = p1 + p2; // Use the overloaded + operator
// It should equal (15, 15, 15)
CPoint<int> result = p2 + p3; // This would not compile unless I overload the operator +.

// For fun print out the result
cout << result.x << " " << result.y << " " result.z << endl;
return 0;

code .cpp end


Could you edit your post and enclose the code in [ source ] tags?
You can make the overloaded operator itself a template:
template <typename Type>class Point{public:    // prefer initialiser lists to assignment    Point() : x(0), y(0), z(0) {}    Point(Type x, Type y, Type z) : x(x), y(y), z(z) {}    void set(Type xx, Type yy, Type zz) { x = xx; y = yy; z = zz; }    Type x;    Type y;    Type z;};template<typename One, typename Two>Point<One> operator+(const Point<One> &one, const Point<Two> &two){    return Point<One>(one.x + two.x, one.y + two.y, one.z + two.z);}int main(){    Point<int> p1; // Ust the default constructor    Point<int> p2(5,4,3);    Point<float> p3(2.0f, 3.0f, 4.0f); // Use the overloaded constructor    Point<double> p4(1.5, 2.5, 3.5); // Use the overloaded constructor    // Use the "set" function    p1.set(10, 11, 12);    Point<int> result1 = p1 + p2; // Use the overloaded + operator    // It should equal (15, 15, 15)    Point<int> result = p2 + p3; // This would not compile unless I overload the operator +.    // For fun print out the result    std::cout << result.x << " " << result.y << " " << result.z << std::endl;}


You'll get errors due to data truncation though - the result of Point<int> + Point<float> should probably be of type Point<float>.
Quote:Original post by Zahlman
Quote:Original post by markg
I am currently working on learning about how templates work. There's only one thing I am a little fuzzy on.If you take an integer, and a floating ponit number
it will not compile.I would have to overload the operator + function. I am not sure how to do that.Can anyone give me a little help?


Uh, ints and floats can already be added together without writing anything to do with templates. Just 'myint + myfloat' suffices.

What are you trying to do?


It was just a turorial on what templates are and how to use them and the source code was just an example of how it work's.

This topic is closed to new replies.

Advertisement