Thread: STL list recursion

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    4

    Question STL list recursion

    I need access to a list of a class called neuron passed in a function. The problem is this function is part of the neuron class itself. When I try to access the passed neuron with an iterator I get a compile error. Im not sure if this is recursion (in which case the post title is wrong). If the problem is because the class is trying to access itself before it's fully defined then is there any way around it?
    Code:
    class neuron {
    public:
       //..
       deque<dend> dends;  //class with 3 int variables
       //..
       short calcExcitation(const list<neuron> &ln) {
          float total=0;
          neuron searchNeuron;
          list<neuron>::iterator iter;
          for(int i=0;i<dends.size();i++) {
             searchNeuron.neuronID=dends[i].ID;
             iter=find(ln.begin(), ln.end(), searchNeuron); //error here
             if(iter!=ln.end())
                total+=dends[i].weight * (*iter).excitation;
          }
          return excitation=total>ExcWeightThr?1:0;
       }
    };
    
    list<neuron> baseNeuron;  //global variable
    The error I get is:
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty,_Ax>::const_iterator' (or there is no acceptable conversion)
    with
    [
    _Ty=neuron,
    _Ax=std::allocator<neuron>
    ]

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    return excitation==total>ExcWeightThr?1:0;
    Try that.
    Do not make direct eye contact with me.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> list<neuron>::const_iterator iter;

    Do you know why?

    gg

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    4
    Thx codeplug. That made it work. After that I got 2 errors that neuron and dend didnt have == defined so I overloaded this operator for the both of them and it compiled fine.

    To answer your question, I don't why. I didnt even know there was a const_iterator type with an underline. I know that const by itself means that a variable cant be altered but that's about it. When it comes to consts with container classes and iterators it just bogles my mind.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A const_iterator means that the objects referenced by the iterator may not be modified. This is a necessity for containers that may not be modified.
    Look at a reference for std::list and you'll find that it has two begin() functions. One returns an iterator, the other a const_iterator. They are distinct because the second is marked const - it can be called on a const container.

    ln is passed like
    const list<neuron> &ln
    which means it's a const container. You can only call the const version of begin on it, so you only get a const_iterator.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM