Thread: cannot convert

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    5

    Angry cannot convert

    I'm trying to assign an array to an array however I'm having some problems,

    here is my class;


    Code:
    class Transaction {
       
       public:
          Transaction::Transaction();
          Transaction::Transaction(float InAmount, char InCode, char InNote[]);
          int Transaction::SetAmount(float NewAmount);
          float Transaction::GetAmount();
          int Transaction::SetCode(char NewCode);
          char Transaction::GetCode();
          int Transaction::SetNote(char *NewNote);
          int PrintNote();
       private:
          float Amount;
          char Code;
          char Note[20];
          
    };
    here is the overloaded constructor;

    Code:
     Transaction::Transaction(float InAmount, char InCode, char InNote[]) {
    
       Amount = InAmount;
       Code = InCode;
       Note = InNote;
       
    }
    when I try to compile I get a "cannot convert from 'char []' to 'char [20]'" error.

    If I declare note like this. "char *Note" it compiles fine but when I try to run the program it crashes....

    can someone help??

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Note = InNote;

    Use the strcpy() function to copy char arrays:
    strcpy(Note, InNote);

    To use strcpy, include <cstring>.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    when you recompile it with char * Note it crashes because although that is the same type as an array, but you are only initializing what would be the first element of the array and when you try to overwrite the other elements it goes into unassinged memory. Initialize note as char [] instead of char[20] and give it its assignments later.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Of course, std::string from the <string> header is really a better choice than a statically sized array. All of the operations that are intuitive for arrays but fail work with std::strings.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert char* time to const tm*
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 11-14-2006, 09:24 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM