• Javascript
  • Python
  • Go

Error: C2228 - '.size' must have class/struct/union

Error: C2228 - '.size' must have class/struct/union When working with C++ programming language, it is not uncommon to come across a variety ...

Error: C2228 - '.size' must have class/struct/union

When working with C++ programming language, it is not uncommon to come across a variety of errors. One such error is C2228, which can be quite confusing for beginners. In this article, we will take a closer look at this error and understand its cause and possible solutions.

Firstly, let's understand what this error means. C2228 is a compiler error that occurs when the '.size' operator is used without a class, struct, or union. This means that the program is trying to access the size of a data type that is not a class, struct, or union. Let's dive deeper into the reasons behind this error.

The '.size' operator is used to determine the size of a data type in bytes. It is often used in conjunction with the 'sizeof' operator to allocate memory dynamically. However, the '.size' operator can only be used with class, struct, or union data types. If it is used with any other data type, the compiler will throw the C2228 error.

To better understand this error, let's look at an example:

```

int num = 10;

int size = num.size;

```

In the above code, we are trying to use the '.size' operator with an integer data type. Since int is not a class, struct, or union, the compiler will throw the C2228 error. To fix this, we can use the 'sizeof' operator instead:

```

int num = 10;

int size = sizeof(num);

```

This code will compile without any errors as we are now using the correct operator for determining the size of an integer.

Another common scenario where this error can occur is when using the '.size' operator with arrays. Let's see an example:

```

int arr[] = {1, 2, 3, 4, 5};

int size = arr.size;

```

In this code, we are trying to get the size of the array using the '.size' operator. However, since arrays are not class, struct, or union data types, the compiler will throw the C2228 error. To get the size of an array, we can use the 'sizeof' operator:

```

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr)/sizeof(arr[0]);

```

By dividing the total size of the array by the size of its first element, we can get the number of elements in the array.

In some cases, this error can also occur due to a missing header file. For example, if we are using the '.size' operator with a string data type, we need to include the <string> header file. If this file is not included, the compiler will throw the C2228 error.

In conclusion, the C2228 error occurs when the '.size' operator is used with a data type that is not a class, struct, or union. This can be fixed by using the 'sizeof' operator or including the necessary header file. Understanding the root cause of this error can help in writing more efficient and error-free code. Happy coding!

Related Articles

Profiling in Visual Studio 2008

Profiling is an essential tool for developers, allowing them to identify and optimize the performance of their code. In Visual Studio 2008, ...

n a File in C++: Step-by-Step Guide

When it comes to programming, there are many different languages and tools to choose from. However, one language that has stood the test of ...