简体   繁体   中英

c++ “error: invalid conversion from ‘const char*’ to ‘char*’”

I'm fairly new to C++ programming and I've been getting an error that I can't seem to figure out. I've tried it many different ways and just get variations of the same error. here is the error and my code:

 account.cxx: In member function ‘char* account::get_name() const’:                                                                                                                                                                     
 account.cxx:26: error: invalid conversion from ‘const char*’ to ‘char*’ 

          //File: account.h
 68     class account
 69     {
 70     public:
 71     typedef char* string;
 72     static const size_t MAX_NAME_SIZE = 15;
 73         // CONSTRUCTOR
 74     account (char* i_name, size_t i_acnum, size_t i_hsize);
 75     account (const account& ac);
 76     // DESTRUCTOR
 77     ~account ( );
 78         // MODIFICATION MEMBER FUNCTIONS
 79     void set_name(char* new_name);
 80     void set_account_number(size_t new_acnum);
 81     void set_balance(double new_balance);
 82     void add_history(char* new_history);
 83     // CONSTANT MEMBER FUNCTIONS
 84     char* get_name ( ) const;
 85         size_t get_account_number ( ) const;
 86         double get_balance( ) const;
 87     size_t get_max_history_size( ) const;
 88         size_t get_current_history_size ( ) const;
 89         string* get_history( ) const;
 90         friend ostream& operator <<(ostream& outs, const account& target);
 91     private:
 92     char name[MAX_NAME_SIZE+1]; //name of the account holder
 93     size_t ac_number; //account number
 94     double balance; //current account balance
 95     string *history; //Array to store history of transactions
 96     size_t history_size; //Maximum size of transaction history
 97     size_t history_count; //Current size of transaction history
 98     };


 1 // File: account.cxx
 2 // Author: Mike Travis
 3 // Last Modified: Feb, 26, 2012
 4 // Description: implementation of Account class as prescribed by the file account.h
 5 
 6 #include <stdio.h>
 7 #include <iostream>
 8 #include "account.h"
 9 
 10 using namespace std;
 11 //Constructor
 12 
 13 account::account(char* i_name, size_t i_acnum, size_t i_hsize){
 14 string *d_history = NULL;
 15 d_history = new string[i_hsize];
 16 
 17 for(int i = 0; i<MAX_NAME_SIZE +1; i++){
 18     name[i] = i_name[i];
 19 }
 20 ac_number = i_acnum;
 21 history_size = i_hsize;
 22 history_count = 0;
 23 }
 24 
 25 char* account::get_name() const {
 26 return &name;
 27 }

I haven't yet written the rest of the implementation file since I can't get around this error.

In line 26 of account.cxx I have tried several variations with no success.

You declare get_name() const , which means that this is a const pointer. By extension, name is also const , and can only be converted to const char* . You should return name , not return &name&name is a pointer to an array , not an array. Array names are implicitly convertible to element pointers; pointers to arrays are not.

Apart from that, you need only change the declaration to the following:

const char* get_name() const;

Or the mutable equivalent:

char* get_name();

However, if you're using C++, use C++ features: there is a std::string class for a reason, and it will save you a lot of trouble in the long run over using C-style strings:

string get_name() const;

You're already using string for history , after all. Also, you might change string* history to vector<string> history , using the std::vector class from the <vector> header. This will hide the details of memory management involved in adding and removing history items.

char* account::get_name() const 

should be

const char* account::get_name() const

If that error wasn't stopping you, people could start editing your object's name!

account::get_name() is a const member function, which means that its access to any data member is also const . That means it sees name as a char const[MAX_NAME_SIZE+1] .

Also:

  • &name is a char const(*)[MAX_NAME_SIZE+1]
  • name — when it decays — will turn into a char const* , and that's what you want.

So:

const char* account::get_name() const
{
   return name;
}
char name[MAX_NAME_SIZE+1];

... is const char * ()[MAX_NAME_SIZE+1] not char * . Try using std::string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM