Results 1 to 6 of 6

Thread: Help with a class. Any advice appreciated.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    19

    Help with a class. Any advice appreciated.

    Hi,

    I don't know where my program is going wrong, but I can't get it to compile. I have 3 files, dq.h, dq.cpp and dqtester.cpp

    I have no problems with adding functions to the linked list once its done, I just need to class to compile so I can add functions to it. I have no idea where I am going wrong. If someone can point out where I have gone wrong I would appreciate it, I really need this to compile so I can start adding functions and making the queue work.

    dq.h

    Code:
    #include <iostream>
    
    struct node
    {
    
      int item;
      node* next;
    
    };  
    
    class dqueue
    {
    
      public:
        dqueue( );
        bool empty( );
      
      private:
        int size;
        node* front;
    
    };
     
    
    #include "dq.cpp"
    #endif
    dq.cpp

    Code:
    #include "dq.h"
    
    dqueue::dqueue( )
    {
      front = NULL;
      size = 0;
    }
    
    bool dqueue::empty()
    {
      if (size == )
        return true;
      return false;
    }
    dqtester.cpp

    Code:
    #include <iostream.h>
    #include "dq.h"
    
    void main()
    {
    
          cout<<"Yes!";
    }
    I am really bad with classes but I am trying to learn. I have been mucking around with this, rewriting it over and over again for hours. Its 2.30 am. If someone can point out where I have gone wrong I would appreciate it. I don't know if I am meant to declare the node struct in a different file then the .h or if I have a syntax error or logic error but I am pulling my hair out and I can't take it anymore

    I know some of you guys are very good at C++ - If you can give me any advice about getting good at classes I would be grateful. I know they are important, I just can't get good at them no matter what I do. I guess I just need practise.

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    in dqueue::empty():
    Code:
      if (size == )
        return true;
    Didn't you just forget to give the second variable for the comparision of "size"?

    Another problem might be that you declared node as a structure. If you want to use that structure as a variable type, you need to declare "front" and "next" as "struct node* front" and "struct node* next". (The same goes for the "next" variable inside the structure.) Another option is to declare the structure as following:
    Code:
    typedef struct node_t
    {
    
      int item;
      node* next;
    
    } node;
    This will declare node_t as a structure and with the typedef statement you create a new variable type node as struct node_t.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No riis, that goes only for C, in C++ his way is ok.

    But the other thing should be right.

    Look, Michael_Bray, you IDE (VC++ or Borland or whatever) should have a small output window at the bottom where compile errors are written to. Double-clicking such an error will take to directly to the line where the error occurred. Search in this line (and a few above and beyond) for the error. The compiler also outputs an error message. In your case it should be
    error C2059: syntax error : ')'
    assuming you use VC++.

    What does that mean? That in the line the message points to:
    if (size == )
    the compiler encountered something it couldn't do anything with. In this case it was the ')'. ) is no expression that could be compared to something else, so ) is a syntax error here.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Re: Help with a class. Any advice appreciated.

    Also,
    Originally posted by Michael_Bray
    Code:
    #include "dq.cpp"
    You should never be #include'ing .c or .cpp files, only .h files.
    As long as the .cpp file is a part of your project, it will be compiled.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    19
    Thanks for pointing that out. I feel like an idiot I spent hours on that, looking for some weird problem.

  6. #6
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Originally posted by CornedBee
    No riis, that goes only for C, in C++ his way is ok.
    Well, I guess I've learnt something more. The book from which I learnt C++ didn't mention it, probably because it went to dealing with classes almost immediately.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width