You are here:

C++/iterator declaration in C++ template under gcc

Advertisement


Question
Hi, I'm need to write cross-platform code. I wrote a custom list which compiles and works fine in MS visual studio 2003 but does not compile with G++ on linux. (gcc version 4.1.1 20070105 (Red Hat 4.1.1-51))



There error which appears for the 2nd typedef line ( the one for the iterator ) states this:



type "std::list<T, std::allocator<_CharT> >" is not derived from type "CustomList<T>"





I can not figure out what is wrong and how to fix it.. Can you help?



Thanks,



Daniel



----- this snippet illustrates the problem ----

#include <list>



template<class T>

class CustomList

{

public:

typedef std::list< T >          CustomListType;

typedef std::list< T >::iterator CustomListIterType;

bool FindItem(int pParam, T& pItem)

{

  CustomListIterType lIter;

  lIter = aInternalList.begin();

  while( lIter != aInternalList.end() )

  {

    if( (*lIter)->SomeCall(pParam) == true )

    {

      pItem = (*lIter);

      return true;

    }

    lIter++;

  }

  return false;

};

CustomListType& mGetItems(){ return aInternalList; };

private:

CustomListType aInternalList;

};

Answer
Hello Daniel, thank you for the question.

Could you please include your declaration wherever you are using this list? I'm assuming you are using it for std::strings based on the error, but it could be something else.

Template code isn't actually compiled until it is instantiated, and I need to see the template object declaration in main or wherever it might be to understand that error.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.