How do I use a vector in VS2008 .NET framework

I'm trying to use a vector instead of an array in my program. It is in the .NET framework and for some reason it has no idea what a vector is. I've included #include <vector> at the top of all of my header files, but I get errors as though it didn't know what a vector is. Does anyone know what my problem is? Am I missing something, did I leave something out?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include "painting.h"
#include "ctype.h"
#include <vector>

namespace My266program8 {

	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 Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

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

  private:

#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->SuspendLayout();
      // 
      // Form1
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
      this->ClientSize = System::Drawing::Size(369, 280);
      this->Margin = System::Windows::Forms::Padding(2);
      this->Name = L"Form1";
      this->Text = L"Form1";
      this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
      this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
      this->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::Form1_MouseClick);
      this->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::Form1_KeyPress);
      this->ResumeLayout(false);

    }
#pragma endregion
		vector<cpoint> vec;
		char buttoncolor;
	private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) 
			 {
		Graphics ^ pg = CreateGraphics();
        pg->Clear(this->BackColor);
        SolidBrush ^ b = gcnew SolidBrush (Color::Black);

        pg->DrawString ("Left click to draw an x", this->Font,b,10,10);
        pg->DrawString ("Right click will clear the screen", this ->Font,b,10,30);
        pg->DrawString ("Press 'b' , 'g' , 'r' to change colors.  Warning, whatever you do, do not press any other key.", this->Font,b,10,50);
			 
		int i;
		for (i = 0; i <vec.size();i++)
		{
			vec[i].draw();
		}

			 }
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
			 {
				//vec = new vector<cpoint>(); 
				buttoncolor = 'b';

        //Pen ^ pen1 = gcnew Pen(Color::Red);
        //Pen ^ pen2 = gcnew Pen(Color::Green);
        //Pen ^ pen3 = gcnew Pen(Color::Blue);
        

			 }
  private: System::Void Form1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) 
           {
            buttoncolor = e->KeyChar;
			buttoncolor = tolower(buttoncolor);

           }
  private: System::Void Form1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
           {
            String ^ button = (e->Button).ToString();

			Graphics ^ pg = CreateGraphics();
			
			cpoint pt;

            if (button == "Right")
            {
				pg->Clear(this->BackColor);	
            }

            else
            {
              int x = e->X;
              int y = e->Y;
              pt.setx(x);
			  pt.sety(y);
			  pt.setptcolor(buttoncolor);
			  vec.push_back(pt);
			
			}
		   };
}
That is a C# program, not a C++, or even C++/CLI program. Vectors are a C++/STL container. C# has it's own containers, and vector isn't one of them. You could use a List, or just a standard C# array which provides a lot of functionality similar to the STL vector and doesn't have all the pitfalls C/C++ arrays have.
closed account (z05DSL3A)
Looks like C++/CLI to me...

It is possible to mix managed and native code (if the project settings allow it) but it is not recommended, stick to framework provided containers as jRaskell said but you probably forgot that it would be std::vector<...
Last edited on
Hmm, I should probably brush up on my C++/CLI, but I kind of consider it Microsoft's mongrelized bastard child of programming languages.
Topic archived. No new replies allowed.