Thread: Strange error.

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    Strange error.

    I'm writing a program to do an organizational chart using linked lists. It has a class called "orgchart" which is the main chart. It consists of a bunch of functions plus a linked list of another class, "orgnode." The orgnode class is basically a generic "person" class, it has their title, name, and level in it. The five files in the program are orgchart.h (class definition), orgchart.cpp (class functions), orgnode.h (def), orgnode.cpp (functs), and final.cpp (the driver program).

    I'm just starting it right now though, so it doesn't do much...except give me the strangest error I've ever seen.

    orgchart.h
    Code:
    // orgchart.h: interface for the orgchart class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_ORGCHART_H__A2A5232E_84BD_4986_B7C9_B929B63F7A0B__INCLUDED_)
    #define AFX_ORGCHART_H__A2A5232E_84BD_4986_B7C9_B929B63F7A0B__INCLUDED_
    
    #include <iostream>
    #include <string>
    #include <list>
    #include "orgnode.h"
    using namespace std;
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class orgchart  
    {
    public:
    	orgchart(string, string, int);
    /*	ostream &list(ostream &);
    private:
    	list<orgnode> chart;
    	list<orgnode>::iterator current;
    	int counter;
    	void locate(int);
    */
    };
    
    #endif // !defined(AFX_ORGCHART_H__A2A5232E_84BD_4986_B7C9_B929B63F7A0B__INCLUDED_)
    orgchart.cpp
    Code:
    // orgchart.cpp: implementation of the orgchart class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "orgchart.h"
    #include "orgnode.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    orgchart::orgchart(string username, string usertitle, int userlevel)
    {
    	orgnode node(username, usertitle, userlevel);
    	cout << node;
    }
    orgnode.h
    Code:
    // orgnode.h: interface for the orgnode class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_ORGNODE_H__990674FB_EED8_4ABB_9F06_FAE80C28AED3__INCLUDED_)
    #define AFX_ORGNODE_H__990674FB_EED8_4ABB_9F06_FAE80C28AED3__INCLUDED_
    #include <list>
    #include <string>
    using namespace std;
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class orgnode  
    {
    	friend ostream &operator<<(ostream &, orgnode &);
    public:
    	orgnode(string, string, int);
    	string title;
    	string name;
    	int level;
    };
    
    #endif // !defined(AFX_ORGNODE_H__990674FB_EED8_4ABB_9F06_FAE80C28AED3__INCLUDED_)
    orgnode.cpp
    Code:
    // orgnode.cpp: implementation of the orgnode class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "orgnode.h"
    #include <list>
    #include <string>
    #include <iostream>
    using namespace std;
    
    orgnode::orgnode(string username, string usertitle, int userlevel)
    {
    	name = username;
    	level = userlevel;
    	title = usertitle;
    }
    
    ostream &operator<<(ostream &output, const orgnode &node)
    {
    	output << node.level << ": " << node.title << " (" << node.name <<  ")";
    	return output;
    }
    Finally, final.cpp
    Code:
    #include "orgnode.h"
    #include "orgchart.h"
    #include <iostream>
    #include <string>
    
    void main()
    {
    	string username, usertitle;
    	int userlevel;
    	
    	cout<<"Enter name"<<endl;
    	cin >> username;
    
    	cout <<"enter title"<<endl;
    	cin>>usertitle;
    
    	cout<<"enter level"<<endl;
    	cin>>userlevel;
    
    	orgchart chart(username, usertitle, userlevel);
    }


    The error (I'm not using a code tag here, because the table stretching would be terrible):

    Compiling...
    orgchart.cpp
    Linking...
    orgchart.obj : error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class orgnode &)" (??6@YAAAV?$basic_ostream@
    DU?$char_traits@D@std@@@std@@AAV01@AAVorgnode@@@Z)
    Debug/final.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    final.exe - 2 error(s), 0 warning(s)


    I have no idea what that means. I don't even know where to begin. I feel bad for asking a lot of questions when I can answer only a few, but I'm really stuck here. Help would be appriciated.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Wild guess here. Maybe you need a:
    using namespace std;
    in your final.cpp

    Also, it might work better if your includes in final.cpp were in this order:
    #include <iostream>
    #include <string>
    #include "orgnode.h"
    #include "orgchart.h"
    using namespace std;

    Since you didn't include <iostream> in "orgnode.h"

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you could try putting a:
    #include <iostream> in your orgchart.cpp, since this is the module where the linking error occurs.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    I tried your suggestions. No luck, sadly. Do you think it could be a problem with one of the included header files (iostream, list, ...)?

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You include iostream, string, and list more than once. Make only one of the header files include them. Didn't really get to the rest of it.

    ORGNODE.H
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    #include "ORGNODE.CPP"
    
    //whatever code

    ORGNODE.CPP
    Code:
    //whatever code

    ORGCHART.H
    Code:
    #include "ORGNODE.H"
    #include "ORGCHART.CPP"
    
    //whatever code

    ORGCHART.CPP
    Code:
    //whatever code

    /*EDIT*/ Fixed Typos /*EDIT*/
    Last edited by golfinguy4; 04-30-2002 at 04:22 PM.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    That doesn't seem to be it either. I took out some of re-includes without additional problems, but it didn't solve the original one either.
    Last edited by Will; 04-30-2002 at 04:15 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >class orgnode
    >{
    > friend ostream &operator<<(ostream &, orgnode &);

    This should be:
    friend ostream &operator<<(ostream &, const orgnode &);

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    That was it. It is times like this that I hate programming. Thanks a bunch for your help though, I would've never caught that.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    just out of curiosity is ostream included in the <iostream.h>? I always thought it was in <ostream.h>... Just wondering...
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I think that iostream includes ostream and istream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM