Thread: question about arrays

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    question about arrays

    Code:
    class Cards
    {
    private: 
             char name_of_card [5];
           	 int value;
    public: 
    	Cards(int v, char c []);
    	~Cards();
    	char [] get_name();
    	int get_value();
    
    };
    My code doesn't seem to like the char [] get_name();

    it throws up a error C3409: empty attribute block is not allowed


    Then in my constructor for this class i have
    Code:
    Cards::Cards(int v, char c [])
    {
    	value = v;
    	name_of_card = c;
    }
    I'm used to using java but this isn't compiling giving this error
    error C2440: '=' : cannot convert from 'char []' to 'char [5]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays

    Is it obvious what i'm doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char [] get_name();
    You could make this:
    const char *get_name();

    > name_of_card = c;
    To copy char arrays, use strcpy():
    strcpy(name_of_card, c);

    Or do things the C++ way, and use strings instead of char arrays:
    Code:
    private: 
             string name_of_card;
    And then:
    Code:
    	name_of_card = c;
    works just fine.
    And get_name would become:
    Code:
    string get_name();
    Or I believe you could also use:
    Code:
    string &get_name();

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    Quote Originally Posted by swoopy
    > char [] get_name();
    You could make this:
    const char *get_name();

    how does char * differ ?

    Does it mean a pointer to the array i'm slightly confused

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    how does char * differ ?
    You can't return an array from a function, so you need to return a pointer to the first element or a pointer to the array as a whole. Since returning a pointer to the first element is easier, that's what everyone does.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM