【问题标题】:Visual Studio Error unknown type 'array^'Visual Studio 错误未知类型'array^'
【发布时间】:2018-07-16 00:32:08
【问题描述】:

我很难找出为什么会出现此错误。我正在 Visual Studio 上使用 C++ 开发 CLR UI 窗体应用程序。

如果我尝试这样做

private: int valueInSquare[9];

我收到一条错误消息,提示“托管类的成员不能是标准数组”

如果我把它改成

private: array<int>^valueInSquare;

我在尝试查看我的表单的头文件设计时收到错误提示,

C++ CodeDOM 解析器错误:行:94,列:24 --- 未知类型 'array^'。请确保引用了包含此类型的程序集。如果此类型是您的开发项目的一部分,请确保该项目已成功构建。

这里是代码的一部分,因为很多是通过在我的头文件中使用设计器预先生成的。

   #pragma once
#include "Windows.h"

namespace MagicSquare {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MagicSquare
    /// </summary>
    public ref class MagicSquare : public System::Windows::Forms::Form
    {

    public:
        MagicSquare(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MagicSquare()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    protected:
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Label^  label3;
    private: System::Windows::Forms::Label^  label4;
    private: System::Windows::Forms::Label^  label5;
    private: System::Windows::Forms::Label^  label6;
    private: System::Windows::Forms::Label^  label7;
    private: System::Windows::Forms::Label^  label8;
    private: System::Windows::Forms::Label^  label9;
    private: System::Windows::Forms::TextBox^  textBox1;
    private: System::Windows::Forms::TextBox^  textBox2;
    private: System::Windows::Forms::TextBox^  textBox3;
    private: System::Windows::Forms::TextBox^  textBox4;
    private: System::Windows::Forms::TextBox^  textBox5;
    private: System::Windows::Forms::TextBox^  textBox6;
    private: System::Windows::Forms::TextBox^  textBox7;
    private: System::Windows::Forms::TextBox^  textBox8;
    private: System::Windows::Forms::TextBox^  textBox9;
    private: array<int>valueInSquare;
    //private: int valueInSquare[9];
    private: bool valueAlreadyUsed;


    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->label3 = (gcnew System::Windows::Forms::Label());
            this->label4 = (gcnew System::Windows::Forms::Label());
            this->label5 = (gcnew System::Windows::Forms::Label());
            this->label6 = (gcnew System::Windows::Forms::Label());
            this->label7 = (gcnew System::Windows::Forms::Label());
            this->label8 = (gcnew System::Windows::Forms::Label());
            this->label9 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->textBox2 = (gcnew System::Windows::Forms::TextBox());
            this->textBox3 = (gcnew System::Windows::Forms::TextBox());
            this->textBox4 = (gcnew System::Windows::Forms::TextBox());
            this->textBox5 = (gcnew System::Windows::Forms::TextBox());
            this->textBox6 = (gcnew System::Windows::Forms::TextBox());
            this->textBox7 = (gcnew System::Windows::Forms::TextBox());
            this->textBox8 = (gcnew System::Windows::Forms::TextBox());
            this->textBox9 = (gcnew System::Windows::Forms::TextBox());
            this->valueInSquare = gcnew array<int>(9);
            for (int i = 0; i < 9; i++) {
                valueInSquare[i] = 10;
            }

            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->BackColor = System::Drawing::Color::Blue;
            this->label1->Location = System::Drawing::Point(65, 63);
            this->label1->Name = "1";
            this->label1->Size = System::Drawing::Size(65, 65);
            this->label1->TabIndex = 0;
            this->label1->Text = L"label1";
            this->label1->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label2
            // 
            this->label2->BackColor = System::Drawing::Color::Blue;
            this->label2->Location = System::Drawing::Point(218, 63);
            this->label2->Name = "2";
            this->label2->Size = System::Drawing::Size(65, 65);
            this->label2->TabIndex = 1;
            this->label2->Text = L"label2";
            this->label2->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // 
            // label3
            // 
            this->label3->BackColor = System::Drawing::Color::Blue;
            this->label3->Location = System::Drawing::Point(383, 63);
            this->label3->Name = "3";
            this->label3->Size = System::Drawing::Size(65, 65);
            this->label3->TabIndex = 2;
            this->label3->Text = L"label3";
            this->label3->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label4
            // 
            this->label4->BackColor = System::Drawing::Color::Blue;
            this->label4->Location = System::Drawing::Point(65, 153);
            this->label4->Name = "4";
            this->label4->Size = System::Drawing::Size(65, 65);
            this->label4->TabIndex = 3;
            this->label4->Text = L"label4";
            this->label4->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label5
            // 
            this->label5->BackColor = System::Drawing::Color::Blue;
            this->label5->Location = System::Drawing::Point(218, 153);
            this->label5->Name = "5";
            this->label5->Size = System::Drawing::Size(65, 65);
            this->label5->TabIndex = 4;
            this->label5->Text = L"label5";
            this->label5->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label6
            // 
            this->label6->BackColor = System::Drawing::Color::Blue;
            this->label6->Location = System::Drawing::Point(383, 153);
            this->label6->Name = "6";
            this->label6->Size = System::Drawing::Size(65, 65);
            this->label6->TabIndex = 5;
            this->label6->Text = L"label6";
            this->label6->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label7
            // 
            this->label7->BackColor = System::Drawing::Color::Blue;
            this->label7->Location = System::Drawing::Point(65, 242);
            this->label7->Name = "7";
            this->label7->Size = System::Drawing::Size(65, 65);
            this->label7->TabIndex = 6;
            this->label7->Text = L"label7";
            this->label7->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label8
            // 
            this->label8->BackColor = System::Drawing::Color::Blue;
            this->label8->Location = System::Drawing::Point(218, 242);
            this->label8->Name = "8";
            this->label8->Size = System::Drawing::Size(65, 65);
            this->label8->TabIndex = 7;
            this->label8->Text = L"label8";
            this->label8->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // label9
            // 
            this->label9->BackColor = System::Drawing::Color::Blue;
            this->label9->Location = System::Drawing::Point(383, 242);
            this->label9->Name = "9";
            this->label9->Size = System::Drawing::Size(65, 65);
            this->label9->TabIndex = 8;
            this->label9->Text = L"label9";
            this->label9->Click += gcnew System::EventHandler(this, &MagicSquare::labelOnClick);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(65, 84);
            this->textBox1->Name = "1";
            this->textBox1->Size = System::Drawing::Size(65, 20);
            this->textBox1->TabIndex = 9;
            this->textBox1->Visible = false;
            this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MagicSquare::MagicSquare::textBoxOnKeyPressed);
            // 
            // textBox2
            // 
            this->textBox2->Location = System::Drawing::Point(218, 84);
            this->textBox2->Name = "2";
            this->textBox2->Size = System::Drawing::Size(65, 20);
            this->textBox2->TabIndex = 10;
            this->textBox2->Visible = false;
            this->textBox2->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MagicSquare::MagicSquare::textBoxOnKeyPressed);
            // 
            // textBox3
            // 
            this->textBox3->Location = System::Drawing::Point(383, 84);
            this->textBox3->Name = "3";
            this->textBox3->Size = System::Drawing::Size(65, 20);
            this->textBox3->TabIndex = 11;
            this->textBox3->Visible = false;
            this->textBox3->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MagicSquare::MagicSquare::textBoxOnKeyPressed);
            // 
            // textBox4
            // 

【问题讨论】:

  • C++ 中没有“托管类”之类的东西,private: array&lt;int&gt;^valueInSquare; 在语法上也是无效的。请将语言标签从 [c++] 更改为您实际使用的方言,例如 C++/CLI。
  • array&lt;int&gt;valueInSquare; 之间有一个插入符号
  • @BaummitAugen 我的错,我的教授通常说 Visual C++ 这是我第一次使用该语言,所以我认为这就是我正在使用的语言
  • @SidS 删除插入符号说“这里不允许使用 C++/CLI 数组类型”有没有办法在 Visual c++ 中定义数组?
  • Visual C++ 也不是一种语言,它是一种 IDE 或编译器。你真的应该弄清楚你使用的是什么语言,否则,提出一个有用的问题可能会很困难。

标签: visual-studio visual-c++ c++-cli


【解决方案1】:

您的标识符 array 可能存在名称冲突。尝试使用其完全限定名称 cli::array,以避免与 std::array 和名称的其他用法产生歧义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多