Reputation: 541
I formed class template that represent two dimensional array :
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
array<double>^ input={20,4,6,15,7,2,1,8,9};
I have function it's inputs are class template and one dimensional data array.I set one dimensional array values to class template :
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:
Array2D<double>A(3,3);
I sent it to function:
function(A,input,3,3);
And I tried to print Array2D values:
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:
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...
Upvotes: 1
Views: 770
Reputation: 109
As an aside, there is a lot of benefit to using the standard library containers, even in managed C++. For example, a 2D array of type T is just std::vector< std::vector< T > >. You don't need to concern yourself with memory management, or ensuring that the row+column calculations are correct. If you don't need to compile your code as native C++ then use the .NET containers if desired.
Upvotes: 0
Reputation: 2753
This line will produce undefined behaviour:
~Array2D() { delete data; }
You allocated data with new []
, so you need
~Array2D() { delete [] data; }
Upvotes: 0
Reputation: 9705
Things to look for when your code won't compile due to an undeclared type:
#include
d the relevant header (in your case, the one containing Array2D)? If not, the compiler won't know the definition of your type, and will think it's undeclared.::
, e.g. std::vector
)? If not, have I used using
to make the whole namespace available?#define
d to something else in the header but not the client code, or vice versa? This probably isn't your problem but can happen sometimes in windows development, as many Win32 functions have ASCII and Unicode versions, and which you use depends on which #define
s are set in your header files. This is especially obnoxious when you've unwittingly shadowed a Win32 function name, e.g. a function named SendMessage
.I'd bet that you're missing a header, maybe in some code that your IDE generated for you automatically.
Upvotes: 1
Reputation: 1390
Check the namespaces of your code. Often, when I move classes from one project to another, I forget to change the namespace, which causes errors like this.
Upvotes: 0