Jump to content

Don't really understand the compile error, help?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
I am still new to C++. I understand what the compile error is saying but don't understand why basically.

Error:
LCS.cpp:18: error: no matching function for call to ‘dressArray(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<std::vector<char, std::allocator<char> >, std::allocator<std::vector<char, std::allocator<char> > > >&, std::string&, std::string&)’



#include <iostream>

#include <vector>


using namespace std;


template<class T>

void dressArray(vector<T> &array, vector<T> &dirArray, string strY, string strX);

template<class T>

void printArray(vector<T> &array, string strY, string strX);  


int main() {

	

	string strY	= "AKBMCND";

	string strX = "AABACD";

	

	vector < vector <int> > LCSArray(strY.size()+1, vector<int>(strX.size()+1));

	vector < vector <char> > directionArray(strY.size()+1, vector<char>(strX.size()+1));	

	dressArray(LCSArray, directionArray, strY, strX);

	

	

	printArray(LCSArray, strY, strX);

	printArray(directionArray, strY, strX);


	return 0;

}


template<class T>

void dressArray(vector<T> &array, vector<T> &dirArray, string strY, string strX) {

	

	for(int i = 1; i < strY.size()+1; i++) 

	{

		

		for(int j = 1; j < strX.size()+1; j++) 

		{

			

			if((i == 0) || (j == 0)) 

			{

				array[i][j] = 0;

				dirArray[i][j] = 'N';

				

			}

			if((i > 0) && (j > 0))

			{

				if(strY.at(i-1) != strX.at(j-1))

				{

					if(array[i][j-1] > array[i-1][j])

					{

						array[i][j] = array[i][j-1];

						dirArray[i][j] = 'L';

					}

					else 

					{

						array[i][j] = array[i-1][j];

						dirArray[i][j] = 'U';

					}					

				}

				if(strY.at(i-1) == strX.at(j-1))

				{

					array[i][j] = (array[i-1][j-1])+1;

					dirArray[i][j] = 'D';


				}	

			}

		}

	}

}									


template<class T>

void printArray(vector<T> &array, string strY, string strX) {

	

	for(int i = 0; i < strY.size()+1; i++) 

	{

		for(int j = 0; j < strX.size()+1; j++) 

		{

			cout << "" << array[i][j] << " ";

		}

		cout << endl;

	}

}




#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Your problem is simple, you're using a function template, and passing in two different vectors each holding vector arrays of their own. When you call a Function Template, you have to give it two template parameters, so do this instead:

template<class T, class U>
void dressArray(vector< vector<T> > &array, vector< vector<U> > &dirArray, string strY, string strX);
template<class T>
void printArray(vector<T> &array, string strY, string strX);  

int main() {
	
	string strY	= "AKBMCND";
	string strX = "AABACD";
	
	vector < vector <int> > LCSArray(strY.size()+1, vector<int>(strX.size()+1));
	vector < vector <char> > directionArray(strY.size()+1, vector<char>(strX.size()+1));	
	dressArray<int, char>(LCSArray, directionArray, strY, strX);
	
	
	printArray(LCSArray, strY, strX);
	printArray(directionArray, strY, strX);

	return 0;
}

template<class T, class U>
void dressArray(vector< vector<T> > &array, vector< vector<U> > &dirArray, string strY, string strX) {
You need to have two different classes in your template since you're passing two different vectors, which are each different types of objects as far as the compiler is concerned, they're not interchangeable. As such, your vector< vector<int> > is different from your vector< vector<char> >, therefore you must pass two different template objects. There you go, should run now. ^_^

EDIT: Also, the reason I said "vector< vector<T> >" instead of just "vector<T>" and using "dressArray< vector<int>, vector<char> >" is because your function dressArray relies on the passed vectors being a two dimensional vector array, since you use "array[j]" in your code, mandating a 2D array. As such, to maintain proper type safety (or a semblance of it), I used "vector< vector<T> >", and it makes calling it easier too. :) Also, I did use <int, char>, which isn't [i]technically necessary, since the compiler can automatically detect this, the primary problem was you passed two different template parameters when the template only requested one.

Edited by ZekeDragon, 16 September 2009 - 06:26 PM.
See EDIT

Wow I changed my sig!

#3
MichaelNQ

MichaelNQ

    Newbie

  • Members
  • PipPip
  • 11 posts
Thank you a bunch, that makes sense.