using templates and constructor

using templates and constructor

Page 1 of 1

13 Replies - 1040 Views - Last Post: 25 March 2009 - 06:31 PM Rate Topic: -----

#1 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

using templates and constructor

Post icon  Posted 25 March 2009 - 03:26 PM

 #include <iostream.h> 

template < class T>
class pair
{
	private:
	T x;T y;

	public:


	void output(T x, T y)
	{
		cout <<"The pair vlaues are "<<x<<" and "<<y<<endl;
	
	}

	void examine(T x, T y)
	{
		if (x==y)
			cout << "They are equal.\n";
		else 
			cout <<"They are NOT equal.\n";	
	}

	T sum(T x, T y)
	{
		return (x+y);
	}

	pair(T a, T b)
	{
		x=a;
		y=b;
	}

};

int main()
 
{ 
	pair <int>i (2,3);
	i.output();
	i.examine();
	i.sum();
	pair <double>d (1.1,2.6);
	d.output();
	d.examine();
	d.sum();
	pair<char>c ('c', 'c');
	c.output();
	c.examine();
	c.sum();
	
	return 0;
		
}  


it not passing the values from main to the following functions.

Is This A Good Question/Topic? 0
  • +

Replies To: using templates and constructor

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,407
  • Joined: 13-June 08

Re: using templates and constructor

Posted 25 March 2009 - 03:30 PM

View Posttrent62301, on 25 Mar, 2009 - 02:26 PM, said:

 #include <iostream.h> 

template < class T>
class pair
{
	private:
	T x;T y;

	public:


	void output(T x, T y)
	{
		cout <<"The pair vlaues are "<<x<<" and "<<y<<endl;
	
	}

	void examine(T x, T y)
	{
		if (x==y)
			cout << "They are equal.\n";
		else 
			cout <<"They are NOT equal.\n";	
	}

	T sum(T x, T y)
	{
		return (x+y);
	}

	pair(T a, T b)
	{
		x=a;
		y=b;
	}

};

int main()
 
{ 
	pair <int>i (2,3);
	i.output();
	i.examine();
	i.sum();
	pair <double>d (1.1,2.6);
	d.output();
	d.examine();
	d.sum();
	pair<char>c ('c', 'c');
	c.output();
	c.examine();
	c.sum();
	
	return 0;
		
}  


it not passing the values from main to the following functions.


I think you want

template <typename t>



Right now, Template is expecting some kind of class but I think you're trying to use them as integers or something else.
Was This Post Helpful? 0
  • +
  • -

#3 bigbeck89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-March 09

Re: using templates and constructor

Posted 25 March 2009 - 03:45 PM

View Postccubed, on 25 Mar, 2009 - 02:30 PM, said:

I think you want

template <typename t>



Right now, Template is expecting some kind of class but I think you're trying to use them as integers or something else.


No you can use <typename T> or <class T>, both are correct.

This post has been edited by bigbeck89: 25 March 2009 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

Re: using templates and constructor

Posted 25 March 2009 - 04:41 PM

I tried that it dont work. the contructor it suppose to be called in main and pass the values from main to the constructor
Was This Post Helpful? 0
  • +
  • -

#5 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,407
  • Joined: 13-June 08

Re: using templates and constructor

Posted 25 March 2009 - 05:05 PM

View Posttrent62301, on 25 Mar, 2009 - 03:41 PM, said:

I tried that it dont work. the contructor it suppose to be called in main and pass the values from main to the constructor


i'm pretty sure you can't have a space between pair and <type>

So it should be pair<int> i(2,3) I think.

This post has been edited by ccubed: 25 March 2009 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

Re: using templates and constructor

Posted 25 March 2009 - 05:39 PM

i just tried that and here is the error im getting

C:\csc 160\8.2 using templates.cpp(47) : error C2660: 'output' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(48) : error C2660: 'examine' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(49) : error C2660: 'sum' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(51) : error C2660: 'output' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(52) : error C2660: 'examine' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(53) : error C2660: 'sum' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(55) : error C2660: 'output' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(56) : error C2660: 'examine' : function does not take 0 parameters
C:\csc 160\8.2 using templates.cpp(57) : error C2660: 'sum' : function does not take 0 parameters
Error executing cl.exe.

8.2 using templates.obj - 9 error(s), 0 warning(s)
Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: using templates and constructor

Posted 25 March 2009 - 05:41 PM

function does not take 0 parameters - Read that VERY carefully.
DOES NOT TAKE: ZERO PAREMETERS (arguments)

If I made a function called: MyFunction(int);
and called it like so: MyFunction() I would get the error:
FUNCTION DOES NOT TAKE 0 PAREMETERS
Was This Post Helpful? 0
  • +
  • -

#8 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

Re: using templates and constructor

Posted 25 March 2009 - 05:57 PM

is the constructor call before the function call automatically assign the values into the function
	pair (T a, T b)
	{
		x=a;
		y=b;
	}

};

int main()
 
{ 
	pair<int>i(2,3);
	i.output();
	i.examine();
	i.sum();
	pair<double>d(1.1,2.6);
	d.output();
	d.examine();
	d.sum();
	pair<char>c('c', 'c');
	c.output();
	c.examine();
	c.sum();



View Posttrent62301, on 25 Mar, 2009 - 04:56 PM, said:

is the constructor call before the function call automatically assign the values into the function
	pair (T a, T b)
	{
		x=a;
		y=b;
	}

};

int main()
 
{ 
	pair<int>i(2,3);
	i.output();
	i.examine();
	i.sum();
	pair<double>d(1.1,2.6);
	d.output();
	d.examine();
	d.sum();
	pair<char>c('c', 'c');
	c.output();
	c.examine();
	c.sum();


Was This Post Helpful? 0
  • +
  • -

#9 bigbeck89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-March 09

Re: using templates and constructor

Posted 25 March 2009 - 06:04 PM

Im not sure if it matters, but did you try making a default constructor before overloading one?
Was This Post Helpful? 0
  • +
  • -

#10 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

Re: using templates and constructor

Posted 25 March 2009 - 06:08 PM

im suppose to make one template that had all lthose functions in it and use it for int, double, and char data type
Was This Post Helpful? 0
  • +
  • -

#11 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,407
  • Joined: 13-June 08

Re: using templates and constructor

Posted 25 March 2009 - 06:12 PM

There's nothing wrong with your code. It's working exactly as it should. You have declared your functions as taking an X and Y value and you're not giving them these values, so you're getting an error. You don't need to pass these functions X and Y, they'll have access to the private data members of your class pair.
Was This Post Helpful? 0
  • +
  • -

#12 trent62301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-September 08

Re: using templates and constructor

Posted 25 March 2009 - 06:26 PM

Thank for the info but the sum isnt couting to the screen got any clue?

nevermind that last post i figured it out and thank you ccubed for the help
Was This Post Helpful? 0
  • +
  • -

#13 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,407
  • Joined: 13-June 08

Re: using templates and constructor

Posted 25 March 2009 - 06:28 PM

Glad to be of help. :^:
Was This Post Helpful? 0
  • +
  • -

#14 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: using templates and constructor

Posted 25 March 2009 - 06:31 PM

Don't forget to use the This Post Was Helpful! button.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1