adding an #include stops my program from compiling

This is a discussion on adding an #include stops my program from compiling within the C++ Programming forums, part of the General Programming Boards category; hi, I'm working on some assignment for school. Thanks to some previous help on this board I already finished the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    adding an #include stops my program from compiling

    hi,

    I'm working on some assignment for school.
    Thanks to some previous help on this board I already finished the basics.
    Now I am writing the more complex stuff. I tried compiling what I wrote so far but I started getting a lot of errors from my compiler.
    when I tried commenting out all the new parts I saw that just by adding 1 #include statement I got those error messages.
    I really cant figure out what is happening.

    here is the crashing code:
    Code:
    #ifndef SCHEDULEITEM_H
    #define SCHEDULEITEM_H
    
    #include <iostream>
    #include <string>
    #include "schedule.h"
    
    typedef unsigned long int ulint;
    
    class ScheduleItem
    {   public:
            friend bool Schedule::check_item(ScheduleItem&, const ScheduleItem&, ScheduleItem&);
            ScheduleItem(ulint, ulint, std::string);
            ScheduleItem(std::istream&);
            ulint getbegin() const;
            ulint getend() const;
            std::string getdescription() const;
            ulint getlength() const;
        private:
            ulint begin_;
            ulint end_;
            std::string descr_;
    };
    
    std::ostream& operator<<(std::ostream&, const ScheduleItem&);
    
    bool operator<(const ScheduleItem&, const ScheduleItem&);
    bool operator>(const ScheduleItem&, const ScheduleItem&);
    bool operator==(const ScheduleItem&, const ScheduleItem&);
    
    #endif
    by commenting out the bold #include (and also the bold friend-function, because it needs stuff that gets included by the #include)
    everything works fine (well, fine meaning: program compiles)

    schedule.h also gets included from other files so I dont believe there is something wrong with that, but I'll show it below just to be sure.

    Code:
    // schedule.h
    
    #ifndef SCHEDULE_H
    #define SCHEDULE_H
    
    #include <iostream>
    #include <list>
    #include "scheduleitem.h"
    
    typedef unsigned long int ulint;
    
    class Schedule
    {   public:
            friend std::ostream& operator<<(std::ostream&, const Schedule&);
            Schedule();
            Schedule(std::istream&);
            bool insert_item(const ScheduleItem&);
            bool remove_item(const ScheduleItem&);
            bool check_item(ScheduleItem&, const ScheduleItem&, ScheduleItem&);
            bool check_item(ScheduleItem&, const ScheduleItem&, ScheduleItem&, const std::list<Schedule>&);
        private:
            std::list<ScheduleItem> items_;
    };
    
    #endif
    I'll also show you the Schedule::check_item code, but commenting this part out still gives the same error messages so I dont think it has anything to do with that (although there might be some mistakes in the code, but I havent been able to find that out yet because of these errors).

    Code:
    // part of schedule.cc
    
    bool Schedule::check_item(ScheduleItem& sibefore, const ScheduleItem& si, ScheduleItem& siafter)
    {   std::list<ScheduleItem>::iterator pos(findfreepos(items_, si));
        if (pos != 0)
            return 1;
        else
        {   sibefore.begin_ = si.getbegin();
            sibefore.end_ = si.getend();
            siafter.begin_ = si.getbegin();
            siafter.end_ = si.getend();
            for (std::list<ScheduleItem>::iterator i = items_.begin(); i != items_.end() || !(*i > sibefore); i++)
                if (!(*i < sibefore))
                {   sibefore.end_ = *i.getend() + sibefore.getlength();
                    sibefore.begin_ = *i.getbegin();
                }
            for (std::list<ScheduleItem>::iterator i = items_.end(); !(*i < siafter); --i)
            {   if (!(*i > siafter))
                {   siafter.begin_ = *i.getbegin() - siafter.getlength();
                    siafter.end_ = *i.getbegin()
                }
                if (*i == items_.begin())
                    break;
            }
            return 0;
        }
    }

    the errors I get are all like this:
    17 C:\Documents and Settings\ken\Desktop\struII\test\schedule.h expected `,' or `...' before '&' token
    17 C:\Documents and Settings\ken\Desktop\struII\test\schedule.h ISO C++ forbids declaration of `ScheduleItem' with no type

    etc...

    if anybody knows what is causing this his help would be much appreciated.

    EDIT:
    I will include some more code that might be useful.

    here is findfreepos, used in check_item
    Code:
    // findfreepos.cc
    
    #include <list>
    #include "scheduleitem.h"
    
    std::list<ScheduleItem>::iterator findfreepos(std::list<ScheduleItem>& lst, const ScheduleItem& x)
    {   for (std::list<ScheduleItem>::iterator i = lst.begin(); ; ++i)
        {   if (i == lst.end() || (*i > x))
                return i;
            else if (!(*i < x))
                return 0;
        }
    }
    and here is the mainfunction where I also include schedule.h
    Code:
    // main.cc
    
    #include <iostream>
    #include "scheduleitem.h"
    #include "schedule.h"
    
    int main()
    {   Schedule s;
        while(true)
        {   std::cout << "give begin, end and description:\n";
            ScheduleItem i(std::cin);
            if (i.getbegin() < 10)
                break;
            s.insert_item(i);
            std::cout << s;
        }
        while(true)
        {   std::cout << "give begin, end and description:\n";
            ScheduleItem i(std::cin);
            if (i.getbegin() < 10)
                break;
            s.remove_item(i);
            std::cout << s;
        }
        return 0;
    }
    I just noticed some other strange thing.
    if I change the order of includes in main.cc
    (just switching #include "scheduleitem.h" and #include "schedule.h")
    it gives totally different error messages.
    it just says:

    14 C:\Documents and Settings\ken\Desktop\struII\test\scheduleitem.h `Schedule' has not been declared


    thanks in advance,

    ken
    Last edited by angelscars; 11-11-2005 at 07:14 AM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you can't include like that, the compiler has no clue what to do and generally pops up random errors in your code. I also have this problem alot when trying to seperate classes up, like you're doing...basically you just can't. You can only include a file from another file, and they can't cross. What I mean is, if your going to include scheduleitem.h from schedule.h, then schedule.h can't be included from scheduleitem.h. Instead, make Schedule.h hold both class definitions.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    thanks for the reply. I'll toss both the class-declarations in one file then

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    I tried putting the class declarations in the same file, but it still gives the same error messages.

    I'll try some other things to avoid this problem, and let you know what happens.
    If anybody knows what is causing this, and how to solve it. please let me know.

    thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You need a forward declaration. This is necessary whether you use two header files or one (it isn't necessary to combine the two files). My guess is that you should forward declare Schedule in ScheduleItem.h instead of including the header, although it may work the other way around.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    thanks a lot Daved (again), the forward declaration fixed the problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stray Error while compiling
    By Mankthetank19 in forum C Programming
    Replies: 6
    Last Post: 02-03-2011, 02:24 PM
  2. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 05:23 AM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 11:44 AM
  4. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 03:05 PM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 12:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21