Frage

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. 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.

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:

#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).

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.

War es hilfreich?

Lösung

The main problem here is that you are trying to mix C++/CLI objects and C++ containers. In your case, you don't want to use a std::vector or std::list here. Instead, you should declare Daten as a ref class and use a List to store the buttons.

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.

Reasons why you would use a std::vector in C++/CLI code:
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. You might also call C++ code that returns data in a 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#.

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).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top