Results 1 to 3 of 3

Thread: Problem reading deque pointers on objects

  1. #1
    Join Date
    May 2008
    Posts
    2,442

    Problem reading deque pointers on objects

    I have used double-ended queue in my program. But I noticed that only the last elements gets introduced.

    Here is my own class (I use "CustomRequest" to complete and "DisplayRequest" for display):

    Code:
      #include <iostream> 
      #include <fstream> 
      #include <string.h> 
      #include "Class.h" 
      using namespace std; 
      unsigned long Request:: counter = 0L; 
    
      Request:: Request() // Constructor
      {
    	  id = ++counter; 
    	  pnom = new char [MAXLEN_NAME 1]; 
    	  memset (pnom, 0, MAXLEN_NAME 1);
    	  health = 100; 
    	  exp = 0; 
    	  class = 0; 
      }
      About Me:: ~ Request(void) // Destructor 
      {
    	  delete [] pnom; 
      }
    
      Request:: Request(Request &source) 
      {
    	  if (this == &source) return; 
    	  pnom = new char [MAXLEN_NAME 1]; 
    	  strcpy (pnom source. pnom); 
    	  exp = source. exp; 
    	  counter = source. counter; 
    	  Health = source. Health; 
    	  class = source. Class; 
      }
      Request &Request:: operator = (Request &source) 
      {
    	  if (this == &source) return (*this); 
    	  strcpy (pnom source. pnom); 
    	  Exp = source. Exp; 
    	  counter = source. counter; 
    	  Health = source. Health; 
    	  class = source. Class; 
    	  return (* this); 
      }
      void Request:: DisplayRequest(Request *aRequest) 
      {
    	  cout << "Name:" <<pnom <<endl; 
    	  cout << "Experience:" <<exp <<endl; 
    	  cout << "ID:" <<id <<endl; 
    	  cout << "health" <<health <<endl; 
    	  cout << "Class:" <<grade <<endl; 
    	  cout << "Force:" <<power <<endl; 
    	  cout << "Agility:" <<AGILITY <<endl; 
    	  cout << "Intelligence:" <<Intell <<endl; 
    	  cout <<"----------------------------------------" <<endl; 
      }
      void Request:: CustomRequest (Request *aRequest) 
      { 
    	  int buflen = aRequest-> MAXLEN_NAME; 
    	  cout << "Creation" <<endl <<endl; 
    	  cout << "Name:"; 
    	  cin. ignore ();
    	  cin. getline (aRequest-> pnom, buflen); 
    	  aRequest-> pnom [buflen +1]; 
    	  cout << "Enter power:"; 
    	  cin>> force; 
    	  cout << "Enter Agility:"; 
    	  cin>> AGILITY; 
    	  cout << "Enter Intelligence"; 
    	  cin>> Intel; 
      }
    Here is my main:

    Code:
      #include "Class.h" 
      #include <deque> 
      using namespace std; 
      void main () 
      {
    	  Request* ptrav = NULL; 
    	  ptrav=new Request;
    	  DEQUE <Request*> dprequest; 
    	  DEQUE <Request*>:: iterator IPB 
            ptrav-> CustomRequest (ptrav);
    	  dprequest. push_back (ptrav); 
    	  ptrav-> CustomRequest (ptrav); 
    	  dprequest. push_back (ptrav); 
    	  System ( "cls"); 
    	  for (IPB = dprequest. begin (); IPB! = dprequest. end (); IPB + +) 
    	  { 
    		  ptrav-> DisplayRequest(IPB *); 
    	  } 
    	  cout << "the DEQUE a" <<dprequest. size () << "elements" <<endl; 
    	  System ( "pause"); 
      }

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Problem reading deque pointers on objects

    Your code seem to me a very complicated one. I would rather use a member function show() in class Request. And then code in this way:

    Code:
      for (ipb = dprequest.begin (); ipb!= dprequest.end (); ipb++)  
      {
    	  ipb-> show(); 
      }

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem reading deque pointers on objects

    DisplayRequest and CustomRequest applies on a personal object (they are clearly not static) and takes as parameter a pointer to own? It needs little reading/modification of members of the object and the parameters. Finally, why are you using a char* for your string and not std::string?

Similar Threads

  1. There was a problem reading this document (14)
    By Paramartha in forum Software Development
    Replies: 4
    Last Post: 11-01-2011, 06:43 PM
  2. problem in Reading XML via LINQ
    By Steadfast in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 08:29 PM
  3. Problem in reading stream
    By Windowed in forum Windows Software
    Replies: 5
    Last Post: 06-01-2010, 11:15 AM
  4. Problem reading C++ matrix
    By killerboy in forum Software Development
    Replies: 4
    Last Post: 14-11-2009, 05:07 PM
  5. Problem reading USB stick
    By - Empty Shell - in forum Portable Devices
    Replies: 3
    Last Post: 05-02-2009, 08:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,048,746.37844 seconds with 16 queries