LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2009, 12:49 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
C++: using templates


Hi,

I am trying to port the Ogre 3D plugin "Particle Universe" to Linux since the author does not show any interest in doing so. The source is open but I am having some problems compiling it.

The problem is with templates and I am not very used to that, so I don't really know what the error is saying me (obviously also it compiles fine under Windows and Visual Studio).

I have broken down the code to the most simple example I could come up with:
Code:
#include <stdio.h>
#include <vector>

namespace MySpace
{
	template <typename T>
	class MyClass
	{
		public:
			MyClass()
			{
				std::vector<T*>::iterator it;
			}
	};
};

int main()
{
	return 0;
}
The error I get is this:
Quote:
error: expected `;' before ‘it’
Any ideas on what is causing this error? Am I missing something? Maybe my build system needs some tweaking?

Thanks,
 
Old 03-08-2009, 01:08 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Code:
			std::vector<T*>::iterator it;
Newer compilers expect to understand more about templated definitions at definition time (rather than waiting until instantiation time) while still not looking into the definitions of other templated classes while processing this definition.

As a result, the compiler does not know soon enough that "iterator" is a typename. (Older compilers didn't care that they didn't find that out until later).

Adding the keyword typename to the right place will fix it.

I have carefully avoided learning the exact rules for that use of "typename" because I think it is very ugly (the line may be complex enough it is unclear which name is qualified by "typename").
Instead, when typename is used for this purpose, I always use one or more extra typedef's so the keyword "typename" can come immediately after typedef and it can be very obvious which name it qualifies (the one that is after the only :: on the line and before the name being declared).

I haven't tested this, but from memory:
Code:
typedef std::vector<T*> container;
typedef typename container::iterator iterator;
iterator it;
I use the names "container" and "iterator" that way only in the common case that the current class deals with only one significant container or iterator type. If the class used more than one type of container or iterator, I would choose type names that say something about the purpose of the container or iterator within the current class.

Last edited by johnsfine; 03-08-2009 at 01:20 PM.
 
Old 03-08-2009, 01:39 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Thanks a lot! I think it fixed that problem. Now I just have all syntax errors and stuff left that Visual Studio for some reason seems to be fine with. :P
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ , templates vedanu84 Programming 5 03-11-2008 03:01 PM
Need help with templates ........ max_rsr Programming 4 04-22-2005 12:53 PM
c++ templates ashirazi Programming 5 07-30-2004 05:17 AM
C++ and templates The_Nerd Programming 4 06-23-2004 01:15 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration