When I compile the code below with g++ 4.8.2 I get an error.
#include <iostream>
using namespace std;
class test {
public:
void print() {
cout << str << endl;
}
private:
char str[] = "123456789"; // error: initializer-string for array of chars
// is too long
};
int main() {
char x[] = "987654321";
cout << x << endl;
test temp;
temp.print();
}
Why did I get that error, what is the difference between str
in class test
and x
in main
function?
You can initialize a one-dimensional character array by specifying: A brace-enclosed comma-separated list of constants, each of which can be contained in a character. A string constant (braces surrounding the constant are optional)
One way to initialize the array of objects is by using the constructors. When you create actual objects, you can assign initial values to each of the objects by passing values to the constructor. You can also have a separate member method in a class that will assign data to the objects.
Initializing Char Array A char array can be initialized by conferring to it a default size. char [] JavaCharArray = new char [ 4 ]; This assigns to it an instance with size 4.
char myword[] = { 'H' , 'e' , 'l' , 'l' , 'o' , '\0' }; The above declares an array of 6 elements of type char initialized with the characters that form the word "Hello" plus a null character '\0' at the end. But arrays of character elements have another way to be initialized: using string literals directly.
Inside your class, you have to explicitly specify the array size:
class test {
...
private:
// If you really want a raw C-style char array...
char str[10] = "123456789"; // 9 digits + NUL terminator
};
Or you can simply use a std::string
(which I think is in general much better in C++ code than using raw C-style strings):
#include <string>
...
class test {
...
private:
std::string str = "123456789";
};
You can't have arrays of unknown size in structures/classes, you need to set an array size.
Or better yet, use std::string
for strings. It's what it's made for.
I realised what you are asking and rewrite my entire reply. You are not supposed to do you initialization in your class. You only declare the variables/methods you want to use. Only after that, you use your class to create objects.
Set the object's variable's value from then on.
For example:
class Test
{
public:
int num; //You don't intialie value here
};
int main
{
Test testObj();
testObj.num = 100;
}
However, if you set the variables to private, you need to create a function in your class to access the class members. For exmaple setNum()
Alternatively, you can set the variables using the constructor, and insert it as parameter inputs during object creation.
class Test
{
public:
Test(int); //Constructor
private:
int num; //You don't intialie value here
};
Test::Test(int value)
{
this -> num = value;
}
int main
{
Test testObj(100); //set num to 100
}
If you want to access it using class member function, you can do this, but of course you have to define setNum()
in your class first.
testObj.setNum(100);
I know you are asking about char[], but I am giving you example on int. That is not the problem in your code. Either int or char[], you should avoid declaring it directly in your class. It seems like the main problem with your code is not whether you are using char[] or char*. You should not initialize your values in your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With