简体   繁体   中英

assigning a const char array to a char array

So I have a class, Mail, with a class data member, char type[30]; and static const char FIRST_CLASS[]; outside of the class definition i initialize FIRST_CLASS[] to "First Class".

In my default Mail constructor I would like to set type to FIRST_CLASS[] but cannot seem to figure out a way to do so. Here's the code (a bit stripped down so not to bother you with stuff you dont need)

#include "stdafx.h"
#include <iostream>
#include <iomanip>   
#include <cstring>
#include <string>

using namespace std;
class Mail
{
public:
    Mail();
    Mail(const char* type, double perOunceCost, int weight);
    Mail(const Mail& other);

    ~Mail()
    { }

private:
    static const int TYPE_SIZE = 30;
    static const char FIRST_CLASS[];
    static const double FIXED_COST;
    static const int DEFAULT_WEIGHT = 1;

    char type[TYPE_SIZE];
    int weight;
    double perOunceCost;
};

const char Mail::FIRST_CLASS[] = "First Class";
const double Mail::FIXED_COST = 0.49;

// default
Mail::Mail()
{
    weight = DEFAULT_WEIGHT;
    perOunceCost = FIXED_COST;
    type = FIRST_CLASS;
}

and here's the errors:

1   error C2440: '=' : cannot convert from 'const char [12]' to 'char [30]'
2   IntelliSense: expression must be a modifiable lvalue

You can not assign one array to another. try strcpy

 strcpy(type,FIRST_CLASS);

Make sure destination is at least as large as source .

Note: If not mandatory, you should avoid array and use std::string for character array, and other STL containers ( vector , map .. etc.).

The error you are getting is not because you're trying to assign a const char array to a char array (yet - it will be an error). The error you're reporting is because you're trying to assign an array of size 12 ("First Class" is 12 characters long) to an array of size 30. You would get the same error even if both arrays were const, or non-const.

Since this is C++ and not C, like others have suggested, you should use std::string. Here is your example using std::string instead of char arrays.

#include <string>

using namespace std;
class Mail
{
public:
    Mail();
    Mail(string type_, double perOunceCost_, int weight_);

private:
    static const int TYPE_SIZE = 30;
    static const string FIRST_CLASS;
    static const double FIXED_COST;
    static const int DEFAULT_WEIGHT = 1;

    string type;
    int weight;
    double perOunceCost;
};

const string Mail::FIRST_CLASS("First Class");
const double Mail::FIXED_COST = 0.49;

// default
Mail::Mail()
{
    weight = DEFAULT_WEIGHT;
    perOunceCost = FIXED_COST;
    type = FIRST_CLASS;
}


Mail::Mail(string type_, double perOunceCost_, int weight_) :
    type(move(type_)),
    weight(weight_),
    perOunceCost(perOunceCost_)
{}

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