2

im trying to Set the name of A to "new name" and return the reference of A however im getting an error from the operator= function 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. 

If i simply do return n = "new name"; it returns a segmentation fault

Please pay attention to my operator= function in my Account.cpp file.

Here are my three files:

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

Here is my Account.cpp i removed functions that i thought were unnecessary. Edit: I'll include my entire account.cpp and account.h

Account.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); } } 

Here is the Declaration for the Operator= in Account.h

#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 

Any help/tips would be appreciated thanks.

Edit: Added some code to operator=

10
  • What error did you encounter? Commented Nov 4, 2015 at 17:03
  • @cad, I listed the error above. Commented Nov 4, 2015 at 17:06
  • 1
    Why don't you just use strncpy again, like you did before? Commented Nov 4, 2015 at 17:15
  • Can you copy literal text into strncpy? such as strncpy(n._name , "new name", 40); ? Commented Nov 4, 2015 at 17:19
  • In your edit, you have switched the first two operands of the strncpy function. Did you look up the reference to strncpy that I referenced below? Commented Nov 4, 2015 at 17:46

2 Answers 2

2

You operator= is written backwards!

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

It should be:

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

When you write A = B; then it is like A.operator=(B). So A will be *this (the object assigned to) and B will be n (the original one).

But you have a small issue with the actual use. Since there is no operator= that takes a string as argument, your code:

A = "new name"; 

is actually equivalent to:

A = Account("new name"); 

which is also equivalent to:

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

No harm done because operator= does not assign to the balance, only to the name. But you probably should not be doing this...

My advice, do not overload operator just for fun: use normal member functions.

PS: Issues with strncpy apart, I'm not entering in those.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't assign an array. In your case, you can do a strncpy (as you did in your constructor) into the array. But, in general, it's much better to use std::string.

12 Comments

strncpy(n._name, _name); ?
No. You would have to supply an n as the third argument. Also, look up std::strncpy so you know the arguments it expects. See that here: en.cppreference.com/w/cpp/string/byte/strncpy
std::strncpy(_name,n._name, 40); Wont work. Says error under _name const char is incompatible with char
All i need it to do is set the name passed reference or for example A to new name so shouldnt n._name = "new name"; work?
Don't use strncpy, unless you really have to deal in 0-padded (not terminated) buffers of fixed length.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.