Populate an array with new objects

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"
using namespace 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++;  
} 
Last edited on
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.
Last edited on
here are the .h files for county and countylist. the part I'm having trouble with is in countylist.cpp

countylist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef COUNTYLIST_H
#define COUNTYLIST_H
#include <string>
#include "county.h"
using namespace 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 

county.h

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
#ifndef COUNTY_H
#define COUNTY_H
#include <string>
using namespace std;

class county
{
    private:   
       string state;
       string countyName;
       int stateId;
       int countyId;
    
    public:
       // set function prototypes    
       void setState(string);
       void setCountyName(string);
       void setStateId(int);
       void setCountyId(int);
       
       // get function prototypes
       string getState();
       string getCountyName();
       int getStateId();
       int getCountyId();
       
       // constructor prototypes
       county();
       county(string, string, int, int);              
};

#endif 


do I need to change county counties[3500]; to something like county* counties[3500];?
Last edited on
Oh, haha.

You have a variable with the same name as your class so the compiler is confused.

 
counties[i] = new county(state, county, stateId, countyId);



You will either need to change the 2 argument's name or change the class name to County.
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"

I really hate pointers they are so confusing.
Last edited on
It doesn't even look like you are using pointers.

Last edited on
findCounty is defined to return a county, but the return statement itself returns a pointer-to-county.

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()
Topic archived. No new replies allowed.