-1

我正在尝试将新类添加到现有项目中。我的班级在 Windows 窗体应用程序项目中的 System::Windows::Forms::Form 类下工作正常。我想将我的类添加到该项目中,因为我想要不同文件中的所有类。但是当我想编译时出现错误“语法错误:标识符'字符串'”。当我删除 String 对象时,一切都可以编译。哪里有问题?

#ifndef ConGen
#define ConGen

template <class T> 
ref class GenerateControls
{

public:
    GenerateControls(int i , int j, int k,int l, int m)//, String ^ n)//, int o, int p)
    : size(i),x(j),y(k),rozstawx(l),rozstawy(m), name(n), sizex(o), sizey(p)
    {
        tablica = gcnew array < T ^ >(size);

        for ( int j = 0 ; j < i ; ++j)
        {
            tablica[j] = gcnew T(); 
            tablica[j]->Name = String::Concat(n,Convert::ToString(j));
            tablica[j]->Location = System::Drawing::Point(x+rozstawx*j,y+rozstawy*j);
            //tablica[j]->Size = System::Drawing::Size(sizex,sizey);
            tablica[j]->AutoSize = true;
            tablica[j]->Visible = true;
        }

    }
    ~GenerateControls()
    {
        //for ( int j = 0 ; j < size ; ++ j )
        //{
        //}
    }


property int Size
{
    void set(int value)
    {
        //if(value < 0 ) throw
        size=value;
    }
    int get()
    {
        return size;
    }
}
property array< T ^ > ^ Tablica
{
    array<T ^ > ^ get()
    {
        return tablica;
    }
}

void SetVisible ( int i)
{
    for( int j = 0 ; j < size ; ++j)
    {
        if ( j < i ) tablica[j]->Visible = true;
        else tablica[j]->Visible = false;
    }

}

void SetPosition( int x, int y, int odleglosc_x, int odleglosc_y, int ile)
{
    for ( int i = 0 ; i<ile ; ++i)
    {
        Tablica[i]->Location = System::Drawing::Point(x+odleglosc_x*i,y+odleglosc_y*i);
    }
}

private:
    array < T ^ > ^ tablica;
    int size;
    int x;
    int y;
    int rozstawx;
    int rozstawy;
    int sizex;
    int sizey;
//  String  ^ name;
};


#endif

我想将我的代码参加一些课程,但发生了一些奇怪的事情。这是我的表单类:

#pragma once
#include "ConGen.h"

namespace Trening {

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


