Menu

Using a user-defined type with list class

gallo
2008-03-17
2012-09-26
  • gallo

    gallo - 2008-03-17

    Dev C++ version 4.9.9.2
    Windows xp service pack 2

    I am doing an assignment for an Operating Systems class, and we are currently learning about kernels. I have to write a program that has a Process Control Bus structure, a Resource Control Bus structure, and some functions that will add and remove these structures from a queue. Later, we will have to compile it as a library.

    I have a libque.h which lists all the function prototypes, and data members.
    I have a libque.cpp which does not have a main() (since it will be a library later)
    My Process Control Bus structure (PCB) only has only has two data members for now (int PID, and int priority)
    I am using the list class to represent my queues.

    Now, I have main.cpp to test my code, and I attempted to create a list by doing this:
    list<PCB> SL; //< supposed to be a "system queue" for the assignment

    however, I get this error:

    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\von\Desktop\Makefile.win"
    Executing make...
    make.exe -f "C:\Documents and Settings\von\Desktop\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_list.h: In instantiation of std::_List_node&lt;pcb_t&gt;': C:/Dev-Cpp/include/c++/3.4.2/bits/list.tcc:72: instantiated fromvoid std::_List_base<_Tp, _Alloc>::_M_clear() [with _Tp = pcb_t, _Alloc = std::allocator<pcb_t>]'
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_list.h:328: instantiated from std::_List_base&lt;_Tp, _Alloc&gt;::~_List_base() [with _Tp = pcb_t, _Alloc = std::allocator&lt;pcb_t&gt;]' main.cpp:10: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_list.h:100: error:std::_List_node<_Tp>::_M_data' has incomplete type
    libque.h:7: error: forward declaration of `struct pcb_t'

    C:/Dev-Cpp/include/c++/3.4.2/bits/list.tcc: In member function void std::_List_base&lt;_Tp, _Alloc&gt;::_M_clear() [with _Tp = pcb_t, _Alloc = std::allocator&lt;pcb_t&gt;]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_list.h:328: instantiated fromstd::_List_base<_Tp, _Alloc>::~_List_base() [with _Tp = pcb_t, _Alloc = std::allocator<pcb_t>]'
    main.cpp:10: instantiated from here
    C:/Dev-Cpp/include/c++/3.4.2/bits/list.tcc:77: error: 'struct std::_List_node<pcb_t>' has no member named '_M_data'

    make.exe: *** [main.o] Error 1

    Execution terminated

    I assumed that it was a problem with my PCB struct, because of the "_M_data' has incomplete type" in the error, and so I googled the error to see if it would lead me anywhere. Unfortunately, some of the stuff that I came across was way over my head. Can someone point me in the right direction?

     
    • gallo

      gallo - 2008-03-18

      I only used lists because I was unaware of the STL <queue> class.
      For this assignment we need FIFO queues, and also Priority queues, so I re-wrote my code using the STL <deque> class, since we have to insert some emements according to priority.
      Also, I moved my project into c:\kernel1.

      This is my main.cpp:

      include <cstdlib>

      include <iostream>

      using namespace std;

      include "libque.h"

      int main(int argc, char *argv[])
      {
      // Creating the two process queues, and one resource queue
      deque<PCB> SL; //System Queue (priority based)
      deque<PCB> UL; //User Queue (FIFO based)
      deque<PCB> WL; //Resource Quwuw (FIFO based)

      //a. Add some process to the WL queue and display
      //b. Delete a process from the WL queue and display
      //c. Delete another process form the WL queue and display
      //d. Insert some processes to the SL queue (assume priority is inversely 
           //proportional to PID, i.e. lower PID = higher priority
      //e. Remove a process from the SL queue and display
      //f. Insert some processes to the UL queue and display
      //g. Remove a process form the UL queue and display
      //h. Check queues SL, UL, WL and display
      
      system(&quot;PAUSE&quot;);
      return EXIT_SUCCESS;
      

      }

      This is my libque.h:

      ifndef libque_H

      define libque_H

      include <deque>

      using namespace std;

      struct PCB; //Process Control Bus data structure
      struct RCB; //Resource Control Bus data structure
      typedef PCB pcb_t;
      typedef deque<pcb_t>
      queue_t;

      void enqueue(queue_t &que, pcb_t &pcb); //Enqueues the PCB to the given queue
      void dequeue(queue_t &que, pcb_t &pcb); //Dequeues the PCB from the given queue
      void insert(queue_t &que, pcb_t &pcb); //Inserts a PCB to the given queue
      void remove(queue_t &que, pcb_t &pcb);//Deletes a PCB from the given queue
      bool empty(queue_t &que); //Checks whether a queue is empty or not
      void display(queue_t &que); //Displays PCB's of all processes in a queue

      endif //libque_H

      Also, I am including my libque.cpp just up to the part that has the PCB struct

      include <cstdlib>

      include <iostream>

      using namespace std;

      include "libque.h"

      struct PCB
      {
      public:
      int PID;
      //queue_t Resources; (not used yet)
      //Status status; (not used yet)
      //Creation_Tree CrTree; (not used yet)
      static int Priority;

      };

      And unfortunately, this is the compiler log.

      Compiler: Default compiler
      Building Makefile: "C:\kernel1\Makefile.win"
      Executing make...
      make.exe -f "C:\kernel1\Makefile.win" all
      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In member function void std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_initialize_map(size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated fromstd::_Deque_base<_Tp, _Alloc>::_Deque_base(const _Alloc&, size_t) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated from std::deque&lt;_Tp, _Alloc&gt;::deque(const typename std::_Deque_base&lt;_Tp, _Alloc&gt;::allocator_type&amp;) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' main.cpp:10: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:444: error: invalid application ofsizeof' to incomplete type `PCB'

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated from std::_Deque_base&lt;_Tp, _Alloc&gt;::_Deque_base(const _Alloc&amp;, size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated fromstd::deque<_Tp, _Alloc>::deque(const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      main.cpp:10: instantiated from here
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:471: error: invalid application of sizeof' to incomplete typePCB'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:471: error: invalid use of undefined type struct PCB' libque.h:7: error: forward declaration ofstruct PCB'

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In member function `void std::_Deque_iterator<_Tp, _Ref, _Ptr>::_M_set_node(_Tp**) [with _Tp = PCB, _Ref = PCB&, _Ptr = PCB*]':

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:468: instantiated from void std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_initialize_map(size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated fromstd::_Deque_base<_Tp, _Alloc>::_Deque_base(const _Alloc&, size_t) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated from `std::deque<_Tp, _Alloc>::deque(const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      main.cpp:10: instantiated from here

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:232: error: invalid use of undefined type struct PCB' libque.h:7: error: forward declaration ofstruct PCB'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In member function `void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_node(_Tp*) [with _Tp = PCB, _Alloc = std::allocator<PCB>]':

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:497: instantiated from void std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_destroy_nodes(_Tp**, _Tp**) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:425: instantiated fromstd::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated from std::deque&lt;_Tp, _Alloc&gt;::deque(const typename std::_Deque_base&lt;_Tp, _Alloc&gt;::allocator_type&amp;) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' main.cpp:10: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:401: error: invalid application ofsizeof' to incomplete type PCB' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In member functionstd::_Deque_iterator<_Tp, _Ref, _Ptr>& std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator++() [with _Tp = PCB, _Ref = PCB&, _Ptr = PCB*]':
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h:120: instantiated from void std::__destroy_aux(_ForwardIterator, _ForwardIterator, __false_type) [with _ForwardIterator = std::_Deque_iterator&lt;PCB, PCB&amp;, PCB*&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h:152: instantiated fromvoid std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::_Deque_iterator<PCB, PCB&, PCB*>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:703: instantiated from std::deque&lt;_Tp, _Alloc&gt;::~deque() [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' main.cpp:10: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:143: error: cannot increment a pointer to incomplete typePCB'

      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h: In function void std::_Destroy(_Tp*) [with _Tp = PCB]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h:120: instantiated fromvoid std::destroy_aux(_ForwardIterator, _ForwardIterator, false_type) [with _ForwardIterator = std::_Deque_iterator<PCB, PCB&, PCB*>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h:152: instantiated from void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::_Deque_iterator&lt;PCB, PCB&amp;, PCB*&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:703: instantiated fromstd::deque<_Tp, _Alloc>::~deque() [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      main.cpp:10: instantiated from here
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_construct.h:107: error: invalid use of undefined type struct PCB' libque.h:7: error: forward declaration ofstruct PCB'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In member function _Tp* std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_allocate_node() [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:483: instantiated fromvoid std::_Deque_base<_Tp, _Alloc>::_M_create_nodes(_Tp, _Tp) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:459: instantiated from void std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_initialize_map(size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated fromstd::_Deque_base<_Tp, _Alloc>::_Deque_base(const _Alloc&, size_t) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated from std::deque&lt;_Tp, _Alloc&gt;::deque(const typename std::_Deque_base&lt;_Tp, _Alloc&gt;::allocator_type&amp;) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' main.cpp:10: instantiated from here C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:397: error: invalid application ofsizeof' to incomplete type PCB' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h: In static member functionstatic size_t std::_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size() [with _Tp = PCB, _Ref = PCB&, _Ptr = PCB*]':
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:232: instantiated from void std::_Deque_iterator&lt;_Tp, _Ref, _Ptr&gt;::_M_set_node(_Tp**) [with _Tp = PCB, _Ref = PCB&amp;, _Ptr = PCB*]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:468: instantiated fromvoid std::_Deque_base<_Tp, _Alloc>::_M_initialize_map(size_t) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated from std::_Deque_base&lt;_Tp, _Alloc&gt;::_Deque_base(const _Alloc&amp;, size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated fromstd::deque<_Tp, _Alloc>::deque(const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      main.cpp:10: instantiated from here
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:106: error: invalid application of sizeof' to incomplete typePCB'
      C:/Dev-Cpp/include/c++/3.4.2/ext/new_allocator.h: In member function _Tp* __gnu_cxx::new_allocator&lt;_Tp&gt;::allocate(size_t, const void*) [with _Tp = PCB]': C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:397: instantiated from_Tp* std::_Deque_base<_Tp, _Alloc>::_M_allocate_node() [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:483: instantiated from void std::_Deque_base&lt;_Tp, _Alloc&gt;::_M_create_nodes(_Tp**, _Tp**) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:459: instantiated fromvoid std::_Deque_base<_Tp, _Alloc>::_M_initialize_map(size_t) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:367: instantiated from std::_Deque_base&lt;_Tp, _Alloc&gt;::_Deque_base(const _Alloc&amp;, size_t) [with _Tp = PCB, _Alloc = std::allocator&lt;PCB&gt;]' C:/Dev-Cpp/include/c++/3.4.2/bits/stl_deque.h:636: instantiated fromstd::deque<_Tp, _Alloc>::deque(const typename std::_Deque_base<_Tp, _Alloc>::allocator_type&) [with _Tp = PCB, _Alloc = std::allocator<PCB>]'
      main.cpp:10: instantiated from here
      C:/Dev-Cpp/include/c++/3.4.2/ext/new_allocator.h:81: error: invalid application of sizeof' to incomplete typePCB'

      make.exe: *** [main.o] Error 1

      Execution terminated

      Thanks for your time by the way.

       
    • gallo

      gallo - 2008-03-20

      fixed, the previous compile log makes a lot more sense now, thanks again.

       
    • cpns

      cpns - 2008-03-18

      With respect to your error, you have only provided half the context, sometimes that is enough to make an educated 'guess', but in this case teh code would really help.

      I suggest that you post the code in main.cpp at least up-to line 10 where the list is instantiated, and also libque.h at least up to line 7; the error reported for that line is likely related.

      The rest of the Compile Log is just 'noise' caused by errors in your code at or before the lines I have mentioned. STL code errors can create a lot of such noise. The trick is to see the wood for the trees.

      A couple of asides:

      >> I am using the list class to represent my queues.
      I wonder why you would not use the an STL <queue> class?

      >> C:\Documents and Settings\von\Desktop\ Don't put your projects there. Apart from the fact that it is not a good idea because it will 'pollute' your desktop with many intermediate build files, the path contains spaces; the GNU toolchain has known issues with such paths.

      Clifford

       
    • cpns

      cpns - 2008-03-19

      You simply need to move the full decalartion of PCB from libque.cpp to libque.h.

      C++ employs 'separate compilation', each .cpp file is independently compiled to object code and the compiler cannot 'see' definitions in other modules (that is teh responsibility of the linker). That is what the header files are for; to provide declarations of shared symbols.

      In libque.h you only have a partial (forward) declaration of PCB, so when attempting to instantiate "deque<PCB> SL" the compiler has no idea what size PCB is, and it needs this information to create a queue with the correct ammount of memory.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.