Thread: problem coding a template

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    problem coding a template

    hey guys I am trying to code a template for my main program but I am having a difficult time doing it.

    I am make a template called Array that manages a 1 dimensional array of contiguously stored lists of objects of type E. The object by default are doubles.

    When the object is being constructed the Array object receives the number of type E objects in the array. My program also contains these two functions:

    * unsigned int size() const - an inline query that returns the number of elements in the array.
    * E& operator[](int i) - an inline subscript operator that returns a reference to element i, where the first element is at index 0. If the value of i is outside array bounds, your operator returns a reference to a dummy element of type E.

    I think my main problem is when the constructor is being called, but I am not exactly sure. I have included the copy constructor and assignment operator but I haven't coded them yet. If anyone could just help me get on track with this I would really appreciate it.

    Main program:
    Code:
     #include <iostream>
     using namespace std;
     #include "Array.h"
    
     int main ( ) {
        Array<int> x(3);
        for (int i = 0; i < 3; i++)
            x[i] = 9 - i;
        x[-1] = 99;
        for (int i = 0; i < 6; i++)
            cout << x[i] << endl;
    
        Array<> y(2);
        y[0] = 2.1;
        y[1] = 1.1;
        for (int i = 0; i < 2; i++)
            cout << y[i] << endl;
    
        return 0;
     }
    template:
    Code:
    template <class Array = double,int size=50>
    class E{
    	E a_[size];
    	int numb_;
    	
    	
    public:
    
    	E(int i){
    	
    	numb_=i;
    	
    	}
    	
    	
    	unsigned int size() const{
    	
    	
    	return numb_;
    	
    	}
    	
    	
    	E& operator[](int i){
    	
    	 if (i > numb_ - 1 && i < size)
                 numb_ = i + 1;
             else
                 i = 0;
             return a[i];
    	
    	
    	}
    	
    	~E(){
    	
    	delete [] numb_;
    	
    	}
    	
    	E(const E& original){
    	
    	
    	}
    	
    	const E& operator =(const E& org){
    	
    	
    	
    	
    	}
    	
    };

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to study the following link: Template Tutorial, your definition does not look correct.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    template <class Array = double,int size=50>
    class E
    This should be:

    Code:
    template <class E = double,int size=50>
    class Array
    don't delete numb_. You didn't call new on it. Also, numb is not an array.

    You have size used for two different things. This is confusing.

    There are other problems too.
    Last edited by King Mir; 11-14-2011 at 11:14 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Fix up those problems as best you can and then repost your code along with the errors that the compiler is outputting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coding problem
    By Learner87 in forum C Programming
    Replies: 9
    Last Post: 07-27-2009, 10:00 AM
  2. One more coding problem
    By zrepaer in forum C Programming
    Replies: 1
    Last Post: 04-09-2009, 05:37 PM
  3. Coding problem
    By shady_shockerz in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2009, 11:36 AM
  4. Coding Problem-Help plz
    By taimurdar in forum C Programming
    Replies: 8
    Last Post: 07-05-2005, 08:55 PM
  5. coding problem
    By mandiamos in forum C Programming
    Replies: 3
    Last Post: 11-16-2003, 05:20 PM