Jump to content
  • Advertisement
Sign in to follow this  

std iterator issue

This topic is 4724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing some coding, and for some reason my compiler is throwing a wierd error for me with iterators. Here are two examples:
map<const char*, FreeList<BC>, ltstr>::iterator itr = registered.find(className);

and
list<U*>::iterator itr = freelist.begin();
for(itr; itr != freelist.end(); ++itr)
   ...

In both cases, the compiler says "error: expected ';' before "itr"" and in the last one, it says "itr not declared in the scope" (assumingly because of the previous error). In both cases, I #include <map> and <list>, and have stated using namespace std; Any ideas as to what is going on? Thanks! -visage

Share this post


Link to post
Share on other sites
Advertisement
Can you duplicate the error out of context? (and then post the exact results).

Generally "expected ';' before <variable>" means that the compiler dosn't think that what you think is a type, is a type.

In other words, it thinks there is no such thing as a "map<const char*, FreeList<BC>, ltstr>::iterator" or "list<U*>::iterator".

One assumes that you're "using namespace std" correctly, so perhaps there is something else wrong with your type (with the information you have posted, I can't really tell). Check your template parameters are there and useable. Also, what compiler?

Alternativly, there's something wrong on a preceeding line.

The second error will have been caused by the first. It can't declare itr, so it thinks itr dosn't exist (and you get the same errors as if that were the case).

Share this post


Link to post
Share on other sites
Here is some of the code:

In the header file:

template <class BC>
class ObjectFactory
{
private:
map<const char*, FreeList<BC>*, ltstr> registered;

...



The actual method...

template <class BC>
bool ObjectFactory<BC>::unregisterClass(std::string className)
{
map<const char*, FreeList<BC>*, ltstr>::iterator cur = registered.find(className);
if(cur != registered.end())
registered.erase(cur);
return true;
}




With the FreeList

template <class U> class FreeList
{
private:
list<U*> freelist;

...




And the rest of the source for the method...

template <class U>
void FreeList<U>::FreeAll()
{
list<U*>::iterator itr = freelist.begin();
for(itr; itr != freelist.end(); ++itr)
{
delete *itr;
}
freelist.clear();
}




On an unrelated note, what is the correct syntax for a template method in a template class? How do I set up that method prototype?

Thanks for the help so far guys

Share this post


Link to post
Share on other sites
Are you sure they're there and useable?

Try putting these in as local variables and seeing if it works:


// first one:
std::vector<BC> test1;
std::vector<FreeList<BC> > test2;

// second one:
std::vector<U> test3;


That should ensure that your types are usesable (if they are, then it's something more complex, and if they arn't you'll have to make them so).

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
All three tests worked...and no, I am putting them in a .cpp on their own that includes the header that declares them.


Uhmn - this might be your problem. Template definitions must be in the header, not in the .cpp file (unless you go pretending the .cpp file is a header, or use a specific compiler that supports template function definitions in C++ files).

Share this post


Link to post
Share on other sites
Can you explain a little more what you mean? Maybe with an example?

Do you mean all my methods must be inline, or in the .h file? Why is that?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!