473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how to use template class

254 100+
i have this array file :
Expand|Select|Wrap|Line Numbers
  1. #ifndef _ARRAY2D_H_
  2. #define _ARRAY2D_H_
  3. #include "exception.h"
  4.  
  5. namespace Game {
  6.  
  7.     /*** Simple template class for a 2D Array ***/
  8.     template<class T>
  9.     class Array2D {
  10.     public:
  11.         Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
  12.             array = new T[xsize * ysize];
  13.         }
  14.         ~Array2D() { delete array; }
  15.         void Put(unsigned x, unsigned y, T item) {
  16.             if (x >= xsize || y >= ysize) {
  17.                 throw GameException("Array out of bounds");
  18.             }
  19.             array[y * xsize + x] = item;
  20.         }
  21.         T Get(unsigned x, unsigned y) const {
  22.             if (x >= xsize || y >= ysize) {
  23.                 throw GameException("Array out of bounds");
  24.             }
  25.             return array[y * xsize + x];
  26.         }
  27.         void SetAll(T item) {
  28.             for (unsigned i = 0; i < xsize * ysize; i++) {
  29.                 array[i] = item;
  30.             }
  31.         }
  32.     private:
  33.         const unsigned xsize;
  34.         const unsigned ysize;
  35.         T* array;
  36.     };
  37. }
  38. #endif
  39.  
I have a header file to use that template class, but i dont know how to call that template class, here is my code:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _SCREEN_H_
  2. #define _SCREEN_H_
  3. #include <string>
  4. #include <iostream>
  5. #include "array2d.h"
  6.  
  7. namespace Game{
  8.     using namespace std;
  9.  
  10.     class Screen{
  11.                 /* default constructor that create a screen with specified display size (width & height) */
  12.         Screen(){
  13.                         /* somehow use the template class above in here */
  14.         }
  15.     };
  16.  
  17. }
  18. #endif
  19.  
Thank you for being helpful,
Nicky Eng.
Dec 10 '06 #1
1 1226
nickyeng
254 100+
here is one i have done just now:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. ...
  3. namespace Game{
  4.     using namespace std;
  5.         /*   im not sure is correct, so please give some correction if wrong. */
  6.         /*   i done it using array2d.h to initialise the width and height of the screen size   */
  7.     class Screen{
  8.         Screen(int width = 70, int height = 24){
  9.             Array2D <int> myArray(width,height);
  10.         }
  11.     };
  12.  
  13. }
  14. ...
  15. ...
  16.  
Dec 10 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.