-1

having this problem while running my main.cpp. there are a bunch of class .h and .cpp files and for some reason i just get this error.

In file included from /home/ubuntu/workspace/cs1440-hw2/Analyzer.h:8:0,
                 from /home/ubuntu/workspace/cs1440-hw2/Station.h:7,
                 from /home/ubuntu/workspace/cs1440-hw2/Day.h:6,
                 from /home/ubuntu/workspace/cs1440-hw2/main.cpp:7:
/home/ubuntu/workspace/cs1440-hw2/Region.h:12:3: error: ‘Station’ does not name a type
   Station*        _stations[MAX_STATION_COUNT];

this was not code written by me but im trying to reorganize it into seperate files but when i finished that and tried to run it i kept getting this error. not sure why. any help appreciated.

#ifndef Region_H
#define Region_H

#include "Stat.h"
#include "Day.h"
#include "Station.h"
#include "Analyzer.h"
#include "Region.h"

class Region {
    private:
        Station*        _stations[MAX_STATION_COUNT];
        int             _stationCount=0;
        int             _currentStation;

    public:
        Region();
        void load(std::ifstream &input);
        void resetStationIteration();
        Station* getNextStation();
        Station* findStation(std::string& id);

    private:
        Station* addStation(std::string& id, std::string& name);
};
#endif

there is another class called station already so this should work, not sure why it wont. please tell if i need to add more code or detail im fairly new to c++ so need a lot of help.

here is the station class .h file

#ifndef Station_H
#define Station_H

#include "Stat.h"
#include "Day.h"
#include "Station.h"
#include "Analyzer.h"
#include "Region.h"

class Station {
    private:
        std::string         _id;
        std::string         _name;
        Day*               _days[MAX_DAYS];
        int                 _dayCount = 0;
        int                 _currentDay = -1;

    public:
        Station(std::string& id, std::string& name);
        void load(std::string& datetime, std::string& qgag, std::string& qpcp);
        void resetDayIteration();
        Day* getDayNext();

        std::string getId();
        std::string getName();

    private:
        Day* findDay(std::string& date);
        Day* addDay(std::string& date);
};
#endif

ok thanks for all the answered questions so far i have used all your suggestions and fixed some of the errors, however, in trying to run my main function i get this error:

Running /home/ubuntu/workspace/cs1440-hw2/main.cpp
/tmp/ccxaejI8.o: In function `main':
main.cpp:(.text+0x1e0): undefined reference to `Region::Region()'
main.cpp:(.text+0x1f9): undefined reference to `Region::load(std::basic_ifstream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x264): undefined reference to `Region::findStation(std::string&)'
main.cpp:(.text+0x28e): undefined reference to `Analyzer::analyze(Station*)'
main.cpp:(.text+0x2ae): undefined reference to `Region::resetStationIteration()'
main.cpp:(.text+0x2c9): undefined reference to `Analyzer::analyze(Station*)'
main.cpp:(.text+0x2d8): undefined reference to `Region::getNextStation()'
collect2: error: ld returned 1 exit status


Process exited with code: 1

and here is the main file so you guys can get a better perpective:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include "Analyzer.h"
#include "Region.h"

#define MAX_DAYS          365
#define MAX_STATS         100
#define MAX_STATION_COUNT 20

bool split(const std::string& s, char delimiter, std::string elements[], int expectedNumberOfElements) {
    std::stringstream ss;
    ss.str(s);
    std::string item;

    int i=0;
    while (std::getline(ss, item, delimiter) && i<expectedNumberOfElements) {
        elements[i++] = item;
    }
    return (i==expectedNumberOfElements);
}

int main(int argc, char* argv[]) {
    if (argc>1) {
        std::ifstream inputStream(argv[1], std::ios_base::in);

        Region region;
        region.load(inputStream);

        Analyzer analyzer;

        if (argc>2) {
            std::string stationId(argv[2]);
            Station* station = region.findStation(stationId);
            if (station!= nullptr)
                analyzer.analyze(station);
        }
        else {
            region.resetStationIteration();
            Station *station;
            while ((station = region.getNextStation()) != nullptr)
                analyzer.analyze(station);
        }
    }

    return 0;
}
Isaak Johnson
  • 129
  • 3
  • 8
  • 4
    Recursive `#include`? – iBug Jan 26 '18 at 05:11
  • 1
    Circular references in the include statements. Station.h includes Region.h and Region.h includes Station.h setting off a chicken and egg war of who comes first Station.h has no need for Region.h, so just remove the include. – user4581301 Jan 26 '18 at 05:12
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – user4581301 Jan 26 '18 at 05:13

1 Answers1

-1

The problem is your circular #include:

The first part of your Station.h reads

#include "Region.h"

class Station {

And then Region.h is included there, which then includes Station.h again. At that nested #include, the macro condition #ifndef Station_H fails, so there's no declaration for Station available.

I recommend, that you drop the include of each other from both header files, and provide a forward declaration for the classes if it's really needed, as Daniel H pointed out in comments:

class Station; // Put in Region.h
class Region;  // Put in Station.h

This declares the existence of class Station and will hopefully eliminate the error you're facing.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    Incorrect. Header guards win place will prevent this. Sorry. No. Correct, just weirdly worded. – user4581301 Jan 26 '18 at 05:14
  • Solution can be simpler. No need for region at all in station. Pull the include. – user4581301 Jan 26 '18 at 05:15
  • @user4581301 English is not my primary language. Would you provide help by editing what you think is "weirdly worded"? Thanks in advance. – iBug Jan 26 '18 at 05:18
  • It looks like `Station` doesn’t actually use `Region`, at least in the header file, so I wouldn’t even forward-declare it. – Daniel H Jan 26 '18 at 05:23