C++/CLI 中的类模板

发布于 2024-07-26 02:01:37 字数 1635 浏览 6 评论 0原文

我形成了表示二维数组的类模板:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情旳空白 2024-08-02 02:01:37

当您的代码因未声明的类型而无法编译时,需要注意的事项:

  • 我是否已#include相关标头(在您的情况下,包含 Array2D 的标头)? 如果不是,编译器将不知道您的类型的定义,并会认为它是未声明的。
  • 定义类型的代码是否与损坏的代码位于同一命名空间中? 如果不是,我是否指的是具有限定名称的类型(通过范围解析运算符 ::,例如 std::vector)? 如果没有,我是否使用了 using 来使整个命名空间可用?
  • 我尝试使用 #defined 的符号是标头中的其他内容而不是客户端代码,还是反之亦然? 这可能不是您的问题,但在 Windows 开发中有时可能会发生,因为许多 Win32 函数都有 ASCII 和 Unicode 版本,您使用的版本取决于头文件中设置的 #define 。 当您无意中隐藏了 Win32 函数名称(例如名为 SendMessage 的函数)时,这尤其令人讨厌。

我敢打赌,您可能缺少一个标头,可能是在您的 IDE 自动为您生成的某些代码中。

Things to look for when your code won't compile due to an undeclared type:

  • Have I #included 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.
  • Is the code defining the type in the same namespace as the broken code? If not, am I referring to the type with a qualified name (via the scope resolution operator ::, e.g. std::vector)? If not, have I used using to make the whole namespace available?
  • Is the symbol I'm trying to use #defined 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 #defines 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.

在风中等你 2024-08-02 02:01:37

检查代码的命名空间。 通常,当我将类从一个项目移动到另一个项目时,我忘记更改命名空间,这会导致这样的错误。

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.

静水深流 2024-08-02 02:01:37

此行将产生未定义的行为:
~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; }

灼疼热情 2024-08-02 02:01:37

顺便说一句,使用标准库容器有很多好处,即使在托管 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文