Collection of Records

Incomplete Type is Not Allowed

So I have a practice project I’m fooling around with, that I originally wrote in C#. The C# syntax I knew while writing it was limited at best, and I began to rely on the compiler, Visual Studio, to help me through all the really basic rough spots. Here I am now, after some extended length of time without touching C#, deciding I’ll dive right into C++. As a starting project I’ll be “porting” my code from C# into C++ compiled code. They are similar enough right? Well… yeah.

Syntax is a great thing in that it helps give us a structured set of rules to follow and fall back on whenever you aren’t sure of things. But making assumptions on a language’s syntax is trouble as well. Take for example the array in C++.

C++ contains arrays of course, but what it does not like, under any circumstance, is initializing an empty array. In my previous experience with Java, as an example, you could declare and initialize an array like so..

//simple java array example 
public class ArrayTest {
    public static void main(String[] args){
//declare array
int[] anArray;
        //initialize array
anArray = {10, 20, 30, 40, 50};
//add 1 and print out
for (int i=0; i < anA.length; i++) {

anArray[i] += 1;
System.out.println(anA[i]);
}
}
}

In C++, the same program would look very similar. Something like this:

//simple C++ array example
#include<iostream>
int main() {
//declare the array
int anArray[10];
    //initialize and print the array
for(int i=0;i < 10; i++) {
anArray[i] = i;
std::cout << anArray[i] << "\n";
}

Both of these examples first declare an array, then initialize it. But why did I have so much trouble? Quite simply, I was trying to declare C++ arrays like I am used to in Java. Tossing out something like

int[] anArray;

shouldn’t be a big deal, right? Wrong. I was missing one key fact, one key difference between Java arrays and C++ arrays.

Fan Art: Phoenix Wright, Ace Attorney

As Phoenix Wright so helpfully points out, the problem was lying in how Java and C++ first initialize their arrays when they are declared. Key difference: When an array is declared in Java, you are not allocating any memory to that array. What it is doing is creating a pointer to the memory taken when the array is then initialized. When an array is declared in C++, the memory is allocated at the time of declaration, so declaring an empty array is a big no-no because the compiler doesn’t know how much memory to reserve for the array.

This was my flaw, and what ate up about 30 minutes of my time as I struggled to figure out why I was getting the error “incomplete type is not allowed”. In hindsight, it’s a fairly simple error message and I just wasn’t thinking — or I was thinking too much. But some of my research led me to dig up vectors in C++, and how they work and differ from their counter parts, the array.


Vectors for the future

Vectors are, for all intents and purposes, pretty much the same as arrays. They are declared similarly, they are containers for data types (ints, doubles, strings, user created types, etc.), they have elements and indices. So what’s the difference?

Turns out vectors are a lot more agile than arrays in many respects. They are declared as follows:

vector<int> v(10);

Within the brackets the type of data to be stored is declared, and v(10) represents the name of the vector and the size on creation. Vectors are dynamic arrays, which means they can be declared with no size. So a valid declaration can be seen with no name or size of the vector declared. What’s more, vectors can be passed and returned to and from functions where standard arrays cannot. Standard arrays also cannot dynamically change size. To add/remove items from a standard array, you must create a new array, copy all the elements in, and add or remove whatever elements desired. Vectors come supplied with a simple method:

v.pushback(x); //adds element x to the end of the vector array.

Pushback is great, and there are other methods and ways that make it so you can easily insert/remove handpicked elements in the middle of the array. The drawbacks? All this work costs a bit more memory, so depending on the situation, a standard array may do the same work with less overhead.

As I’m just getting started with all this, this discovery was pretty great. Even though I feel like a complete newbie, it gave me some good insight into a bit more than what I knew before.