Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top