Solved

Problem with getting size of vector

Posted on 2013-12-18
3
315 Views
Last Modified: 2013-12-18
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
Comment
Question by:Rothbard
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 200 total points
ID: 39726641
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
 

Author Comment

by:Rothbard
ID: 39726648
Argh, thanks!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39726651
:o) - you're welcome ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now