0

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
2
  • @RakibulHasan updated with the errors, thanks for the reminder Commented May 28, 2014 at 3:04
  • 2
    Why are you using arrays? Why not use std::string? Commented May 28, 2014 at 3:06

2 Answers 2

3

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.).

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

1 Comment

Preferably change type to std::string instead though.
-1

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

3 Comments

why the trailing underscores? Also it would be better to pass const string& (for const char*).
@zoska Assuming you are referring to the constructor, did you miss the fact that I move the string? There is no extra copy going on here.
you still copy string for passing it as argument to constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.