-
September 11th, 2011, 08:22 AM
#1
How do you declare a vector in VS C++2008/managed?
Hello. This is not homework. I am just studying different tools for my job as a programmer, trying to decide which will be best for me to use at work. One of the tools is Visual Studio (which I am finding out I do not care for, Code::Blocks is better, but in case I must use all managed code at work, I want to know how to do it)
I do not wish to mix managed and unmanaged code in this program.
What is the proper way to declare a vector using only managed code?
Here is what I have (the relevant parts anyway)
Code:
#pragma once
#include <math.h>
#include <vector>
namespace Calculator {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std; //for vector
//lots of code in between...
private: System::Void button21_Click(System::Object^ sender, System::EventArgs^ e) {
array< String^ >^ sentence = gcnew array< String^ >(ARRAY_SIZE);
//I included the array because it works, and I assume the vector declaration should be similar?
vector<String^> word = gcnew vector<String^>; //what is missing on this line?
//this gives me: error C2726: 'gcnew' may only be used to create an object with managed type
String^ x = "Hello! ";
word.push_back(x);
//error C2440: 'initializing' : cannot convert from 'std::vector<_Ty> *' to 'std::vector<_Ty>'
//I assume that managed code would have a -> instead of the dot member operator, wouldn't it?
}
}
Since I posted this before, in a different section (sorry!), I found a solution that seems to work, though I still don't know if this is the 'right' way to do it.
Code:
System::Collections::Generic::List<String^> word;
word.Add("Hello!");
word.Add("Goodbye!");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|