English 中文(简体)
实际操作量=操作员超负荷工作(C++)
原标题:Struggling to get == operator overloading to work (C++)

奥凯确保我在这里做些什么,但并非正确。 试图超负荷工作=某类方法,而只是......不工作。 我至少从我的<代码>main和cout执行=校正产出时得到错误的回复。

这些是我的三个档案:

// TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

class TestClass {
public:
    TestClass(int contents);
    TestClass(const TestClass& orig);
    virtual ~TestClass();
    bool operator==(const TestClass& other);
private:
    int contents;
};

#endif  /* TESTCLASS_H */



// TestClass.cpp

#include <iostream>

#include "TestClass.h"

TestClass::TestClass(int contents) {
    this->contents = contents;
}

TestClass::TestClass(const TestClass& orig) {
    this->contents = orig.contents;
}

TestClass::~TestClass() {
}

bool TestClass::operator ==(const TestClass& other) {
    std::cout << "COMPARING" << std::endl;
    return (contents == other.contents);
}


// Main.cpp

#include <cstdlib>
#include <iostream>

#include "TestClass.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    TestClass* tc = new TestClass(1);
    TestClass* tc1 = new TestClass(1);

    cout << (tc == tc1) << endl;

    return 0;
}

因此,问题是——我做了什么错误? 我对某个地方可能非常令人sil笑皆是的错误表示歉意,但我可以发现这一点。

最佳回答

tc=tc1比较点值。 “should”应为*tc ==*tc1,但我不了解你为什么首先积极分配。

自动(快速)分配非常可取,只有在你需要目标独立于范围时,才有活力地分配。 (然后由自动指定的智能点人跟踪,然后酌情删除点名)。)


此外,运营商应当有<条码>const,因为它没有修改<条码>。

//                                      vvvvv
bool operator==(const TestClass& other) const;

即便更好,也是一种自由职能:

bool operator==(const TestClass& lhs, const TestClass& rhs);

这可能是一个朋友。 (自由功能总是首选,加上允许5=tc上班。)

问题回答

你正在比较点数。 而是:

cout << (*tc == *tc1) << endl;

两点:

  • You should free allocated memory with delete, or use a smart pointer
  • 请宣布经营者=const:

    bool营运人=(const TestClass& other) const





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签