cannot convert

This is a discussion on cannot convert within the C++ Programming forums, part of the General Programming Boards category; I'm trying to assign an array to an array however I'm having some problems, here is my class; Code: class ...

  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,796
    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, 02: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, 07: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, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21