Vectors in windows forms

I have made an initial foray into using vectors in windows forms. After studying several sites on vectors I created a new windows form (VS2010).

I wrote the class code using managed strings and called it public ref class raw_mat.
The declaration line was vector <raw_mat> allRawMaterials; When compiled the error messages are:

Error 1 error C2143: syntax error : missing ';' before '<'
Error 3 error C2238: unexpected token(s) preceding ';'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

If I comment out the vector <raw_mat> allRawMaterials; line the program compiles and runs.
What am I missing?

Following is the code for the class and Form1.

class code: // more data members and code to follow if this can be made 2 work
// should I even be using managed strings here?

public ref class raw_mat
{
public:
raw_mat(void);
raw_mat(String^ str, double value);
void setvalues(String^ str, double value);
virtual ~raw_mat(void);
String^ getcode(void);
double getwpg(void);
private:
String^ m_code;
double m_wpg;
};

Form1 code:

#pragma once

#include <vector>
#include "raw_mat.h"

namespace Vectors_and_Classes
{

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

public ref class Form1 : public System::Windows::Forms::Form
{
public:
vector <raw_mat> allRawMaterials;
Form1(void)
{
InitializeComponent();
}

protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
protected:

private:
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// label1
//
this->label1->BackColor = System::Drawing::Color::Yellow;
this->label1->Location = System::Drawing::Point(532, 96);
this->label1->Margin = System::Windows::Forms::Padding(4, 0, 4, 0);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(102, 34);
this->label1->TabIndex = 0;
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(340, 38);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 26);
this->textBox1->TabIndex = 1;
this->textBox1->Enter += gcnew System::EventHandler(this, &Form1::textBox1_Enter);
this->textBox1->Leave += gcnew System::EventHandler(this, &Form1::textBox1_Leave);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(9, 19);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(1284, 383);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Font = (gcnew System::Drawing::Font(L"Times New Roman", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Margin = System::Windows::Forms::Padding(4);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion

};
}
I don't think that it is a good idea to store managed classes in unmanaged containers. I would rather use the .NET collections for your managed classes.
https://msdn.microsoft.com/en-us/library/7y3x785f%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/system.collections%28v=vs.110%29.aspx
The compiler error arises because you need the namespace std:: before the vector (or using namespace std).

Managed objects can only be stored with this special pointer ^.
Topic archived. No new replies allowed.