Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

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?
0
Rothbard
Asked:
Rothbard
  • 2
1 Solution
 
ZoppoCommented:
Hi Rothbard,

if it's not a typo the error is simply caused by missing ( and ) here:
      if vecsz < 1
This should be
      if ( vecsz < 1 )

Hope that helps,

ZOPPO
0
 
RothbardAuthor Commented:
Argh, thanks!
0
 
ZoppoCommented:
:o) - you're welcome ...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now