【问题标题】:c++ Operator functionsc++ 运算符函数
【发布时间】:2016-02-05 07:04:52
【问题描述】:

我正在尝试将 A 的名称设置为“新名称”并返回 A 的引用 但是我从 operator= 函数 binary '=' : no operator found which takes a right-hand operand of type 'const char [6]' (or there is no acceptable conversion) 得到一个错误

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= 中添加了一些代码

【问题讨论】:

  • 你遇到了什么错误?
  • @cad,我在上面列出了错误。
  • 你为什么不像以前那样再次使用strncpy
  • 你能把文字复制到 strncpy 中吗?如 strncpy(n._name , "新名称", 40); ?
  • 在您的编辑中,您切换了 strncpy 函数的前两个操作数。您是否查看了我在下面引用的 strncpy 的引用?

标签: c++ function operator-keyword


【解决方案1】:

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 的问题,我不参与这些。

【讨论】:

    【解决方案2】:

    您不能分配数组。在您的情况下,您可以在数组中执行 strncpy (就像您在构造函数中所做的那样)。但是,一般来说,使用 std::string 要好得多。

    【讨论】:

    • strncpy(n._name, _name); ?
    • 没有。您必须提供一个 n 作为第三个参数。此外,查找 std::strncpy 以便了解它所期望的参数。在这里看到:en.cppreference.com/w/cpp/string/byte/strncpy
    • std::strncpy(_name,n._name, 40);不会工作。说 _name const char 下的错误与 char 不兼容
    • 我需要做的就是将名称传递的引用或例如 A 设置为新名称,所以不应该 n._name = "new name";工作吗?
    • 不要使用strncpy,除非你真的必须处理固定长度的 0 填充(终止)缓冲区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2020-09-24
    • 2015-05-09
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多