3

I have a class with fields like firstname, age, school etc. I need to be able to store other information like for instance, where they have travelled, and what year it was in. I cannot declare another class specifically to hold travelDestination and what year, so I think a struct might be best. This is just an example:

struct travel {
    string travelDest;
    string year;
};

The issue is people are likely to have travelled different amounts. I was thinking of just having an array of travel structs to hold the data. But how do I create a fixed sized array to hold them, without knowing how big I need it to be?

Perhaps I am going about this the completely wrong way, so any suggestions as to a better way would be appreciated.


I realise there is essentially no difference between a class and struct, but for the purposes of assignment criteria I am not allowed a "class", so yeah.

4
  • 3
    "I cannot declare another class specifically to hold travelDestination and what year". You can, and in fact you have (in C++, the keyword struct defines a class. So does the keyword class). Don't be afraid of defining as many classes as you need in C++, there isn't a tax on them ;-) Commented Mar 19, 2010 at 0:36
  • There is in the sense that this is part of an assignment, and one of the criteria dictates exactly what classes we should have :( Else yes, I would just use a class. I will see if we can use other containers, but I have a feeling we can't. Commented Mar 19, 2010 at 0:41
  • 2
    See, I'd probably be thrown off that course for spending too much time arguing with the instructors. If they want to ban "complicated" classes, but allow "simple" ones like the one you're planning, then they should define their terms. For example is struct travel { string dest; int year; void print() {std::cout << year <<": "<< dest << "\n"; }; an "unapproved extra class", or not? Probably yes, but why does it matter to the assignment whether you do it or not? And so on. Commented Mar 19, 2010 at 0:47
  • Agreed. The criteria is very vague on specifics. It's a fun assignment, and perfect for me to learn C++ and OO with. Commented Mar 19, 2010 at 0:50

5 Answers 5

9

You could try associating a std::vector with each person, with each entry in the vector containing a struct:

typedef struct travel {
    string travelDest;
    string year;
} travelRecord;

std::vector<travelRecord> travelInfo;

You can then add items to the vector as you see fit:

travelRecord newRecord1 = {"Jamaica", "2010"};
travelInfo.push_back(newRecord1);

travelRecord newRecord2 = {"New York", "2011"};
travelInfo.push_back(newRecord2);

Some more information about vector operations can be found here.

Sign up to request clarification or add additional context in comments.

7 Comments

std::vector<travel> does the job. The separate space for struct names does exist in C++, but you don't need to use it.
typedef and class names live in the same namespace in C++. struct A { int dummy; }; typedef int A; is not possible in C++ like it is in C. If you write struct A { }; typedef struct A A; in C++ (which as a special case is allowed) it redefines that name: Was it a class-name before, it is then a typedef-name in addition. There is no class name that isn't also a typedef name anymore afterwards.
@Steve - I'm not sure that I understand your comment. What do you mean by "separate space for struct names"?
(so on a very pedantic C++ compiler, typedef struct A { } A; struct A a; will fail to compile, because the elaborated type specifier struct A will resolve the identifier to a typedef-name (see 7.1.5.3/1). However, none of comeau/EDG, gcc and clang do that - presumably because it would break a lot of code).
Ah, thanks litb. I should have said, "the ability to stick struct before a class name does exist in C++..." ;-).
|
1

Try learning about the C++ Standard Template Library (STL). For example, you could use a list.

Comments

1

You probably want to use one of the STL container classes to hold your struct (or better yet, refactor your struct into a class to hide the data members and make get/set accessors)

std::vector

std::list

...

Which one you pick depends entirely on what operations you anticipate doing on them. A vector is fine for basic storage, and random access. If you want to perform any sorting or searching functions, you probably want a list, or potentially even a map or multimap.

Here's a good starting point for more information on STL: http://www.parashift.com/c++-faq-lite/class-libraries.html

Comments

1

I would agree with the others, use an stl container like list. A list (or any other container) will grow as you push data onto it, like this:


#include <list>
#include <string>
using namespace std;

struct travel {
    string travelDest;
    string year;
};

int main() {
    list<travel> travels;
    travel t = {"Oslo", "2010"};
    travels.push_back(t);
}

Also note that while you are saying you cannot create a class, you are perfectly willing to create a struct. In C++, these are for all practical purposes the exact same thing, except that struct variables are public by default, while class variables are private by default.

Comments

0

To create an array of 100 elements you can do this:

travel* pTravelArray = new travel[100];

You can make that variable

int numberOfElements = 100;
travel* pTravelArray = new travel[numberOfElements];

Just don't forget to get rid of it when you're done:

delete [] pTravelArray;

As other have suggested though, the STL library has some much nicer (less easy to do something stupid with memory allocation) collections in it you can use.

3 Comments

Please use a vector instead. This code with new[]/delete[] should be taken as the reason to do so, not as a suggestion ;-)
The problem with this approach, is I won't know how many places the person has travelled, beforehand. And I don't really want to allocate a fixed number HOPING it's enough.
I see what you're getting at, but in fairness you did ask "how do I create a fixed sized array to hold them" :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.