arrays and templates/classes

This is a discussion on arrays and templates/classes within the C++ Programming forums, part of the General Programming Boards category; I am trying to whip out a template program, and the compiler yells at me when i try to declare ...

  1. #1
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12

    arrays and templates/classes

    I am trying to whip out a template program, and the compiler yells at me when i try to declare the contents of an array inside the public/private areas of the class:

    Code:
    template <class T>
    class myclass
    {
    	public:
    		T a[7];
    		void showarray(T);
    	private:	
    		int numarray[]={3,5,9,2,1};
    		char charray[]={'a','e','i','o','u'};
    		string starray[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
    it has a problem with the '{' character of each array. but if i got rid of the content declaration {3,5,9,2,1} then it accepted the array. i need the arrays to be passed to the a[7] array (eventually).

    i tried initializing the array in the main() but it wouldnt pass the values.

    is there a better way to get the values into the a[7] array?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    private member cann't permitted be initialized in class!

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Use a constructor?
    What is C++?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    You're drinking too much java in the morning...

    You can't have zero-sized arrays inside a class. You can only have explicit initializers for class members within a constructor initialization list. But even then you can't have explicit initializers for member arrays.
    What that means is, you can't do the following:
    Code:
    class A
    {
        char a1[]; // <-- "zero-sized" array, ERROR
    
    public:
        A() : a1('a','b') {} // <-- explicit initializers for member array, ERROR
    };
    So even if you gave a1 a size of 2, there is no syntax in C++ for explicit member array initialization.
    What you can do is the following:
    Code:
    class myclass
    {
        int numarray[5];
        
    public:
        myclass()
        {
            numarray[0] = 3;
            numarray[1] = 5;
            numarray[2] = 9;
            numarray[3] = 2;
            numarray[4] = 1;
        }
    };
    I would also suggest that if your array data is never modified, then make it static and const. "static" members of a class are "global" for all instances of that class. And "const" members can not be modified (of course). The really nice thing about static member arrays is that you can have explicit initialization of the array without specifying its size (hurray).
    Here's the syntax for doing that with templates:
    Code:
    template <class T>
    class myclass
    {
        T t;
        static int numarray[];
    };
    
    template<class T>
    int myclass<T>::numarray[] = {3,5,9,2,1};
    gg

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21