简体   繁体   English

在类中使用std :: vector或std :: list的Visual C ++ 2012

[英]Visual C++ 2012 Using a std::vector or std::list in class

I just started to use Visual Studio 2012 and I tried to create a std::list or a std::vector in the private part of a class to save data from an editor I want to create. 我刚开始使用Visual Studio 2012,并且尝试在类的私有部分中创建std :: list或std :: vector来保存要创建的编辑器中的数据。 I would like to save the neccesary data there like the X and Y Coordinates of the UI Componentes, but I wasnt able to create a std::list or a std::vector. 我想在其中保存必要的数据,例如UI组件的X和Y坐标,但是我无法创建std :: list或std :: vector。

The Class where the data is saved: 保存数据的类:

ref class MyButton
{
public:
    MyButton(System::Windows::Forms::Button ^pBtn);


private:
    int X;
    int Y;
    int Width;
    int Height;
    System::String^ text;
};

The class which will manage all types of componentes (for the begining I only use buttons but later I want to save data from pictures and so on): 该类将管理所有类型的组件(开始时,我仅使用按钮,但后来我想保存图片中的数据,依此类推):

#include <list>
#include <vector>
#include "MyButton.h"

using namespace System::Windows::Forms;


class Daten
{
public:
    Daten();
    ~Daten();
    void addButton(Button pBtn);
    Button getButton(int pIdx);

private:
    static std::vector<MyButton> myButton;
};

And now the assosiation to the UI where the buttons are placed: 现在,将其关联到放置按钮的UI:

#include "Daten.h"
public ref class Main : public System::Windows::Forms::Form
{
public:
    Main(void)
    {
        InitializeComponent();
        //
        //TODO: Konstruktorcode hier hinzufügen.
        //
        daten = new Daten();
    }
.
.
.
private: Daten *daten;

Mostly I get the error code C3699 but it sais to replace the *, but not in my Classes but in xmemory0(527). 通常,我得到的错误代码为C3699,但它表示替换*,但不是在我的类中,而是在xmemory0(527)中。

Is there a way to have a list or vector in a class? 有没有办法在一个类中有一个列表或向量? I could save the data in temp-files but I think there has to be a way. 我可以将数据保存在临时文件中,但我认为必须有一种方法。

Thanks for any advice in advance. 感谢您提前提出任何建议。

The main problem here is that you are trying to mix C++/CLI objects and C++ containers. 这里的主要问题是您正在尝试混合使用C ++ / CLI对象和C ++容器。 In your case, you don't want to use a std::vector or std::list here. 就您而言,您不想在此处使用std::vectorstd::list Instead, you should declare Daten as a ref class and use a List to store the buttons. 相反,您应该将Daten声明为ref class并使用List来存储按钮。

using namespace System.Collections.Generic; //For Generic List

public ref class Daten
{
public:
    Daten();
    void addButton(MyButton^ pBtn);
    MyButton^ getButton(int pIdx);

private:
    List<MyButton^>^ myButton;
};

I don't think you want the list to be static without the member functions being static (either all or none), so I removed that. 我不认为您希望列表是静态的,而成员函数不是静态的(全部或全部),所以我删除了该列表。 It also looks like it should be using MyButton everywhere. 看起来它应该在所有地方都使用MyButton

Reasons why you would use a std::vector in C++/CLI code: 原因可以使用一个std::vector在C ++ / CLI代码:
If you have existing (pure) C++ code to process information, you may want to build a vector of objects, but in this case, you would build it with pure C++ data and not with .Net classes or objects. 如果您已有(纯)C ++代码来处理信息,则可能需要构建对象向量,但是在这种情况下,将使用纯C ++数据而不是 .Net类或对象来构建它。 You might also call C++ code that returns data in a std::vector . 您可能还会调用C ++代码,该代码以std::vector返回数据。 In this case, you might then translate/marshal the data into .Net collections and object to work with in C++/CLI or to send to .Net classes built with C#. 在这种情况下,您然后可以将数据转换/封送为.Net集合和对象,以在C ++ / CLI中使用或发送到使用C#构建的.Net类。

Note: 注意:
If you really want to use std::vector and did not intend on starting a C++/CLI project, you need to start over and pick a different template for the project (MFC or Win32 are two options for this and the UI will not be WinForms and you will not have access to .Net objects or classes). 如果您确实要使用std::vector而不打算启动C ++ / CLI项目,则需要重新开始并为该项目选择一个不同的模板(MFC或Win32是这的两个选择,并且UI不会是WinForms,您将无权访问.Net对象或类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM