Thread: Strange Error

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    14

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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Instead of using =, use strncpy().
    Code:
    strncpy(clperson1->name1, _name1, 25);
    strncpy(clperson1->name2, _name2, 25);
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM