UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » help please! undefined symbol while printing out a ternary tree

#1 2009-03-11 04:08 PM

mrbears20
Member
Registered: 2009-03-11
Posts: 3

Re: help please! undefined symbol while printing out a ternary tree

I'm trying to print out a ternary tree, but I keep getting an undefined symbol first referenced in file when I try to do 'return out;'.  Now, I'm pretty sure out isn't undefined but I guess I'm wrong.  Here is the section of code I am having trouble with:

ostream & operator<<(ostream &out, const Tree & t){
  return t.display(out, t.root);
}

ostream & Tree::Display(ostream &out, Node *r)const{
  if(r!=NULL){
    display(out, r->child[Node::L]);
    out<<*r<<endl;
    display(out, r->child[Node::M]);
    out<<*r<<endl;
    display(out, r->child[Node::R]);
  }
  return out;
}

I've also tried removing the return out, but that resulted in warning: control reaches end of non-void function.  I tried doing return 0 or return NULL, but it said it was incompatible type to return.  Thanks in advance for any help you can give, and forgive me if this isn't the right place to ask for help with code.

Offline

#2 2009-03-11 08:05 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: help please! undefined symbol while printing out a ternary tree

If it complains that 'out' is unknown then it should do that for the first display()
call, not only for the return.

I don't see why display() is returning an ostream anyway, why not return
nothing and make it a void function?

Shouldn't it be something like:

ostream & Tree::operator<<(ostream &out, const Tree & t)

Otherwise you might be using the wrong << in the display function itself.
But I'm not sure, it's very confusing.

EDIT: Oh wait, undefined symbol? Do you get it at compile time or when
linking? What is the exact error message?

Offline

#3 2009-03-11 09:09 PM

mrbears20
Member
Registered: 2009-03-11
Posts: 3

Re: help please! undefined symbol while printing out a ternary tree

Hmm, I think I get that during compile time, I'm not sure though.  I get it when I try to compile it with my makefile, sorry for not being clearer.  The exact undefined symbol error message is:

Undefined                       first referenced
symbol                             in file
main                                /usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/crt1.o
ld: fatal: Symbol referencing errors. No output written to p2.o
collect2: ld returned 1 exit status
*** Error code 1

Thanks a lot for taking time to look at this, I really appreciate it; I've been working on this problem for most of the day.

Offline

#4 2009-03-11 11:01 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: help please! undefined symbol while printing out a ternary tree

Um, your undefined symbol listed there is "main"...  Apparently, you have no
main() function defined...  That would certainly be a problem!

By any chance is this just one file of a multi-file app, and the main() is defined in
some other file?  If so, you forgot the "-c" arg to the compiler...  I'm guessing your
makefile must override the standard source->object file rule and define its own
specific rule for that source file?  If you really need to do that for some reason, then
add the "-c" arg to the compilation command there, and it should be fine...

Offline

#5 2009-03-12 12:01 AM

mrbears20
Member
Registered: 2009-03-11
Posts: 3

Re: help please! undefined symbol while printing out a ternary tree

wow it was exactly that. 

Thank you so much, I would have NEVER thought to have looked at the makefile.  I cannot say how grateful I am that people such as yourself are so willing to help out.

Offline

#6 2009-03-18 01:03 PM

BdyJ9wcC
Guest

Re: help please! undefined symbol while printing out a ternary tree

  • Index
  • » C++
  • » help please! undefined symbol while printing out a ternary tree

Board footer

Powered by FluxBB