c ++"错误:从'const char*'无效转换为'char*'"

MRT*_*T89 2 c++

我对C++编程很新,我一直在收到一个我似乎无法弄清楚的错误.我尝试了很多不同的方法,只是得到相同错误的变化.这是错误和我的代码:

 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 }
Run Code Online (Sandbox Code Playgroud)

我还没有编写实现文件的其余部分,因为我无法解决这个错误.

在account.cxx的第26行,我尝试了几种变体但没有成功.

Jon*_*rdy 6

你声明get_name() const,这意味着它this是一个const指针.通过扩展,name也是const,并且只能转换为const char*.您应该return name,而不是return &name- &name指向数组指针,而不是数组.数组名称可以隐式转换为元素指针; 指向数组的指针不是.

除此之外,您只需将声明更改为以下内容:

const char* get_name() const;
Run Code Online (Sandbox Code Playgroud)

或者可变的等价物:

char* get_name();
Run Code Online (Sandbox Code Playgroud)

但是,如果您正在使用C++,请使用C++功能:有一个std::string类是有原因的,从长远来看,使用C风格的字符串可以省去很多麻烦:

string get_name() const;
Run Code Online (Sandbox Code Playgroud)

毕竟你已经在使用stringhistory.此外,您可以使用标题中的类更改string* history为.这将隐藏添加和删除历史记录项所涉及的内存管理的详细信息.vector<string> historystd::vector<vector>