Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize character arrays in class

Tags:

c++

g++

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?

like image 266
so61pi Avatar asked Apr 08 '14 16:04

so61pi


People also ask

How do you initialize a character array?

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)

How do you initialize an array of classes?

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.

How do you initialize a char array in Java?

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.

How do you initialize an array of characters in C++?

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.


3 Answers

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"; 
};
like image 128
Mr.C64 Avatar answered Nov 07 '22 07:11

Mr.C64


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.

like image 27
Some programmer dude Avatar answered Nov 07 '22 07:11

Some programmer dude


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.

like image 1
user3437460 Avatar answered Nov 07 '22 07:11

user3437460