Combination with template and vector

Hello everyone!
I'm new here, also new in c++ programing, so I have problem. I'll post code here, then I gonna explain a situation...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<vector>
using namespace std;
template<class T>
void InputVector(vector<T>a)
{
	
	cout<<"input 5 elements of vector"<<endl;
	for (int i=0;i<5;i++)
	{
		cin>>a[i];
		cout<<endl;
	}
};
template<class T>
void OutputVector(vector<T> &a)
{
	cout<<"-----ELEMENTS-----"<<endl;
	vector<T>::iterator ITERATOR;
	for (ITERATOR=a.begin();ITERATOR<a.end();ITERATOR++)
	{
		cout<<*ITERATOR<<endl;
	}
};
void main ()
{
	vector<int> a(5);
	InputVector(OutputVector(a));
}


As you can see, I want to have one template for input elements of vector, and one template for output, to print elements.
When I build this, there is showing a error:

Error 1 error C2784: 'void InputVector(std::vector<T>)' : could not deduce template argument for 'std::vector<T>' from 'void'

I was trying on other way, to change void main in next style:

1
2
3
4
5
6
void main ()
{
	vector<int> a(5);
	InputVector(a);
	OutputVector(a);
}


So, when I build like that, it has no errors, but it doesn't print my elements.

Can somebody help to me?I hope that you can understand what I'm trying to do.
Thanks!


OK, you should not ever use void main.
And I don't think you should have semicolons after the functions when you define them.
And that means that it can't determine what the type is. You're calling input vector on the return from an output function that returns void. It's inputting to a void. That's impossible.
Last edited on
Yes, you are right...so I made the next:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<vector>
using namespace std;
template<class T>
void InputVector(vector<T>a)
{
	
	cout<<"input 5 elements of vector"<<endl;
	for (int i=0;i<5;i++)
	{
		cin>>a[i];
		cout<<endl;
	}
}
template<class T>
void OutputVector(vector<T> &a)
{
	cout<<"-----ELEMENTS-----"<<endl;
	vector<T>::iterator ITERATOR;
	for (ITERATOR=a.begin();ITERATOR<a.end();ITERATOR++)
	{
		cout<<*ITERATOR<<endl;
	}
}
int main ()
{
	vector<int> a(5);
	OutputVector(InputVector(a));
}


But it crash on building... It show:

Error 1 error C2784: 'void OutputVector(std::vector<T> &)' : could not deduce template argument for 'std::vector<T> &' from 'void' Line 28
Inputvector doesn't return anything either, so you're outputting the contents of a void.
When you say function(function()), you are passing the outer function the RETURN of the inner function. You are not passing the argument list of the inner function. Therefore, rather than nesting calls in that juvenile way when it saves no space and obviously doesn't work, you should separate the calls like you had when it was sensible:
1
2
inputvector(a);
outputvector(a);
Last edited on
When you do this:

 
OutputVector(InputVector(a));


What happens is:
1) InputVector is called with 'a' as a parameter
2) whatever InputVector returns gets passed to OutputVector

The problem is, InputVector doesn't return anything because it's void -- so you're not passing anything to OutputVector.

You probably meant to do this:

1
2
InputVector(a);
OutputVector(a);


EDIT: doh, too slow
Last edited on
How'd you edit that so fast?
I clicked the edit button, made my changes, then clicked Submit.

=P
InputVector should take a reference not by value otherwise the main function will not see the updated values.

Secondly your InputVector function does not return a value. It has a void return type so the compiler does not understand what you are trying to send to the OutputVector function.
Last edited on
I clicked the edit button, made my changes, then clicked Submit.

I lold.
And actually, that's true. It should take a reference. Good catch. (It seems that, in general, passing an iterator to the start and end is more common than passing the vector itself by ref. STL is a case in point.)
From a design standpoint, let me give you a bit of advice. The InputVector is making an assumption that the container has 5 values which in this case happens to be true. However the assumption is unnecessary because you can simply ask the vector how big it is using its size function like so.

1
2
3
4
5
6
7
8
9
10
11
12
13
void InputVector(vector<T>& a)
{
   int size(a.size());
   if(size > 0)
   {
      cout<<"input " << size << " elements of vector"<<endl;
      for(int i=0;i < size; i++)
      {
         cin>>a[i];
         cout<<endl;
      }
   }
}


There are other ways of doing it but since you want to ask the user for a specific number of entries this is probably easiest for you. If you pass two iterators then you'd have to do a pointer diff on them to determine how many values to ask the user for which is a little harder. I also think that it is more secure to pass the object by reference. You can't really validate iterators and invalid iterators could cause undefined behavior if the caller screws up. Sometimes I actually like it better if the container is passed by reference like that.
Last edited on
OK guys, there is solve:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<vector>
using namespace std;
template<class T>
void InputVector(vector<T> &a)
{
	
	cout<<"input 5 elements of vector"<<endl;
	for (int i=0;i<5;i++)
	{
		cin>>a[i];
		cout<<endl;
	}
}
template<class T>
void OutputVector(vector<T> a)
{
	cout<<"-----ELEMENTS-----"<<endl;
	vector<T>::iterator ITERATOR;
	for (ITERATOR=a.begin();ITERATOR<a.end();ITERATOR++)
	{
		cout<<*ITERATOR<<endl;
	}
	
}
void main ()
{
	vector<int> a(5);
	InputVector(a);
	OutputVector(a);
	
}


So, IT WORKS!!! @kempofighter, I made reference on InputVector, so it works very fine...Thanks @tummychow, thanks @disch, for your time, thank you very much, i love this forum ;)

Problem is solved...
@kempofighter, tnx on advice! I know for that, but for now, I just want to try this "side" with template...tomorrow I'll gonna to do more difficulty program, with much bigger function!
Again, tnx mate on help ;)
Topic archived. No new replies allowed.