C++ 错误Visual Studio 和 Xcode 中的运算符重载

发布于 2024-09-08 13:50:36 字数 1491 浏览 5 评论 0原文

我正在为类做一个 C++ 作业,希望我重载“>>”操作员。我在 Visual Studio 2005 和 Xcode 3.2.2 中链接时遇到错误。 C++ 代码被分成几个文件。原型存储在overload.h中,实现存储在overload.cpp中,main()位于overloadtester.cpp中(尽管我无法放置与这个新的重载运算符相关的任何内容)在 main 中,因为错误,下面是我在开始收到错误之前添加的代码(如下所列)

// 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;
}

我在 Xcode 中遇到的错误是:

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

我目前无法使用 VS 访问计算机,希望当我我可以发布在该 IDE 上运行代码时遇到的错误吗?

已编辑以响应@Mark 的更多详细信息请求
编辑以响应 @sbi 的有关 input() 的更多信息的请求

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十级心震 2024-09-15 13:50:36

错误未定义的符号是链接器错误。这意味着当您将所有代码链接到单个可执行文件/库中时,链接器无法找到该函数的定义。造成这种情况的主要原因有两个:忘记将已编译的对象包含在库/可执行文件中或定义函数时出错(假设您声明 void f( int & ); 但实现了 void f( const int & ) {})。

我不知道你的IDE(我从来没有真正理解Xcode),但你可以尝试从命令行编译:g++ -o exe input1.cpp input2.cpp input3.cpp ...

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

韶华倾负 2024-09-15 13:50:36

您没有正确链接overload.cpp。确保在这两种情况下,overload.cpp 都是项目文件的一部分。

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

橘和柠 2024-09-15 13:50:36

如果重载是模板类,则运算符的源必须出现在标头中。

此外,名称必须完全限定。也就是说,如果您有任何命名空间/类静态成员,则在定义成员的实现时必须将完整的定义应用于它们。

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.

那小子欠揍 2024-09-15 13:50:36

您没有正确地将文件添加到您的项目中,因此编译器没有构建它,链接器也没有链接它。

我可以告诉您的原因是您发布了无法编译的无效代码。如果您将其添加到项目中,编译器将会失败,并且您将永远无法进行链接步骤。

在 Visual Studio 中,您可以在添加现有项...下的项目菜单中将现有文件添加到项目中

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文