7

I am going over some code i wrote in 2006 as an undergrad. It's a simple genetic algorithm library written in C++ using templates. It use to work in 2006 when i coded it with visual studio, but now when i am trying to run it in xcode i get compile errors.

This function is giving me errors:

friend bool operator==(const TSPGenome<T> & t1, const TSPGenome<T> & t2)
{
    // loop through each interator and check to see if the two genomes have the same values
    if(t1.genome_vec->size() != t2.genome_vec->size())
        return false;
    else
    {
        // iterate through each
        vector<T>::iterator it_t1;
        vector<T>::iterator it_t2;
        it_t1 = t1.genome_vec->begin();
        for(it_t2 = t2.genome_vec->begin();
            it_t2 != t2.genome_vec->end();
            ++it_t2, ++it_t1)
        {
            if(*it_t2 != *it_t1)
                return false;
        }
    }
    // everything seems good
    return true;
}

xcode complains about these two lines not having ; before it_t1 and it_t2.

vector<T>::iterator it_t1;
vector<T>::iterator it_t2;

Is it because the vector type it T?

I declared it in the class as follows:

template <typename T>
class TSPGenome : public Genome
{

Any help would be appreciated.

Thanks!

gprime
  • 2,283
  • 7
  • 38
  • 50
  • 3
    This is a dupe of this FAQ entry: [Where to put the “template” and “typename” on dependent names](http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names) – sbi Dec 29 '10 at 21:06
  • 2
    No it isn't. The questions are different -- the answer is the same. Someone who doesn't already know the answer to the question won't guess the relevance of that FAQ entry. Linking to the FAQ is great, but claiming that this question is a "duplicate" is ridiculous. – jalf Dec 29 '10 at 23:51
  • Not related to your problem, but couldn't you just do: return (t1.genome_vec->size() == t2.genome_vec->size()) && std::equal(t1.genome_vec->begin(),t1.genome_vec->end(),t2.genome_vec->begin()); –  Dec 30 '10 at 04:38
  • @jalf: Please see [this discussion](http://chat.stackoverflow.com/transcript/message/222548#222548), especially my reply to John: "I think a question is a dupe of another question if it would have to be answered with the same answer as the other question, _even if the user asking isn't aware of that_." It's funny that only after almost 1.5 years on SO I get to the point of thinking about what "dupe" really means. This must have been discussed before. If there's a meta discussion defining this different from me, please point me to it. Otherwise, if you disagree, we should discuss this on meta. – sbi Jan 02 '11 at 21:22
  • @sbi: it is my understanding that questions closed as duplicates are eventually deleted, which would really make this self-defeating. What we need is that people who ask *this* question can be easily directed to the right answer. If this question sticks around as a pointer to the FAQ entry, I'm happy. If it gets deleted because it was claimed to be a duplicate of a FAQ entry which "asked" a different question, then you're just setting yourself up for having to answer or close the same questions over and over. – jalf Jan 06 '11 at 11:21
  • Finally, regardless of deletion status, I just think "close as dupe" in cases like this sends the wrong message to the person who asked the question. A "we don't care enough to read your question, so we assume it's the same as this completely different question. Now go away and stop pestering us with your duplicate questions" vibe, basically. – jalf Jan 06 '11 at 11:23
  • @sbi: ok, I posted http://meta.stackexchange.com/questions/74080. Also see the latest blog post. – jalf Jan 06 '11 at 12:31
  • @jalf: I disagree. Having the same answer numerous times will result in having it in various degrees of completeness, clarity, and comprehensiveness. Having one answer the others all link to allows the community to put all its effort into that one, thereby improving it to get near perfection. That closed questions are usually also _deleted_ is a bad problem for many dupes, but that doesn't mean we shouldn't close dupes anymore. If you lobby for a close-as-spue-but-don't-delete flag/state/close reason/whatever on meta, you'd have my vote, that's for sure. – sbi Jan 06 '11 at 12:34
  • 1
    @sbi: I'm pragmatic. I think that if closing doesn't achieve what we want, we shouldn't do it. If closing just means that the question eventually gets deleted and so the next guy fails to find it and asks the same question again, then no, we shouldn't close it, because we're just creating more work for ourselves. I totally agree that we need much better tools for reusing answers, and sometimes just parts of answers. Also I still think you're wrong about improving anything to "perfection". There simply isn't a single linear scale to rate this on. Your improvement will make it worse for some. – jalf Jan 06 '11 at 12:51

1 Answers1

18

Use typename when declaring variables whose class is a member of a template-dependent type:

typename vector<T>::iterator it_t1;

A good description of the need for the typename keyword can be found at A Description of the C++ typename Keyword.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285