Creating Array of Structs.

Posted on 2005-04-09
Last Modified: 2013-11-20
I'm attempting to create an array of structs.  The struct name  "person" is defined in the .h, then in my constructor in my .cpp,  i have the following code:
      int index=10;
      person entry[index];

in actuality, the person entry[index]; will be in a while loop, as I dont know how many structs i need ahead of time.  When i go to compile, i get the following errors:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
'entry' : unknown size

Why isn't it reading my variable "index" size as 10?  Do i have to use DMA or something?..
Using Microsoft Visual C++ 6.0
Question by:aareedy
    LVL 55

    Accepted Solution

    by using c++ features you have the option to use the new operator, something likeç
    person *entry;
    entry = new person[index];

    to delete use:
    delete [] entry;

    Consider that if you instatiate directly as you proposed in your constructor, array will be destroyed at return.
    With my suggestion, you will need to put 'entry' variable as your app's member or as a global variable.

    If you want fully dynamic arrays, then you can use vector<> template, another c++ feature.
    LVL 30

    Expert Comment

    I recommend you use CArray or std::vector instead.
    These containers are much safer, because they'll automatically delete the contents in the container, and you can use the same syntax that you would use for a regular C-Style array.
    Example code:

    #include <afxtempl.h>

    class person
          CString Name;

    class foo
          foo(int size)
                m_ArrPerson[0].Name = "David";
                m_ArrPerson[1].Name = "Axer";
          CArray<person, person&> m_ArrPerson;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now