So first of all let me say I am pretty new to C++ and find it very difficult to learn (especially when compared to say C#)
Anyway I am working on a program that requires the use of a 2D array or a similar container. After talking with a professor about it he suggested I used vectors seeing as they meet the criteria I was looking for.
Well I am trying to convert my program over to use Dynamic 2D Vectors and I am getting nothing but errors thrown at me.
Here's the scenario (and I think if fixed will fix everything else)
//-------------------------------------------------------------------------------------
public ref class Form1 : public System::Windows::Forms::Form
{
vector<vector<char> > codeTable;
//Adding a & does something ... not sure if it's what I want
//-------------------------------------------------------------------------------------
public:
Form1(void)
{
InitializeComponent();
//vector<vector<char> > codeTable;
int k = 0;
for(int i = 0; i < 5; i++)
{
codeTable.push_back(vector <char>());
for(int j = 0; j < 5; j++)
{
codeTable[i].push_back(' ');
}
}
}
//-------------------------------------------------------------------------------------
Now here's the problem, what you see here doesn't work. I mean I am getting errors like
error C2228: left of '.push_back' must have class/struct/union
as well as
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
This error is refering to the line
codeTable[i].push_back(' ');
error C2663: 'std::vector<_Ty>::push_back' : 2 overloads have no legal conversion for 'this' pointer
This error is refering to this line of code
codeTable.push_back(vector <char>());
error C4368: cannot define 'codeTable' as a member of managed 'GarCodeC_1v1::Form1': mixed types are not supported
This error for some reason shows up last in the list or errors (and really puzzles me) for this chunck of code
vector<vector<char> > codeTable;
These errors go on an on in later functions down the way for other functions such as the following
error C2662: 'std::vector<_Ty>::empty' : cannot convert 'this' pointer from 'std::vector<_Ty>' to 'const std::vector<_Ty> &'
This error referes to the line of code that does this
if(codeTable.empty() == false)
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
This line of code for this error (and this error shows up for all instances I have for this piece of code)
codeTable [i][j] = static_cast<char>(letterCount);
I would like to point out that I found something interesting. If you read the comment that I left in the large segment of code at the top if I do place a '&' before the variable name like this:
vector<vector<char> >& codeTable;
The only error I get is the following
error C2758: 'GarCodeC_1v1::Form1::codeTable' : must be initialized in constructor base/member initializer list
I would also like to point out that the commented code in the first code snippet
//vector<vector<char> > codeTable;
If I call that declaration instead of above the only errors I get are undefined variable errors (which makes sense but I thought I would bring this up as it's poking me I just can't put my finger on it for sure)
So can anyone help me out here? For some reason I feel it has to deal with how I am declaring the initial vector as I went and changed some of the code in some other classes and removed all their error as seen in the code below
//Form1.h
string readIn = encode::code(codeTable, rtbText, characterCodeLength); //calls the function
//encode.h
static string code(vector<vector<char> > codeTable, string input, int characterCodeLength);
//encode.cpp
string encode::code (vector<vector<char> > codeTable, string input, int characterCodeLength)
{
... //a bunch of code (not including where codeTable is used)
encodeMessage+= codeTable[letterIndex][tempCharConvert];
... //more code
}
I would also like to point out that when I tryed changing in the passed in variable to
vector<vector<char> >& codeTable
I got the error
error C2664: 'encode::code' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> &'
Which sucks cause I would really like to reference these and not create a whole new variable (trying hard to work on my memory management since that seems to be one of the things C++ is known for
So please any help would be greatly be appreciated I have digging all around the web and my professor wasn't entirely sure (the scope of the class doesn't cover vectors ... plus I think he's alittle more old school) (oh also I am doing this on VS2010 Prof. and a W7 Prof. 64-bit machine)