C++ 运算符函数

标签 c++ function operator-keyword

我试图将 A 的名称设置为“新名称”并返回 A 的引用 但是,我从operator=函数中收到错误 binary '=' :没有找到采用“const char [6]”类型的右侧操作数的运算符(或者没有可接受的转换)

expression must be a modifiable value.

如果我简单地执行return n = "new name";,它会返回段错误

请注意我的Account.cpp文件中的operator=函数。

这是我的三个文件:

main.cpp:

#include <iostream>
#include "Account.h"
using namespace sict;
using namespace std;

    int main(){
      Account A;
      Account B("Saving", 10000.99);
      Account C("Checking", 100.99);
      double value = 0;
      cout << A << endl << B << endl << C << endl << "--------" << endl;
      A = B + C;
      A = "Joint";
      cout << A << endl << B << endl << C << endl << "--------" << endl;
      A = B += C;
      cout << A << endl << B << endl << C << endl << "--------" << endl;
      value += A;
      value += B;
      value += C;
      cout << "Total balance: " << value << endl;
      return 0;
    }

这是我的 Account.cpp,我删除了我认为不必要的功能。 编辑:我将包含我的整个 account.cpp 和 account.h

帐户.cpp:

  #include "cstring"
#include "iomanip"
#include "Account.h"
using namespace std;
namespace sict{
  Account::Account(){
    _name[0] = 0;
    _balance = 0;
  }
  Account::Account(double balance){
    _name[0] = 0;
    _balance = balance;
  }

  Account::Account(const char name[], double balance){
    strncpy(_name, name, 40);
    _name[40] = 0;
    _balance = balance;
  }
  void Account::display()const{
    cout << _name << ": $" << setprecision(2) << fixed << _balance;
  }
  Account& Account::operator+=(Account &s1)  {
     // return Account(_balance += s1._balance);
      _balance += s1._balance;
      return *this;

  }

  Account& Account::operator=( Account& n) const {

strncpy(n._name , n, 40);

      return n;
  }
  double operator+=(double& d, const Account& a){
      d += a;
      return d;
  }
  ostream& operator<<(ostream& os, const Account& A){
    A.display();
    return os;
}
  Account operator+(const Account &p1, const Account &p2){
    return Account(p1._balance + p2._balance);
  }



}

这是 Account.h 中 Operator= 的声明

#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__
#include <iostream>
namespace sict{
  class Account{
    char _name[41];
    double _balance;
  public:
    Account();
    Account(const char name[], double balance = 0.0);
    Account(double balance);
    void display()const;
     friend Account operator+(const Account &p1, const Account &p2);
     Account& operator+=(Account& s1)   ;
    Account& operator=( Account& n) const;


  };
  Account operator+(const Account &p1, const Account &p2);
  double operator+=(double& d, const Account& a);
  std::ostream& operator<<(std::ostream& os, const Account& C);
};

#endif

任何帮助/提示将不胜感激。

编辑:向operator=添加了一些代码

最佳答案

你的operator=写反了!

Account& Account::operator=( Account& n) const {
    strncpy(n._name , n, 40);
    return n;
}

应该是:

Account& Account::operator=(const Account& n){
    strncpy(_name, n._name, 40);
    return *this;
}

当您编写A = B;时,它就像A.operator=(B)。因此 A 将是 *this (分配给的对象),而 B 将是 n (原始对象) .

但是您在实际使用中遇到了一个小问题。由于没有 operator= 接受字符串作为参数,因此您的代码:

A = "new name";

实际上相当于:

A = Account("new name");

这也相当于:

A = Account("new name", 0.0);

不会造成任何损害,因为 operator= 不分配给余额,只分配给名称。但你可能不应该这样做......

我的建议是,不要仅仅为了好玩而重载运算符:使用普通的成员函数。

PS:除了 strncpy 的问题之外,我不会参与其中。

关于C++ 运算符函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33527742/

相关文章:

c - 赋值运算符的重新定义

c++ - 如何从方法返回动态指针数组

c++ - 如何使 SWIG 绑定(bind)类在 Lua 中使用运算符?

c++ - 你如何组合大型 C++ 项目的组件?

c++ - 如何在 Symbian C++ 中获取网络类型(2G/3G/Wi-Fi)?

c - 为什么函数不会改变变量?

c - 名为 connect() 的函数如何阻止 MPI C 程序运行?

javascript - 数组赋值中的引用错误(左手赋值)

c++ - 返回字符串结构安全吗?

c++ - vector 可以包含一个重载运算符 & 的智能指针吗?