0

Okay, so I haven't used C++ for a couple months, and my one problem has always been using multiple headers. Currently my problem is all my headers for classes are linked to a main header, which the .cpp files use. I'm using ifndef's to make sure nothing gets repeated, but I think the problem is when one group of files get's compiled due to my build output being

1>  student.cpp
1>  person.cpp
1>  main.cpp
1>  functions.cpp
1>  faculty.cpp
1>  Generating Code...
1>functions.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>functions.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>person.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char>     > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>person.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>student.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>student.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>C:\Users\Fluzzarn\Documents\Visual Studio 2012\Projects\pa1\Debug\pa1.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.24

all of the cpp files only include my "header.h", which itself includes all the other headers.

Header.h:

#ifndef HEADER_H
#define HEADER_H


#include "person.h"
#include "faculty.h"
#include "student.h"
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
using namespace std;

bool searchForUser();
void loadFromFile(std::string fileName, std::list<Person> targetList);
void loadBasicInfo(std::fstream& fileReader,Person tempPerson);

#endif

I've been working on trying to fix this problem for over an hour, and any insight would be appreciated

EDIT:

Overloaded <<

std::ostream& operator<<(std::ostream& os,const Address ad)
{
    os << ad.mStreetAddress << std::endl;
    os << ad.mCity << " , " << ad.mState << std::endl;
    os << ad.mZip;

    return os;

};

Address is a struct

Fluzzarn
  • 55
  • 1
  • 6
  • 3
    presumably, you have a function `ostream& operator<<(ostream& os, ...)` defined in your headerfile, rather than defining it in a .cpp file (alternatively, you could declare it as `inline`, but most print operations are complex enough that it makes more sense to not inline) – Mats Petersson Aug 28 '13 at 23:49
  • 1
    Please remove using namespace std; from all headers. – drescherjm Aug 28 '13 at 23:49
  • Thank you, I do have overloaded << for my structs in the header in which they are defined, but when I tried moving it to a general function cpp file , I get error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Name' (or there is no acceptable conversion) – Fluzzarn Aug 28 '13 at 23:56
  • Can you show an example of your overloaded << that you have in the header file? – drescherjm Aug 28 '13 at 23:58
  • 1
    So, move the `operator<<` to a the "address.cpp" file instead of the headerfile. It's not required to be in a headerfile anyway, and that will stop it being dragged into all the other translation units (translation unit = all headers and the source file as seen by the compiler) – Mats Petersson Aug 29 '13 at 00:04
  • @MatsPetersson I'm beginning to like plan-a (namely jamming `inline` in front of that impl), thereby avoid the do-you-know-what-a-prototype-is barrage unavoidably coming forthright. =( – WhozCraig Aug 29 '13 at 01:02
  • Yes, I'm sure OP will figure that part out in due time anyway - or I hope we never have to work together... ;) – Mats Petersson Aug 29 '13 at 01:03

1 Answers1

2

You put the function definitions in the .cpp-files, just like the comments say. To prevent the "no operator found"-error you have to keep the function declaration in the headerfile:

 std::ostream& operator<<(std::ostream& os,const Address ad);

Don't forget the semicolon at the end of the line. And note that the declaration only contains function header, and no body.

And you should pass ad as a reference, but that is only a minor detail, and has nothing to do with your problem.