Thread: class template error

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    9

    class template error

    I got an array class and I'm getting this error:

    Error 1 error C2953: 'Array2D' : class template has already been defined

    Array2D.h

    Code:
    #include "Tools.h"
    template <typename T>
    class Array2D
    {
        T** arr;
        int xSize;
        int ySize;
    
    
    public:
        Array2D(int xSize, int ySize)
        {
            arr = Tools::allocateDynamicArray<T>(xSize, ySize);
            this->xSize = xSize;
            this->ySize = ySize;
    
    
            for(int i = 0; i<xSize; i++)
            {
                for(int j = 0; j<ySize; j++)
                {
                    arr[i][j] = null;
                }
            }
        }
        int getXSize()
        {
            return xSize;
        }
        int getYSize()
        {
            return ySize;
        }
        T get(int i, int j)
        {
            return arr[i][j];
        }
        void set(int i, int j, T value)
        {
            arr[i][j] = value;
        }
        Array2D(void);
        ~Array2D(void);
    };
    Tools.h

    Code:
    #pragma once
    class Tools
    {
    public:
        template <typename T>
        static T **allocateDynamicArray( int nRows, int nCols)
        {
            T **dynamicArray;
    
    
            dynamicArray = new T*[nRows];
            for( int i = 0 ; i < nRows ; i++ )
                dynamicArray[i] = new T [nCols];
    
    
            return dynamicArray;
        }
        static long generateRandomLong();
        static bool isPrime(int num);
        static int randomNumberRange(int min, int max);
    };
    How can I fix this?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd put include guards around the code in Array2d.h.
    Guess that #pragma once thingy does the same thing
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    9
    Ok didn't know this takes care of the template stuff too

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You've gone from not knowing how to use static member variables to not knowing how to use headers to not knowing how to templates work in 24 hours?

    You are so far outpacing your ability to learn that you'll burn out.

    Until you burn out, you'll be back here every couple of hours because you've built such a shaky foundation.

    You should stop trying whatever nonsense you are trying; you aren't going to learn what you are doing.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While I will not comment on the whole "learning things" thingy, I wouldn't say it's a bad thing to spread out and try things. You may not remember all, but you will get to have fun and learn new experiences in the process. Don't call it an evil act.

    Btw, your array class is leaking memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I can't count how many wonderful programmers, as in people who already know how to program using a different language, I've seen fail to learn C++ following exactly this path.

    I will absolutely call building such a flimsy foundation as to guarantee failure idiotic unless failure is somehow the goal.

    Evil is unrelated.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Who says it is to professionally learn the language? If that was the case, then I would agree, but if it is not...? What if it is simply an act to try out and have fun? What if it is to get a feel of what the language can do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Who says it is to professionally learn the language?
    O_o

    Professionalism, like evil, is unrelated.

    What if it is to get a feel of what the language can do?
    o_O

    How many years had you already been programming in C++ a few months back when you said to me something to the effect "You are still better at this than me."?

    With over a decade of experience in C++, I've yet to find those limits.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-10-2007, 05:17 AM
  2. Pointer to template class error
    By brooksbp in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2007, 01:22 AM
  3. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  4. Template Class and C2059 Error
    By flamingwitch in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2004, 01:09 PM
  5. class template error
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 02:42 PM