>\" operator. I am encountering errors linking in both Visual Studio 2005 and Xcode 3.2.2. The C++ code is separated into a" />
开发者

Error with C++ operator overloading, in Visual Studio AND Xcode

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()


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 ...


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


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