Thread: Can't seem to find the error here.

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    Can't seem to find the error here.

    Just been doing some coding recently, and I am getting an error in this snippet of code:

    Code:
    	template <class T>
    	void RemoveElement ( vector <T> hay_stack, T needle )
    	{
    		vector<T>::iterator iter = hay_stack.begin();
    		while ( iter != hay_stack.end() && (*iter) != needle ) iter++;
    		if ( (*iter) == needle ) hay_stack.erase ( iter );
    	}
    It tells me " expected `;' before 'iter' ". At first I thought I had forgot a semicolon somewhere else in the file that then caused this error to be thrown, but I don't see any.

    Here's the whole file:

    Code:
    #ifndef __AGENTS_SIMULATION_H
    #define __AGENTS_SIMULATION_H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <vector>
    
    using namespace std;
    
    #include "world.h"
    
    namespace AgentsSimulation
    {
    	extern World SimulatedWorld;
    
    	void InitializeSimulation ( );
    	void RunIteration ( );
    
    	template <class T>
    	int FindVectorElement ( vector <T> hay_stack, T needle )
    	{
    		for ( unsigned int x = 0; x < hay_stack.size(); x++ )
    		{
    			if ( hay_stack[x] == needle ) return x;
    		}
    
    		return -1;
    	}
    
    	template <class T>
    	void RemoveElement ( vector <T> hay_stack, T needle )
    	{
    		vector<T>::iterator iter = hay_stack.begin();
    		while ( iter != hay_stack.end() && (*iter) != needle ) iter++;
    		if ( (*iter) == needle ) hay_stack.erase ( iter );
    	}
    }
    
    #endif /* __AGENTS_SIMULATION_H */
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Try:

    Code:
    	template <class T>
    	void RemoveElement ( vector <T> hay_stack, T needle )
    	{
    		typename vector<T>::iterator iter = hay_stack.begin();
    		while ( iter != hay_stack.end() && (*iter) != needle ) iter++;
    		if ( (*iter) == needle ) hay_stack.erase ( iter );
    	}
    (Since at compile time it is not really known it is a type)

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    iterator is a qualified dependent type on T so you throw a "typename" in front of it.

    Some other things:
    "stdio.h, stdlib.h, time.h" - there are C++ headers for these
    "vector <T> hay_stack" - that's being passed by value (a copy is made) and can be const for FindVectorElement() since it isn't modified.
    "using namespace std" - never in a header
    "iter++" - use pre-increment on iterators
    "if ( (*iter) == needle )" - iter could be pointer to the end(), check for "!= end()"
    "T needle" - could be const reference in both functions
    "FindVectorElement" - there's already a general "find()" in <algorithm>
    "RemoveElement" - this could also be done with the "remove-erase" idiom if you wanted to remove all instances

    gg
    Last edited by Codeplug; 12-30-2008 at 11:02 AM. Reason: found more stuff

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I usually like to think like this:
    If you are trying to use a type declared in a template class or struct (anything that requires a type, basically) and it is an unknown type (ie T, as in your example), then you must add "typename" before it.
    If it is a known type (ie int), then it is not required.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    If you are trying to use a type declared in a template class or struct (anything that requires a type, basically) and it is an unknown type (ie T, as in your example), then you must add "typename" before it.
    Although that works as a rule of thumb, a more minimalistic rule of thumb would be: if you are trying to use a type name declared in a template and it depends on a template parameter, e.g., T::name where T is a template parameter, then you must use typename to disambiguiate the type name from a member name. This corresponds to the reasoning provided by Codeplug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, yes, something like that. That is my line of reasoning.
    If it is a type that somehow involves an unknown type (eg T), then typename must be used if it is a type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM