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)

Problem.. you didn't include anything.. Unlike C# where everything is included for you or you add a simple reference.. In C++ we need includes.. Quick Tip..

When you see errors like "error C2678: binary '[' : no operator found which takes" well either you're missing an include or it really doesn't have that operator..

Anyway.. Add these to the top of your program right under the #Pragma Once

include <vector>
include <iostream>

using namespace std; `//<--- that using namespace stuff isn't needed if you plan to do std::vector<std::vector<char>>
//But if you do vector<vector<char>> then you need the namespace std;

All in all your program will look like:

#pragma once
#include <vector>
#include <iostream>
#include <Windows.h>

using namespace std;

namespace WindowsFormsApplication1 {

    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
    /// </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;

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

            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(' ');
                }
            }

        }
#pragma endregion
    };
}

Also if anyone is reading this.. PLEASE teach me how to use the new code tags and stuff.. this is soooooo confusing!

While I thank you triumphost for the help ... unfortnatly what you posted didn't work (dang).

First of all I actually forgot to paste in the #includes I had but the only one I didn't have was the < windows.h >

I tried adding that header file and doing what you did (moving my declare into the Windows Form Designer in Initialized Component) and all this lead to was more undeclared variables

(Also yeah I don't like this new style, for one it chops up the code kind of ... that blue background is actually really helpful, that and I put "[" in and it acts like a link, hmm and < > blocks out text it appears)

Not sure why it wouldn't work :S This is actually the full code AND it compiles:

#pragma once
#include <vector>
#include <iostream>
#include <Windows.h>

using namespace std;

vector<vector<char>> CodeTable;

namespace WindowsFormsApplication1 {

    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
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
            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(' ');
                }
            }
        }

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

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

#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(284, 261);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);
        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 MessageBox::Show(CodeTable[0][0].ToString());
             }
    };
}
commented: What he posted worked! Would love to know how now +2

You're mixing C++/CLI with STL.
You'd be better-off picking one or the other.

Is this to be a 2d array of chars or key/value pairs or what?

Okay trimphost I see where you moved the initial call to (where you declare the vector variable), I'll give that try later today and see how it will do

As for thines01, what exactly do you mean by C++/CLI and STL??? (and this is a 2d vector to support chars, using an x,y type location system to aquire a value)

THANK YOU!

Trimphost by moving the line

vector<vector<char> > CodeTable;

up were you declarced namespace std seemed to fix it. I am not use to declaring variable there (I usually put them where you saw in my original code). So thank you, this somehow fixed it (again I have no clue why and I would really like to know how)

I guess I should mark this solved, but I would really like to know why Trimphost's simple fix of moving that one line of code fixed ALL the errors I was being thrown (I was even able to reference the variable in my encode and decode classes instead of passing in the varialbe)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.