简体   繁体   English

将const char数组分配给char数组

[英]assigning a const char array to a char array

So I have a class, Mail, with a class data member, char type[30]; 因此,我有一个Mail类,并带有一个数据类型为char type [30]的类; and static const char FIRST_CLASS[]; 和静态const char FIRST_CLASS []; outside of the class definition i initialize FIRST_CLASS[] to "First Class". 在类定义之外,我将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. 在我的默认Mail构造函数中,我想将类型设置为FIRST_CLASS [],但似乎无法找出一种方法。 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

 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.). 注意:如果不是强制性的,则应避免使用array并将std::string用于字符数组和其他STL容器( vectormap等)。

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). 您得到的错误不是因为您试图将const char数组分配给char数组(但是-这将是一个错误)。 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. 您报告的错误是因为您试图将大小为12的数组(“头等舱”的长度为12个字符)分配给大小为30的数组。即使两个数组都是const,您也会遇到相同的错误,或者非常量

Since this is C++ and not C, like others have suggested, you should use std::string. 由于这是C ++而不是C,因此就像其他人建议的那样,您应该使用std :: string。 Here is your example using std::string instead of char arrays. 这是使用std :: string而不是char数组的示例。

#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_)
{}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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