Trouble with char and classes

Hey folks,

I'n new hear so hello to everyone.

I've been self learning c++ for a month or so now and am having some problems with characters in classes.

Quite simply, I'd like to build a class, which will include text and numbers, and be able to set the text from main. Here is my attempt to set the character class c_chColor to the value "Red".

<pre>
// function example
#include <iostream>


class Change
{
public:
int m_nYear;
int m_nMonth;
char m_chColor;
};

int main()
{
Change cChange;

char myString="Red";

cChange.m_chColor=myString;


std::cout << cChange.m_chColor;

return 0;

}

</pre>

My error message is :invalid convesion from 'const char*' to 'char'

I've spent some time looking up this error on google, but nothing has really helped. Any assistance greatly appreciated! :)

PS - how to I embed the code in this forum?

Last edited on
It's one of those error messages that are hard to understand.

Basically you are trying to put a string into a char. A char can only be one character, what you want is a string. You have to #include <string> and change your char declaration to a string one.

May as well use namespace std for now but, if you don't want to your declaration will have to be std::string myString="Red";. Same in the class definition.

Cheers
Last edited on
Hi Odahk,

Thanks for the quick response!

I'll give your suggestion a try and come bug you some more if it doesn't work!

Thanks!
Topic archived. No new replies allowed.