Incomplete Type is not Allowed Error in Class Attributes

369 Views Asked by At

when I make a list of characters in the class it give me error "Incomplete Type is not Allowed"

class Car
{
private:
    char brand[];
    int model;
};

int main()
{
    Car car1;
}

when I make this declaration at the end of private attributes or use string to declare the brand it work without error

1

There are 1 best solutions below

0
Đức Vũ On

When declaring a static array in C++, you need to provide the compiler with information about the number of elements in the array. This allows the compiler to allocate the correct amount of memory for the array and determine the storage locations for the array elements. By specifying the size of the array, you are essentially telling the compiler how many elements the array can hold. This is important for memory management and accessing the elements of the array correctly.

In C++, both std::string and std::vector are dynamic array types that do not require specifying the length beforehand.

std::string: It is a class in the C++ Standard Library that allows you to work with character strings. One important feature of std::string is that it manages memory for the character string automatically. When you add, remove, or change the length of a std::string, it will automatically allocate or deallocate the necessary memory to store the string.

std::vector: It is a template class in the C++ Standard Library that represents a dynamic array. With std::vector, you can add or remove elements without specifying the size in advance. It automatically handles memory allocation and resizing as needed.

char a[]; // error
char b[3]; // acceptable
char c[] = {'x', 'y', 'z'}; // acceptable
char d[3] = {'x', 'y', 'z'}; // acceptable