It's been so long since I used C++...

Started by
4 comments, last by Daniel Miller 18 years, 8 months ago
template <class PixelType>
class Scanline
{
public:
	Scanline(std::vector<PixelType>::size_type size) //error
	{
		while (pixels.size() < size)
		{
			pixels.push_back(0);
		}
	}

	PixelType& operator[](int x)
	{
		return pixels[x];
	}

private:
	std::vector<PixelType> pixels;
};

What is wrong with that code? I am getting the following error: error C2059: syntax error : ')' It points to the commented line. [Edited by - Daniel Miller on August 1, 2005 3:39:58 PM]
Advertisement
[edit: someone deleted their post]

[sad]
I thought that was how you declared constructors in C++.
Yeah, you're right; I wasn't paying attention. I don't see a problem, but then I haven't used C++ in a year or so.
MSVC7.1 (.NET 2003) insists on a typename keyword, other than that it compiles fine.
Scanline(typename std::vector<PixelType>::size_type size)
In standard-compliant compilers, the "typename" keyword MUST be used to distinguish typenames from static constants within the context of a template. So, where you used "std::vector<PixelType>::size_type", you should add the typename keyword in front of it. I would personally typedef this as well, like the following snippet of code.

template <class PixelType>class Scanline{public:        typedef typename std::vector<PixelType>::size_type size_type;	Scanline(size_type size)	{		while (line.size() < size)		{			pixels.push_back(0);		}	}	PixelType& operator[](size_type x)	{		return pixels[x];	}private:	std::vector<PixelType> pixels;};




Thanks a lot, that got it working. [smile]

Wow, how long has that been the case (forever, I assume [sad])? More importantly, what about that specific case requires typename?

I had always thought typename was only used like this: template <typename T>.

This topic is closed to new replies.

Advertisement