Thread: Stuck with conversions...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Stuck with conversions...

    I'm fairly new to programming, wondering how I could do something like this...

    class Cdata
    {
    public:
    char something;

    void functiion()
    {
    char something= "text";
    }
    };


    I get an error
    "cannot convert from 'char [length]' to 'char"

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    You have declared a member of your class to be a char, yet you are trying to assign an array of chars. The declaration of the type char need's to be an array. Then you could use strcpy(something,"hello"); to assign the variable.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    4
    Ah, thank you very much! I totally forgot about strcpy.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    class Cdata 
    { 
    public: 
        char something; 
    
        void functiion() 
        { 
            char something= "text"; 
        } 
    };
    I don't think the effects of this code will be what you're expecting. in function() you make a local variable, something, which you attempt to assign "text" to (as subdene said, use strcpy). The char something; variable which belongs to Cdata will not get this value, as it's not in scope. If you want to assign "text" to something then drop the char infront of it. For example:

    Code:
    class Cdata 
    { 
    public: 
        char something[4]; 
    
        void functiion() 
        { 
            strcpy(something, "text"); 
        } 
    };
    Also, you didn't allocate enough memory to store "text" in something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM