-
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.
-
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"
-
Or you could try putting a:
#include <iostream> in your orgchart.cpp, since this is the module where the linking error occurs.
-
I tried your suggestions. No luck, sadly. Do you think it could be a problem with one of the included header files (iostream, list, ...)?
-
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
ORGCHART.H
Code:
#include "ORGNODE.H"
#include "ORGCHART.CPP"
//whatever code
ORGCHART.CPP
/*EDIT*/ Fixed Typos /*EDIT*/
-
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.
-
>class orgnode
>{
> friend ostream &operator<<(ostream &, orgnode &);
This should be:
friend ostream &operator<<(ostream &, const orgnode &);
-
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.
-
just out of curiosity is ostream included in the <iostream.h>? I always thought it was in <ostream.h>... Just wondering...
-
I think that iostream includes ostream and istream.