0

I have the following code:

typename List<Pair>::Iterator Iterator;

int changeTableSize(int newSize, int originalSize){
    List<Pair>* temp = new List<Pair>[newSize];
    for (int i = 0; i < originalSize; i++){
        for (Iterator j = elements[i].begin(); j != elements[i].end(); j++){
            Pair p = *j;
            temp[p.key % newSize].insert(p);
        }
    }
    delete elements;
    elements = temp;
    return newSize;
}

The compiler says that j was not declared in this scope. What can cause this?

(Note that List and Pair are implemented by me)

4
  • 1
    What is the type of elements? Your question lacks some context. Commented Jan 4, 2014 at 19:16
  • 1
    Can we have a complete test-case, please. Commented Jan 4, 2014 at 19:17
  • Shouldn't there be a typedef in front of typename? Commented Jan 4, 2014 at 19:53
  • no, rypename is correct Commented Jan 4, 2014 at 19:54

3 Answers 3

2

The problem.

The statement

typename List<Pair>::Iterator Iterator;

declares Iterator as a variable.

It's no longer a type.


Fix.

Did you mean to write typedef, not typename?

That would fix the immediate problem, but not future problems of this kind.

To generally avoid such problems, use C++11 auto for iterators as loop control variables.


Other comments.

Instead of explicitly using new and delete to implement a dynamically sized array, consider just using std::vector. Much more convenient, safe, etc. etc. Less work, more enjoyable!

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation/solution.
1

"Iterator" is it supposed to be a c++ iterator? Because if it is, then that's where the problem is. Use Keyword auto for j and it should do it.

The reason it's giving you the error is because iterator is not fully/properly declared/initialized.

remember that iterators are template classes so by that you should inmediately know that it requires at least one template parameter i.e. forward_list<int>::iterator.

Comments

0

If Iterator is not fully defined you may get this error. Usually you'd see errors about the type being undefined before that though.

Or maybe if you're using an ancient MS compiler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.