Avatar of Rothbard
Rothbard
Flag 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;
		count++;
	}
	cout.width(width);
}

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?
C++

Avatar of undefined
Last Comment
Zoppo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Zoppo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rothbard

ASKER
Argh, thanks!
Zoppo

:o) - you're welcome ...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes