Thread: Class Template in Visual C++

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Class Template in Visual C++

    Hello,

    I formed class template that represent two dimensional array :

    Code:
    template<class T>
    class Array2D {
    protected:  
    
    public:
          T *data;
    
            const unsigned rows, cols, size;
            Array2D(unsigned r, unsigned c) : rows(r), cols(c), size(r*c) {
    
          data = new T[size];
            }
            ~Array2D() { delete data; }
            void setValue(unsigned row, unsigned col, T value) { 
                    data[(row*cols)+col] = value;
            }
            T getValue(unsigned row, unsigned col) const {
                    return data[(row*cols)+col];
            }
    };
    And there is present one dimensional data array :

    Code:
    array<double>^ input={20,4,6,15,7,2,1,8,9};
    have function it's inputs are class template and one dimensional data array.I set one dimensional array values to class template :

    Code:
    void function(Array2d<double> &x,array< double>^ input,int width,int,height)
    {
    
    
    double temp;
    temp1=0;
    
    double temp2;
    
    int i,j;
    
    // assign input array values to two dimensional array
    for(i=0;i<width;i++)
    for(j=0;j<height;j++)
    {
    { temp2=input[temp1];
      x.setvalue(i,j,temp2);
      temp1=temp1+1;
    }
    }
    
    }
    After all I declared 3x3 class template:

    Code:
    Array2D<double>A(3,3);
    I sent it to function:

    Code:
    function(A,input,3,3);
    And I tried to print Array2D values:

    Code:
    double temp;
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    {
    {temp=A.getValue(i,j)
    
    Console:Write("{0}",temp);}}
    I executed this applicaiton in CLR Application in Visual Studio 2008 amd it worked .But I want to implement this code on Windows Form Application,and it gave error like these on Form Application:

    Code:
    error C2065: 'Array2D' : undeclared identifier
    error C2065: 'A' : undeclared identifier
    How can I overcome this error or where should I locate class template?

    Best Regards...

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Just put the declaration in a header file and the definition in a source file, then use the preprocessor #include to allow use of the code in the header file.... but that's not the case is it?
    MSDN <- Programmers Haven!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM