1

I am working on a C++ assignment for class that wants me to overload the ">>" operator. I am encountering errors linking in both Visual Studio 2005 and Xcode 3.2.2. The C++ code is separated into a few files. The prototypes are stored in overload.h, the implementations are stored in overload.cpp, and main() is in overloadtester.cpp (although I have not been able to put anything related to this new overloaded operator in main yet because of the error. below is the code I added before I started getting the errors (listed below)

// overload.h  
// Class Name OverloadClass
#ifndef _OverloadClass_H
#define _OverloadClass_H
#include < string >  
using namespace std ;
class OverloadClass
{
public:
    // Constructors and methods
private:
    x_;
    y_;
};
istream& operator>> (istream& in, OverloadClass &p);
#endif



// overload.cpp
#include < iostream >
#include < string >
#include < sstream >
#include < istream >  
extern "C"
{
#include "DUPoint.h"
}  
using namespace std ;  

void OverloadClass::input()
{
    char leftParen;
    char comma ;
    cin >> leftParen >> x_ >> comma;

    char rightParen;
    cin >> y_ >> rightParen;
}



// Constructor and method implementations  
istream& operator>> (istream& in, OverloadClass &p)
{ 
    p.input();
    return in;
}

The error I have been getting in Xcode is:

Link ./OverloadDirectory   
Undefined symbols: "operator>>(std:basic_istream >&, OverloadClass&)" referenced from: _main in overloadtester.o 
ld: symbol(s) not found collect2: ld returned 1 exit status

I dont have access to the computer with VS at this very moment hopefully when I do I can post the error I am getting from running the code on that IDE.

Edited in response to @Mark's request for more details
Edited in response to @sbi's request for more info on input()

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Jordan
  • 4,928
  • 4
  • 26
  • 39

4 Answers4

1

You're not properly linking in overload.cpp. Make sure that overload.cpp is party of your project file in both cases.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • could you be more specific about how to do this, I added each of the files to the Xcode project before I tried building the files and the files show up in the project window of Xcode, so I am not sure what you mean. – Jordan Jul 06 '10 at 22:48
  • @Jordan: This was an attempt at psychic debugging, apparently my psychic powers were off today. Can you please post the entirety of the code for overload.cpp and overload.h? – Adam Rosenfield Jul 06 '10 at 22:50
  • @Jordan: You can check if `overload.cpp` is included in the build by going to *"Targets"* -> your target. There check if its included in the *"Compile Sources"* folder. – Georg Fritzsche Jul 06 '10 at 23:08
  • @Georg all the .cpp files are there – Jordan Jul 06 '10 at 23:20
1

The error undefined symbol is a linker error. It means that when you are linking all your code into a single executable / library, the linker is not able to find the definition of the function. The main two reasons for that are forgetting to include the compiled object in the library/executable or an error while defining the function (say that you declare void f( int & ); but you implement void f( const int & ) {}).

I don't know your IDEs (I have never really understood Xcode) but you can try to compile from the command line: g++ -o exe input1.cpp input2.cpp input3.cpp ...

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • I gave it a try `g++ -o exe overload.cpp overloadtester.cpp` but not luck a very similar error was coming up relating to symbols not found – Jordan Jul 06 '10 at 23:17
  • In the end I was missing a `const` reference from the code, thanks for your help. – Jordan Jul 07 '10 at 12:13
  • 1
    @Jordan: Which is why you always should copy&paste the code - the code in your question didn't have that problem. – Georg Fritzsche Jul 09 '10 at 02:18
0

If overload is a template class, the source for the operator must occur in the header.

In addition, the names must be fully qualified. That is, if you have any namespace/class static members, they must have the full definition applied to them when defining the member's implementation.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I am not sure what all that meant I am relatively new to the programming world. Maybe you could elaborate a little. – Jordan Jul 06 '10 at 23:23
0

You have not added the files to your project correctly so the compiler is not building it and the linker is not linking it in.

The reason I can tell is that you have posted invalid code that will not compile. Had you added it to your project, the compiler would have failed and you would never have gotten to the linking step.

In Visual Studio, you can add an existing file to your project in the Project menu under Add Existing Item...

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187