在 C++ 中使用运算符重载添加相同的 class 对象 return 值

Adding same class object return values using operator overloading in C++

我目前正在为计数器程序添加内存功能。我的程序必须使用面向对象的设计方面,例如陪伴和继承,所以我创建了两个 类:计数器和用户界面。两个计数器对象(aCounter 和 mCounter)被传递给用户界面对象,以便有单独的计数器实例来跟踪常规计数和内存计数。我遇到的问题是尝试通过使用对象添加当前计数值和内存值来更新内存计数。我认为最简单的解决方案是运算符重载以添加两个对象 return 计数方法 return 值,但是由于用户界面对象传递给计数器对象,我似乎无法获得 operator+ 重载编译时不会出现错误:mCounter 的“表达式必须是可修改的左值”。

任何建议或帮助都会很棒。我也试图远离指针,只是为了练习不使用它们,否则我假设我可以直接从 setmemory 函数读取和写入内存值。

UserInterface.h

#pragma once
#include <iostream>
#include <string>
#include "Counter.h"
#include "Memory.h"
class UserInterface
{
private:
    Counter aCounter;
    Counter mCounter;
    int menuChoice;
public: 
    UserInterface(Counter &c, Counter &m);
    void displayChoices();
    void getUserInput();
    void executeSelection();
    void displayOutput();
    void setMemory();
};

UserInterface.cpp

#include "UserInterface.h"
#include "Counter.h"
#include <string>
using namespace std;


UserInterface::UserInterface(Counter &c, Counter &m)
{
    aCounter = c;
    mCounter = m;
    menuChoice = 0;
    x = 0;

}

void UserInterface::displayChoices()
{
    string choices[] = { "1. increment counter", "2. decrement counter", "3. reset counter", "4. add to memory", "5. reset memory", "6. quit" };
    
    for (int i = 0; i < 6; i++)
    {
        cout << "\n" << choices[i] << endl;
    }
}


void UserInterface::getUserInput()
{
    cin >> menuChoice;

}


void UserInterface::setMemory()
{
    //What I want to essentially do
     mCounter.returnCount() += aCounter.returnCount();


}


void UserInterface::displayOutput()
{
    cout << "Current Count: " << aCounter.returnCount() << endl;
    cout << "Current Count: " << mCounter.returnCount() << endl;
}


void UserInterface::executeSelection()
{
    while (menuChoice != 6)
    {
        displayChoices();
        getUserInput();
        switch (menuChoice)
        {
        case 1: aCounter.incrementCount();
                displayOutput();
                break;

        case 2: aCounter.decrementCount();
                displayOutput();
                break;
        case 3: aCounter.resetCount();
                displayOutput();
                break;
        case 4: setMemory();
                displayOutput();
                break;
        case 5: //do something to reset mem
                break;

        case 6: cout << "Program Ending" << endl;
                break;

        }
    }
}

Counter.h

#pragma once
class Counter
{
private: int count;
public: 
    Counter();
    void incrementCount();
    void decrementCount();
    void resetCount();
    int returnCount();
    
    Counter operator+(const Counter& c)
    {
        Counter counter;
        counter.count = this->count + c.count;
    }
    
};

Counter.cpp

#include "Counter.h"
#include "Memory.h"

Counter::Counter()
{
    count = 0;

}

void Counter::incrementCount()
{
    count++;
}

void Counter::decrementCount()
{
    count--;
}

void Counter::resetCount()
{
    count = 0;
}

int Counter::returnCount()
{
    return count;
}

Main.cpp

#include "Counter.h"
#include "UserInterface.h"
#include <iostream>
using namespace std;

int main()
{
    Counter c;
    Counter m;
    UserInterface anInterface(c, m);
    anInterface.executeSelection();
    return 0;
  
}

您正在尝试重载运算符 + 而您正在执行 += 您评论过的地方。

只需将运算符重载符号从 + 更改为 += 而不是写入:

void UserInterface::setMemory()
{
    //What I want to essentially do
     mCounter.returnCount() += aCounter.returnCount();
}

由于您编写的运算符重载要求 class 的实例,而不是函数 returnCount() 返回的整数,并且必须是可修改的 lvalue (大致可以用 = 符号分配的值)值。

做:

void UserInterface::setMemory()
{
     mCounter += aCounter; // overloading class instance rather than int
}