Link to home
Create AccountLog in
Avatar of Rothbard
RothbardFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Problem with getting size of vector

I am writing a function which prints out the contents of a vector of unknown contents (it's only assumed that std::cout works for them). The function is as follows:

#include <iostream>
#include <vector>
#include <string>
#include <iterator>

using namespace std;

template<class T> void printvec(vector<T>& vec)
	// The following line is the part that doesn't work
	std::vector<T>::size_type vecsz=vec.size();
	if vecsz < 1
		throw domain_error("Cannot print contents of an empty vector");
	std::string s = typeid(vec[0]).name();
	int width=cout.width();
	unsigned int count=0;
	cout << "Printing contents of vector of type " << s << std::endl;
	for (vector<T>::const_iterator iter = vec.begin(); iter != vec.end(); iter++)
		cout << setw(2) << count << "\t" << *iter << endl;

Open in new window

The problem is in the line which defines vecsz. I get the error message:

error C2061: syntax error : identifier 'vecsz'
see reference to function template instantiation 'void printvec<std::string>(std::vector<_Ty> &)' being compiled
1>        with
1>        [
1>            _Ty=std::string
1>        ]

Can anyone help me find the source of the error?
Avatar of Zoppo
Flag of Germany image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Rothbard


Argh, thanks!
:o) - you're welcome ...