Jump to content

Odd vector::iterator Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
For some reason I'm getting compilation errors with the following:

#include <vector>
#include <cstdlib>
#include "huffman.h"
#include "bitbuf.h"

using std::vector;

template <typename T>
HuffmanTree<T>::HuffmanTree(const vector<HuffmanNode<T>*>& node_list)
{
	//ERRORS HERE
	vector<HuffmanNode<T>*>::const_iterator iter;
	iter = node_list.begin();

	while(iter != node_list.end())
		add(iter->token,iter->token_freq);
}

//more code...

The errors are:
...huffman_tree.cpp: In constructor `HuffmanTree<T>::HuffmanTree(const std::vector<HuffmanNode<T>*, std::allocator<HuffmanNode<T>*> >&)':

...huffman_tree.cpp:11: error: expected `;' before "iter"
...huffman_tree.cpp:12: error: `iter' undeclared (first use this function)
...huffman_tree.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.)


I'm using GCC on Cygwin for Windows (against my better judgment, I might add). I've tried inserting using namespace std as well as various combinations of prefixing std:: and even including iterator, though I've never had to before. Any ideas?

EDIT: I think I have to dereference iter again in the loop ( (*iter)->token instead of iter->token ), but that's not what's giving me the error.
sudo rm -rf /

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like you're missing:
#include <iterator>
I've also run into times where an extra typedef comes in handy.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I compiled it on Linux just fine without iterator. It's odd, because I've used iterators before and I've never had to include it.

Anyway, I have a new problem: my huffman_node.cpp file compiles just with no errors (even with -Wall and -Werror, but when I link against it, all the functions are missing. The resulting object file is 650 bytes--way smaller than it should be. What could possibly cause that?

Command line:
gcc -Wall -Werror -lstdc++ -O0 -c -o node.o huffman_node.cpp

If I try compiling both huffman_node.cpp and main.cpp at the same time, like so:
gcc -Wall -Werror -lstdc++ -O0 -o huffman main.cpp huffman_node.cpp

...I get linker errors for every function exported by huffman_node.cpp. I've attached the relevant code (for some reason this doesn't accept .h files, I renamed the header .txt).

Attached Files


sudo rm -rf /

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Oh! Duh! I've run into this before (and it's annoying as all get out). When dealing with templates, all of your function definitions need to be defined in the header file.

Here's what happens: you declare something innocuous like this:
dummy.h:
template<typename T>
class dummy_class{
  T _data;
  dummy_class(T);
}
and you create the definition in standard fashion using dummy.cpp:
#include "dummy.h"

dummy_class<T>::dummy_class(T data)
{
  _data=data;
}
and so you create main.cpp:
#include "dummy.h"
int main()
{
  dummy<int> mydummy(3);
  return 0;
}
And you get ERRORS out the wazoo.
The problem happens in the compile/link process. When you compile dummy.cpp, what happens is a bunch of classes are created based on what is needed in dummy.cpp... which is nothing. Then main.cpp looks for the class definition created dummy_class<int> in dummy.o, doesn't find it (because dummy.cpp didn't require it), and throws an error. The solution is to merge dummy.h and dummy.cpp together into dummy.h, so main.cpp can require the compiler to create the code for dummy_class<int>.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
That fixed it, thanks! Design flaw if I ever saw one, though.
sudo rm -rf /

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Having read some of the details of how compilers actually generate code (name-mangling, etc) it actually makes sense. It's about the only way to have compile-time checking on the code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Eh...not so sure. I've read some compiler theory books too; the code is generated on a type-by-type basis. There should be some sort of #sourcefile directive or something, that way you don't have to show your code to a client.
sudo rm -rf /

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are some tricks that can let you do that, such as declaring a specific type in your .cpp file. I think it's one area where some of the other languages handle generics better.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I definitely agree.
sudo rm -rf /