Solved

Problem with getting size of vector

Posted on 2013-12-18
3
307 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 30

Accepted Solution

by:
Zoppo earned 200 total points
Comment Utility
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
Comment Utility
Argh, thanks!
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
:o) - you're welcome ...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

9 Experts available now in Live!

Get 1:1 Help Now