Stuck with conversions...

This is a discussion on Stuck with conversions... within the C++ Programming forums, part of the General Programming Boards category; I'm fairly new to programming, wondering how I could do something like this... class Cdata { public: char something; void ...

  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
    359
    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, 11:40 AM
  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

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