My guess is that the error is caused by this line:
Team::NewTeam(ncity, nname);
Your compiler is probably complaining that you are trying to use a member function without an instantiated object.
Aside from that, I believe you can design the whole thing better. For example, you could use a stl container (vector perhaps) to hold the teams. This helps you in many ways.
-> First, you don't have to write a member function to add objects to an external (to the class) array. All you'll have to do is write a constructor which you'll call as you push_back your objects to the vector.
-> Second, a vector is using dynamic memory allocation, so you don't have to worry that you'll waste memory if you have less than 31 teams or that you won't have enough space if you have more than 31 teams.
-> Third, there's a whole bunch of stl algorithms to help you with whatever operation you want to perform on your data (e.g. sorting by name or city, finding a team with a specific property etc...)
Finally, as Disch hinted too, try avoiding global vars. You can almost always do what you want with local variables passing them as arguments from one function to another.
Here's a small example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Person
{
string name;
int age;
Person(string name_,int age_):
name(name_),age(age_){}
};
bool SortByName(const Person & p1, const Person & p2)
{
return p1.name<p2.name;
}
bool SortByAge(const Person & p1, const Person & p2)
{
return p1.age<p2.age;
}
void init_db(vector<Person> & db)
{
db.push_back(Person("John",20));
db.push_back(Person("Ben",30));
db.push_back(Person("Mary",40));
db.push_back(Person("Kate",35));
}
void print_db(const vector<Person> & db)
{
for (int i=0; i<db.size(); i++)
{
cout << "name: " << db[i].name;
cout << " | age: " << db[i].age;
cout << endl;
}
}
int main()
{
vector<Person> database;
cout << "initializing database..." << endl;
init_db(database);
cout << "\nunsorted database..." << endl;
print_db(database);
cout << "\nsorted by name..." << endl;
sort(database.begin(),database.end(),ptr_fun(SortByName));
print_db(database);
cout << "\nsorted by age..." << endl;
sort(database.begin(),database.end(),ptr_fun(SortByAge));
print_db(database);
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
|
Useful links:
http://cplusplus.com/reference/stl/vector/
http://cplusplus.com/reference/algorithm/sort/