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