Sign in to follow this  
Daniel Miller

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
MSVC7.1 (.NET 2003) insists on a typename keyword, other than that it compiles fine.

Scanline(typename std::vector<PixelType>::size_type size)

Share this post


Link to post
Share on other sites
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;
};





Share this post


Link to post
Share on other sites
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>.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this