Hi, Trying to learn STL.

I tried implementing the dynamic array with the STL.

I wrote the following code and tried with int array works fine, but when i try with string array input, i get the error..can you please help me,

#include <iostream>
#include <vector>

using namespace std;

template < typename T>
class dyn_arr{

public:
        //Dedault constructor
        dyn_arr() {};

        //Constructor
        dyn_arr( int rows, int cols)

        {
            arr.resize(rows);


           for(int i=0; i<rows; i++)
           {
             vector<int> row;

             for(int j=0; j<cols; j++)
             {
                row.resize( cols);
                fill(row.begin(), row.end(), "hello");
             }
             arr.push_back(row);
           }
         }

        void print_array(int rows, int cols)
        {
          for(int i=0; i<cols; i++)
          {
             for(int j=0; j<rows; j++)
                cout << arr[i][j] << "\t";
             cout << endl;
          }
        }

private:
        vector< vector< T > > arr;
};

int main() {

        int rows=2, cols=2;

        dyn_arr<int> loc_arr(rows,cols);

        loc_arr.print_array(rows, cols);
}


For string: main looks like this

int main() {

        int rows=2, cols=2;

        dyn_arr<string> loc_arr(rows,cols);

        loc_arr.print_array(rows, cols);
}

Error:

dyn_arr.cpp: In constructor `dyn_arr<T>::dyn_arr(int, int) [with T =
std::string]':
dyn_arr.cpp:58: instantiated from here
dyn_arr.cpp:32: error: no matching function for call to `
std::vector<std::vector<std::string, std::allocator<std::string> >,
std::allocator<std::vector<std::string, std::allocator<std::string> > > >::
push_back(std::vector<int, std::allocator<int> >&)'
/usr/include/c++/3.3.1/bits/stl_vector.h:596: error: candidates are: void
std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
std::vector<std::string, std::allocator<std::string> >, _Alloc =
std::allocator<std::vector<std::string, std::allocator<std::string> > >]
/usr/include/c++/3.3.1/bits/stl_algobase.h: In function `void
std::fill(_ForwardIter, _ForwardIter, const _Tp&) [with _ForwardIter =
__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >,
_Tp = char[6]]':
dyn_arr.cpp:30: instantiated from `dyn_arr<T>::dyn_arr(int, int) [with T = std::string]'
dyn_arr.cpp:58: instantiated from here
/usr/include/c++/3.3.1/bits/stl_algobase.h:517: error: invalid conversion from
`const char*' to `int'

I just made some small change in your class definition removing the .resize() and fill() functions as I didn't find their need.

template <typename T>
class dyn_arr{
public:
        //Dedault constructor
        dyn_arr() {};
		//Constructor
        dyn_arr( int rows, int cols)
        {
            for(int i=0; i<rows; i++)           
			{
				vector<T> row; // Your mistake was hardcoding int here as vector<int> as it will need vector<string> here
				for(int j=0; j<cols; j++)
				{
					row.push_back("hello");
				}
				arr.push_back(row);
			}
        }

        void print_array(int rows, int cols)
        {
			for(int i=0; i<rows; i++)
			{
				for(int j=0; j<cols; j++)
					cout << arr[i][j] << "\t";
				cout << endl;
			}
        }

private:
        vector<vector<T>> arr;
};

And ya if you are using it as a string array (dynamic) dont forget to include <string> header file.And it works. :)

commented: Here's a vote back. Good Catch. +4

Thanks a lot csurfer, for the quick reply ! & pointing out my mistake. thanks a lot again.

I just made some small change in your class definition removing the .resize() and fill() functions as I didn't find their need.

template <typename T>
class dyn_arr{
public:
        //Dedault constructor
        dyn_arr() {};
		//Constructor
        dyn_arr( int rows, int cols)
        {
            for(int i=0; i<rows; i++)           
			{
				vector<T> row; // Your mistake was hardcoding int here as vector<int> as it will need vector<string> here
				for(int j=0; j<cols; j++)
				{
					row.push_back("hello");
				}
				arr.push_back(row);
			}
        }

        void print_array(int rows, int cols)
        {
			for(int i=0; i<cols; i++)
			{
				for(int j=0; j<rows; j++)
					cout << arr[i][j] << "\t";
				cout << endl;
			}
        }

private:
        vector<vector<T>> arr;
};

And ya if you are using it as a string array (dynamic) dont forget to include <string> header file.And it works. :)

@ Moderator, sorry, by mistake i pressed the wrong rating button to csurfer and is -ve ! i am not able to change it..please change to +ve

Quick input: when tried with array size 2,2 works ok. But with rows=2, cols =3 coredumps !!!

You have interchanged rows and cols in for loops of your print_array function,and its an obvious core dump. Change that and your code (with the changes I have made in my previous post) works.

P.S : Try to solve such simple problems by looking at your code more cautiously. Its your code, your brain child you should atleast spend some time knowing it :)

sorry, i know, i should have checked, i just posted..i will check before posting !!!

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.