Creating an array with templates

This is a discussion on Creating an array with templates within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to create a temporary array inside a member function. It used to be restricted to an int ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Creating an array with templates

    Hello,

    I'm trying to create a temporary array inside a member function. It used to be restricted to an int type, but now I want to add templates to the class so what used to be:

    Code:
    int *a = new int[size];
    Is changed to:

    Code:
    T *a = new T [size];
    Is it possible to use this method for T? Or do I need to call the constructor here? I get this error message, which I think might be related to this, what do you think?

    Code:
    error: expected initializer before * token

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    That should work, assuming that T has a default constructor. You might want to post the smallest and simplest program that demonstrates the compile error.

    Oh, but then you should just write:
    Code:
    std::vector<T> a(size);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hm, it's quite odd. I created a dummy function that does nothing more than creates the array and destroys it then prints the argument to the console. It works, I guess it might be one of those errors that is the result of a chain of other issues.

    here is the function (BTW this is exactly how I use T in the constructor):

    Code:
                    void dummy(T var){
                            T *arr = new T [123];
                            delete [] arr;
                            std::cout << var << std::endl;
                    }
    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Another strange thing is if I move the declaration to the highest scope level in the function, it works but I get another error indicating that the type ' size_t[size_t]' isn't valid, eventhough T, in this case is a double.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok I got it working. Turns out that when I did the changes from int to T, I made some mistakes so:

    Code:
                                    int offset,
    
                                    arr = new T [size];
    At the top of the function was the problem. These declarations used to be on the same line previously, a comma is then just what is called for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

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