        public ref class okno2 : public System::Windows::Forms::Form
        {
        public:
                okno2(void)
                {
                        InitializeComponent();
                }

        protected:

                ~okno2()
                {
                        if (components)
                        {
                                delete components;
                        }
                }

        private:

        System::ComponentModel::Container ^components;

        System::Windows::Forms::TabControl ^ tabcontrol;
        System::Windows::Forms::TabPage ^ tabpage1;
        System::Windows::Forms::TabPage ^ tabpage2;
        System::Windows::Forms::TabPage ^ tabpage3;

        System::Data::SqlClient::SqlConnection ^ sqlcon;

        GenerateControls < System::Windows::Forms::TextBox > ^ tb1;
        GenerateControls < System::Windows::Forms::TextBox > ^ tb2;
        GenerateControls < System::Windows::Forms::TextBox > ^ tb3;

        GenerateLabels < System::Windows::Forms::Label > ^ lb1;
        GenerateLabels < System::Windows::Forms::Label > ^ lb2;
        GenerateLabels < System::Windows::Forms::Label > ^ lb3;

        array < String ^ > ^ stringtabs1;
        array < String ^ > ^ stringtabs2;
        array < String ^ > ^ stringtabs3;

        array < String ^ > ^ textboxtabs1;
        array < String ^ > ^ textboxtabs2;
        array < String ^ > ^ textboxtabs3;

        int clientwidth;
        int clientheight;


#pragma region Windows Form Designer generated code

                void InitializeComponent(void)
                {

                        clientwidth = 300;
                        clientheight = 300;

                        this->tabcontrol = gcnew System::Windows::Forms::TabControl();
                        this->tabpage1 = gcnew System::Windows::Forms::TabPage();
                        this->tabpage2 = gcnew System::Windows::Forms::TabPage();
                        this->tabpage3 = gcnew System::Windows::Forms::TabPage();

                        this->tabcontrol->SuspendLayout();
                        this->tabpage1->SuspendLayout();
                        this->tabpage2->SuspendLayout();
                        this->tabpage3->SuspendLayout();

                        // tabcontrol

                        this->tabcontrol->Alignment = System::Windows::Forms::TabAlignment::Top;
                        this->tabcontrol->Controls->Add(this->tabpage1);
                        this->tabcontrol->Controls->Add(this->tabpage2);
                        this->tabcontrol->Controls->Add(this->tabpage3);
                        this->tabcontrol->Dock = System::Windows::Forms::DockStyle::Fill;
                        this->tabcontrol->HotTrack = true;
                        this->tabcontrol->Location = System::Drawing::Point(0,0);
                        this->tabcontrol->Multiline = true;
                        this->tabcontrol->Name = L"tabcontrol";
                        this->tabcontrol->SelectedIndex = 0;
                        this->tabcontrol->ShowToolTips = true;
                        this->tabcontrol->Size = System::Drawing::Size(215,129);
                        this->tabcontrol->TabIndex =1;

                        // tb1 & lb1

                        tb1 = gcnew GenerateControls< System::Windows::Forms::TextBox >(10,85,80,0,30,"tb1",145,35);
                        //lb1 = gcnew GenerateLabels< System::Windows::Forms::Label >(10,20,80,0,30,"lb1",35,20);     <----------------------------- TU
                        // tabPage1

                        this->tabpage1->BackColor = System::Drawing::Color::PaleGreen;
                        this->tabpage1->Controls->AddRange(tb1->Tablica);
                        //this->tabpage1->Controls->AddRange(lb1->Tablica);
                        this->tabpage1->Location = System::Drawing::Point(4, 4);
                        this->tabpage1->Name = L"tabpage1";
                        this->tabpage1->Padding = System::Windows::Forms::Padding(3);
                        this->tabpage1->Size = System::Drawing::Size(207, 103);
                        this->tabpage1->TabIndex = 0;
                        this->tabpage1->Text = L"Dane";
                        this->tabpage1->ToolTipText = L"Informacje o użytkowniku";
                        this->tabpage1->UseVisualStyleBackColor = false;

                        //tb2 & lb2

                        tb2 = gcnew GenerateControls< System::Windows::Forms::TextBox >(10,85,80,0,30,"tb1",145,35);
                        //lb2 = gcnew GenerateLabels< System::Windows::Forms::Label >(10,20,80,0,30,"lb1",35,20);     <----------------------------- TU

                        // tabPage2

                        this->tabpage2->BackColor = System::Drawing::Color::Plum;
                        this->tabpage2->Controls->AddRange(tb2->Tablica);
                        //this->tabpage1->Controls->AddRange(lb2->Tablica);
                        this->tabpage2->Location = System::Drawing::Point(4, 4);
                        this->tabpage2->Name = L"tabpage2";
                        this->tabpage2->Padding = System::Windows::Forms::Padding(3);
                        this->tabpage2->Size = System::Drawing::Size(207, 103);
                        this->tabpage2->TabIndex = 1;
                        this->tabpage2->Text = L"Wymiary";
                        this->tabpage2->ToolTipText = L"Wymiary poszczególnych partii";
                        this->tabpage2->UseVisualStyleBackColor = false;

                        //tb3 & lb3

                        tb3 = gcnew GenerateControls< System::Windows::Forms::TextBox >(10,85,80,0,30,"tb1",145,35);
                        //lb3 = gcnew GenerateLabels< System::Windows::Forms::Label >(10,20,80,0,30,"lb1",35,20);     <----------------------------- TU
                        // tabPage3

                        this->tabpage3->BackColor = System::Drawing::Color::Plum;
                        this->tabpage3->Controls->AddRange(tb3->Tablica);
                        //this->tabpage1->Controls->AddRange(lb3->Tablica);
                        this->tabpage3->Location = System::Drawing::Point(4, 4);
                        this->tabpage3->Name = L"tabpage3";
                        this->tabpage3->Padding = System::Windows::Forms::Padding(3);
                        this->tabpage3->Size = System::Drawing::Size(207, 103);
                        this->tabpage3->TabIndex = 2;
                        this->tabpage3->Text = L"Osiągi";
                        this->tabpage3->ToolTipText = L"Twoje osiągi";
                        this->tabpage3->UseVisualStyleBackColor = false;


                        this->Controls->Add(this->tabcontrol);
                        this->tabcontrol->ResumeLayout(false);
                        this->tabpage1->ResumeLayout(false);
                        this->tabpage1->PerformLayout();
                        this->tabpage2->ResumeLayout(false);
                        this->tabpage2->PerformLayout();
                        this->tabpage3->ResumeLayout(false);
                        this->tabpage3->PerformLayout();

                        this->ResumeLayout(false);

                        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                        this->ClientSize = System::Drawing::Size(clientwidth, clientheight);
                        this->Name = L"Form1";
                        this->Text = L"Form1";
                        this->ResumeLayout(false);
                }
#pragma endregion

                 }

        };
}

