I'm having problems populating an array with objects. I'm trying to read in a file and create an array of counties. I'm reading in a line then trying to create a new county object by calling its parameterized constructor but it keeps telling me county is not a type. I have no idea what I'm doing wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <string>
#include <fstream>
#include <iostream>
#include "countylist.h"
#include "county.h"
usingnamespace std;
.
.
.
int i = 0;
char comma;
while(inFile >> state >> comma >> stateId >> comma >> countyId >> comma >> county && comma == ',')
{
counties[i] = new county(state, county, stateId, countyId);
i++;
}
counties[i] = new county(state, county, stateId, countyId);
Is counties[i] a pointer? Is county a class or struct that you have defined either in that file or another file you have included? I see that you included county.h but it it is obviously not finding it's declaration.
If you show all of the code we could fix it for you.
#ifndef COUNTYLIST_H
#define COUNTYLIST_H
#include <string>
#include "county.h"
usingnamespace std;
class countyList
{
private:
county counties[3500];
int countiesSize;
public:
// constructor prototypes
countyList();
// find county index by searching for Fips
county findCounty(int, int);
};
#endif
well, I feel like an idiot now. but now I'm getting a new problem. this is in a different part of countylist.cpp
it's saying getStateId is not declared which I assume means I'm not deferencing counties[i] properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// searches for and returns a the county instance that contains the info related to the state and fips code passed to it
county countyList::findCounty(int state, int fips)
{
for (int i = 0; i <= countiesSize; i++)
{
// checks if countyList[i] has the correct state and fips id's
if (&counties[i].getStateId == state);// && counties[i].getCountyId == fips)
return counties[i];
};
county* ptr;
ptr = new county();
return ptr;
}
and the return is giving me the error "conversion from `county*' to non-scalar type `county' requested"
ok. thanks guys. I figured out my problem. I changed everything to return a county pointer and I changed counties[i].getStateId to counties[i]->getStateId()