I have been trying to make a program that uses derived classes but have run into a problem. I get the error type: " error C2440: '=' : cannot convert from 'const char [16]' to 'char' " when I try and intialize a char array of 255 to "Name Unknown" and such. Here is the complete program:
inherit.h

#include<iostream>
using namespace std;
class Person{
public:
	Person()
	{
		name[1]="Name Unknown";
		date_of_birth[1]="Date of Birth Unknown";
		gender[1]="Gender Unknown";
		SSN[1]="SSN Unknown";
		address[1]="Adress Unknown";
		phone_number[1]="Phone Number Unknown";
	}
	char name[255];
	char date_of_birth[255];
	char gender[255];
	//bool is_boy;//the gender requirement
	//bool is_girl;
	char SSN[255];
	char address[255];
	char phone_number[255];
};
	class Dependent: public Person{
	public:
		Dependent()
		{SSN_of_employee[1]="SSN of Employee Dependant on Unknown";}
		char SSN_of_employee[255];
	};
	class Employee: public Person{
	public:
		Employee()
		{
			hire_date[0]="Hire Date Unknown";
			salary=0.0;
			work_location[0]="Work Location Unkown";
			work_phone_number[0]="Work Phone Number Unknown";
		}
		char hire_date[255];
		double salary;
		char work_location[255];
		char work_phone_number[255];

	};
		class Manager: public Employee{
		public:
			Manager()
			{title[0]="Title Unknown";}
			char title[255];
		};
		class Worker: public Employee{
		public:
			Worker()
			{project[0]="Project Unknown";}
			char project[255];
		};

main.cpp

#include<iostream>
#include"inherit.h"
using namespace std;
int main()
{
	class Person person;
	class Employee emplyee;
	class Dependent dependent;
	class Worker worker;
	class Manager manager;

	system("pause");
	return 0;
}

Any suggestions for how to fix this error list:
error C2440: '=' : cannot convert from 'const char [13]' to 'char'
error C2440: '=' : cannot convert from 'const char [22]' to 'char'
error C2440: '=' : cannot convert from 'const char [15]' to 'char'
error C2440: '=' : cannot convert from 'const char [12]' to 'char'
error C2440: '=' : cannot convert from 'const char [15]' to 'char'
error C2440: '=' : cannot convert from 'const char [21]' to 'char'
error C2440: '=' : cannot convert from 'const char [37]' to 'char'
error C2440: '=' : cannot convert from 'const char [18]' to 'char'
error C2440: '=' : cannot convert from 'const char [21]' to 'char'
error C2440: '=' : cannot convert from 'const char [26]' to 'char'
error C2440: '=' : cannot convert from 'const char [14]' to 'char'
error C2440: '=' : cannot convert from 'const char [16]' to 'char'
Thanks.

Recommended Answers

All 4 Replies

Is there a specific reason why you are intermingling C++ and C-like data storage?

If you want to store characters like immutable strings, you should be using const char arrays as opposed to char arrays.

But that's besides the point.

What you're doing is assigning a const char array of a particular size to a character value in the arrays that you declared in class scope.

You can make your life easier with the <string> implementation as opposed to using globally scoped arrays.

Just keep in mind that arrays aren't pointers - arrays are of a particular size and you cannot make them point to a different address. Once you declare an array of a size (say 255), you can't make it point to another array, which is what it looks like you are doing.

You can negate the problem by changing your globally-scoped arrays into pointers (particularly const char pointers) and have them point to c-strings declared in your constructor.

I don't understand regular strings (as opposed to the STL defined strings), but what I've heard is that these strings are 'handled' within read-only memory so for the most part you can trust your implementation to handle these type of strings for you without worrying about allocating memory or freeing it.

I wouldn't place logic on faith. Use the STL and make your life easier and your code portable.

Example:


change

char name[255];

to

const char* name;

and change

name[1]="Name Unknown";

to

name = "Name Unknown";

but again, this would be placing logic on faith.

If you use the string implementation, your code would look like this instead--

//...

string name;

//...

name = "Name Unknown";

OMG thank you! IT WORKS :}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} When would you use string vs. char arrays?

OMG thank you! IT WORKS :}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} When would you use string vs. char arrays?

When I need to perform searches for characters to a particular string, or modify a string, or provide a self-contained method of measuring the string's 'components'.

There are many other reasons that this to use strings, but the ones I just mentioned are the general reasons why most would use strings.

When would you use string vs. char arrays?

Almost always, unless you are instructed to use character arrays. When you are taking a course in c++ language your instructor may require you to use character arrays for the educational value of learning how to use them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.