C++/CLI 中的类模板
我形成了表示二维数组的类模板:
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];
}
};
并且存在一维数据数组,
array<double>^ input={20,4,6,15,7,2,1,8,9};
我有函数,它的输入是类模板和一维数据数组。我将一维数组值设置为类模板:
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;
}
}
}
毕竟我声明了 3x3 类模板:
Array2D<double>A(3,3);
我将其发送到函数:
function(A,input,3,3);
并且我尝试打印 Array2D 值:
double temp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
{temp=A.getValue(i,j)
Console:Write("{0}",temp);}}
我在 Visual Studio 2008 中的 CLR 应用程序中执行了此应用程序,并且它有效。但我想在 Windows 窗体应用程序上实现此代码,并且它在表单应用程序上给出了类似以下的错误:
error C2065: 'Array2D' : undeclared identifier
error C2065: 'A' : undeclared identifier
如何克服此错误或我应该在哪里找到类模板?
此致...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您的代码因未声明的类型而无法编译时,需要注意的事项:
#include
相关标头(在您的情况下,包含 Array2D 的标头)? 如果不是,编译器将不知道您的类型的定义,并会认为它是未声明的。::
,例如std::vector
)? 如果没有,我是否使用了using
来使整个命名空间可用?#define
d 的符号是标头中的其他内容而不是客户端代码,还是反之亦然? 这可能不是您的问题,但在 Windows 开发中有时可能会发生,因为许多 Win32 函数都有 ASCII 和 Unicode 版本,您使用的版本取决于头文件中设置的#define
。 当您无意中隐藏了 Win32 函数名称(例如名为SendMessage
的函数)时,这尤其令人讨厌。我敢打赌,您可能缺少一个标头,可能是在您的 IDE 自动为您生成的某些代码中。
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 usedusing
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 namedSendMessage
.I'd bet that you're missing a header, maybe in some code that your IDE generated for you automatically.
检查代码的命名空间。 通常,当我将类从一个项目移动到另一个项目时,我忘记更改命名空间,这会导致这样的错误。
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.
此行将产生未定义的行为:
~Array2D() { 删除数据; }
您使用
new []
分配了数据,因此需要~Array2D() { delete [] data; }
This line will produce undefined behaviour:
~Array2D() { delete data; }
You allocated data with
new []
, so you need~Array2D() { delete [] data; }
顺便说一句,使用标准库容器有很多好处,即使在托管 C++ 中也是如此。 例如,T 类型的二维数组就是 std::vector< std::向量< T> >。 您无需关心内存管理,或确保行+列计算正确。 如果您不需要将代码编译为本机 C++,则可以根据需要使用 .NET 容器。
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.