-
Strange Error
Hello. I've been relearning C++ for the past few days, and I've got this code written, and I'm getting an error, which I really don't understand.
Here's the code:
Code:
// file "main.cpp" of project "testapp1"
#include <iostream.h>
#include <string>
class clperson
{
public:
char name1[25];
char name2[25];
int age;
};
int initialize_clperson(char _name1[25], char _name2[25], int age, clperson *clperson1)
{
clperson1->name1 = _name1;
clperson1->name2 = _name2;
clperson1->age = (int)age;
return(0);
}
int main(int argc, char *argv[])
{
char name1[25];
char name2[25];
int age;
clperson main_person;
for (int i = 1; i < argc; ++i)
{
cout << "Argument " << i << " : " << argv[i] << '\n';
}
cout << endl << endl;
cout << "Welcome to my testing program! It will test functionality of\n";
cout << "different aspects of the C++ programming language.\n";
cout << endl << endl;
cout << "Please enter your full name...\n";
cout << '>';
cin >> name1, name2;
cout << endl << endl;
cout << "Please enter your age...\n";
cout << '>';
cin >> age;
initialize_clperson(name1, name2, age, &main_person);
return(0);
}
Here's the error:
Code:
C:\Program Files\Microsoft Visual Studio\MyProjects\testapp1\main.cpp(17) : error C2440: '=' : cannot convert from 'char []' to 'char [25]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
Does anybody know the solution to the problem? Thanks in advance.
-
You cannot make array assignments directly. Use something like 'strcpy to copy the contents from one array to the other. (That is to say, you cannot do full copies directly. You can assign one element at a time through a loop.)
Quzah.
-
Instead of using =, use strncpy().
Code:
strncpy(clperson1->name1, _name1, 25);
strncpy(clperson1->name2, _name2, 25);