这是 GenerateControls & GenerateLabels 类(在一个文件中,因为当它们被拆分时我无法编译)。

#ifndef ConGen
#define ConGen


using namespace System;
namespace Trening
{
        template <class T> 
        ref class GenerateControls
        {

        public:
                GenerateControls(int i , int j, int k,int l, int m, String ^ n, int o, int p)
                : size(i),x(j),y(k),rozstawx(l),rozstawy(m), name(n)//, sizex(o), sizey(p)
                {
                        tablica = gcnew array < T ^ >(size);

                        for ( int j = 0 ; j < i ; ++j)
                        {
                                tablica[j] = gcnew T();        
                                tablica[j]->Name = String::Concat(n,Convert::ToString(j));
                                tablica[j]->Location = System::Drawing::Point(x+rozstawx*j,y+rozstawy*j);
                                //tablica[j]->Size = System::Drawing::Size(sizex,sizey);
                                tablica[j]->AutoSize = true;
                                tablica[j]->Visible = true;
                        }

                }
                ~GenerateControls()
                {
                        //for ( int j = 0 ; j < size ; ++ j )
                        //{
                        //}
                }


        property int Size
        {
                void set(int value)
                {
                        //if(value < 0 ) throw
                        size=value;
                }
                int get()
                {
                        return size;
                }
        }
        property array< T ^ > ^ Tablica
        {
                array<T ^ > ^ get()
                {
                        return tablica;
                }
        }

        void SetVisible ( int i)
        {
                for( int j = 0 ; j < size ; ++j)
                {
                        if ( j < i ) tablica[j]->Visible = true;
                        else tablica[j]->Visible = false;
                }

        }

        void SetPosition( int x, int y, int odleglosc_x, int odleglosc_y, int ile)
        {
                for ( int i = 0 ; i<ile ; ++i)
                {
                        Tablica[i]->Location = System::Drawing::Point(x+odleglosc_x*i,y+odleglosc_y*i);
                }
        }

        void SetSize( int x, int y, int ile)
        {
                // if (ile > Size ) throw
                for ( int i = 0 ; i < ile ; ++i)
                {
                        Tablica[i]->Size = System::Drawing::Size(x, y);
                }
        }

        private:
                array < T ^ > ^ tablica;
                int size;
                int x;
                int y;
                int rozstawx;
                int rozstawy;
                int sizex;
                int sizey;
                String  ^ name;

        };

        #include "ConGen.h" // skopiowałem poprzez crtl + a całą klasę i zapomniałem usunąć tej linijki.
using namespace System;

template < class T >
        ref class GenerateLabels : public GenerateControls< T > //: public SetControlText< T >
        {
                public:

            GenerateLabels(int i , int j, int k,int l, int m, String ^ n, int o, int p)
                :GenerateControls( i,j,k,l,m,n,o,p)
                {
                }
                ~GenerateLabels()
                {
                }

                void SetText( array< String ^ > ^ name)
                {
                        int i = 0;
                        //if (name.Size != controlarray.Size ) throw 
                        for each ( String ^ str in name)
                        {
                                Tablica[i++]->Text = str;
                        }
                }

        };
}
#endif

没有代码女巫被标记为“<--------Tu”我得到了这样的错误(他在头等舱okno2)。

1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: tablica): (0x04000033).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: size): (0x04000034).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: x): (0x04000035).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: y): (0x04000036).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: rozstawx): (0x04000037).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: rozstawy): (0x04000038).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: sizex): (0x04000039).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: sizey): (0x0400003a).
1>Trening.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; fields: name): (0x0400003b).
1>Trening.obj : error LNK2022: metadata operation failed (801311D7) : Differing number of fields in duplicated types (Trening.GenerateControls<System::Windows::Forms::Label>): (0x02000005).
1>Trening.obj : error LNK2022: metadata operation failed (8013118B) : Inconsistent implemented interfaces in duplicated types (types: Trening.GenerateControls<System::Windows::Forms::Label>; interfaces: System.IDisposable): (0x09000002).
1>LINK : fatal error LNK1255: link failed because of metadata errors

我认为 GenerateLabels 类有问题。因为当那个类的实例没有被初始化时,我得到了那个错误。使用 GenerateControls 一切正常。

4

1 回答 1

0

using namespace System;我在您的代码中没有看到 a 。您在复制/粘贴到网络时是否修剪了它,还是需要添加它?

我看到您通过它们的完整名称空间引用 System::Drawing::Point 和 System::Drawing::Size,您可能只需要 System::String 的 using 指令。

于 2012-09-18T18:29:40.753 回答