class issues

This is a discussion on class issues within the C++ Programming forums, part of the General Programming Boards category; Anything about this seem wrong? I keep getting errors about the character array. class g_Vars { public: char strAppName[8]; // ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    class issues

    Anything about this seem wrong? I keep getting errors about the character array.

    class g_Vars
    {
    public:
    char strAppName[8];

    // Constructor
    void g_vars( void );
    };

    void g_Vars::g_vars( )
    {
    strAppName = "testing";
    }
    Last edited by YingPar; 06-04-2003 at 10:22 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Try strcpy(strAppName, "testing") from <cstring>.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It's the same reason why you can't do this:
    Code:
    int main()
    {
    	char text[5];
    	text="hell";
    	
        return 0;
    }
    I think the explanation goes like this: the declaration creates space in memory for the array and text points to that location. However, it is a constant pointer. When you say:

    text="hell";

    what happens is "hell' is assigned a location in memory, and then that address is assigned to text. But text is a const pointer, so you get an error. Array names are pointers, but they are const pointers and you cannot modify their addresses.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    Whatever the case, the problem is still continuing.

    When I declare a value for the array outright (ie. strAppName = "..."; ), I get the following error:
    error C2106: '=' : left operand must be l-value
    When using the cstring function (ie. strAppName = strcpy( Global.strAppName, "ADVENTURE! by Sean Gilleran" ); ), I get the following error:
    error C2440: '=' : cannot convert from 'char *' to 'char [28]'

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    I just realized my mistake (strcopy doesn't return anything), and fixed it, and that seems to get the program working again.

    But I'm still confused about that first error. It seems to me I've set the value of a char array before without a hassle.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "When I declare a value for the array outright (ie. strAppName = "..."; ),"

    You can't do that in a class. You can only declare a data member. Initialization takes place in the contructor. Since when have you ever done this:
    Code:
    class my_class
    {
    public:
        int number = 10;
    };
    ??

    You can declare a char array and intialize it at the same time:

    char text[] = "hello";

    but that isn't what is happening with an object of your class.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    [Deleted, sorry, my mistake]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. c++ class scope issues
    By avatarofhope2 in forum C++ Programming
    Replies: 13
    Last Post: 10-23-2007, 07:20 PM
  3. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2006, 11:00 PM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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