Problem Compiling

Hi,

I'm having problems compiling my program.  First, I used <iostream.h>, but aparently Microsoft Visual .NET 2003 v7.1 doesn't use that anymore.  Anyways, I used <iostream> and using namespace std instead.  However, once I changed that all these errors popped up like: error C2084: function 'llist::llist(char [])' already has a body...

I uploaded the files here: http://www2.hawaii.edu/~lilyllam/C++/

-------------------------------------------------------------------
*NOTE:
-------------------------------------------------------------------
1. UI.cpp contains the main()
2 .error_log.txt is the current error log.



Please let me know what I'm doing wrong.  Thank you!
luna621Asked:
Who is Participating?
 
nonubikConnect With a Mentor Commented:
Change the include order, in LLIST.CPP, of the llist.h and record.h:

---------LLIST.CPP

#include<iostream>
#include "record.h"
#include "llist.h"
using namespace std;

llist::llist()
{
.............
0
 
nonubikCommented:
You'll have to remove
  #include "llist.cpp"
from llist.h file
0
 
luna621Author Commented:
------ Build started: Project: Proj2, Configuration: Debug Win32 ------

Compiling...
llist.cpp

llist.cpp(5) : error C2653: 'llist' : is not a class or namespace name
llist.cpp(7) : error C3861: 'pm': identifier not found, even with argument-dependent lookup
llist.cpp(8) : error C2065: 'start' : undeclared identifier
llist.cpp(9) : error C2065: 'filename' : undeclared identifier
llist.cpp(10) : error C2065: 'debug' : undeclared identifier
llist.cpp(11) : error C3861: 'readfile': identifier not found, even with argument-dependent lookup
llist.cpp(13) : warning C4508: 'llist' : function should return a value; 'void' return type assumed
llist.cpp(14) : error C2653: 'llist' : is not a class or namespace name
llist.cpp(16) : error C3861: 'pm': identifier not found, even with argument-dependent lookup
llist.cpp(17) : error C3861: 'filename': identifier not found, even with argument-dependent lookup
llist.cpp(18) : error C3861: 'debug': identifier not found, even with argument-dependent lookup
llist.cpp(19) : error C3861: 'readfile': identifier not found, even with argument-dependent lookup
llist.cpp(20) : warning C4508: 'llist' : function should return a value; 'void' return type assumed
llist.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
nonubikCommented:
Are you sure you removed from LLIST.H the line

#include "llist.cpp"

and not viceversa (removed the include .h from the .cpp) ?

What I am talking about is the last line before the #endif line in the header (llist.h)
0
 
luna621Author Commented:
Oh, that #include "llist.cpp".  I thought you were talking about the other one.  Sorry.  Thank you for clarifying.  Okay, now there are these errors:

------ Build started: Project: Proj2, Configuration: Debug Win32 ------

Compiling...
llist.cpp
llist.h(7) : error C2143: syntax error : missing ';' before '*'
llist.h(7) : error C2501: 'llist::record' : missing storage-class or type specifiers
llist.h(7) : error C2501: 'llist::start' : missing storage-class or type specifiers
llist.cpp(9) : error C2065: 'start' : undeclared identifier
llist.cpp(125) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(147) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(150) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(151) : error C2227: left of '->next' must point to class/struct/union
        type is ''unknown-type''
llist.cpp(151) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(151) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(184) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(194) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(212) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(265) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(302) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(334) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(335) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(345) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(346) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(356) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(357) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(383) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(383) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(430) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(436) : error C3861: 'start': identifier not found, even with argument-dependent lookup
llist.cpp(485) : error C3861: 'start': identifier not found, even with argument-dependent lookup

Proj2 - 26 error(s), 0 warning(s)

---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped
0
 
luna621Author Commented:
OMG!!  Order matters?!  I didn't know that!!  Okay, let me see if the program runs correctly.   I'll post back soon.
0
 
nonubikCommented:
Order matters. The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears.

So, if they were like before, the struct 'record' would have been declared *after* the llist class declaration, class that has a 'record' type member. If you change the order, the struct is declared before.
0
 
luna621Author Commented:
Hmm... my writefile(), and in turn readfile() are not working.  I want it to write to records.txt, but it's not...

Updated codes: http://www2.hawaii.edu/~lilyllam/C++/
0
 
nonubikCommented:
But the compile errors are gone?

You have then to be more specific: what is not working? Do you have any recodrd? Try to do some debugging and see what's wrong.
0
 
luna621Author Commented:
It's not saving to a file called records.txt.  My program calls records.txt in the readfile() at the beginning, and is supposed to save any added records when the program quits using the writefile().  I think the writefile() is not saving my session to records.txt.  Therefore, each time I open the program, the list is empty!
0
 
nonubikCommented:
Place a breakpoint in writefile() and proceed step-by-step.
0
 
luna621Author Commented:
llist::llist()
{
       pm("Inside Default Constructor");
       start = NULL;
       strcpy(filename,"records.txt");// default data file
       debug = 0; // true
       readfile();

}

//sets the file to be saved as records.txt

...

readfile(...)
{
 if ((reading = fopen (filename, "r")) == NULL) //<------------------- here's the file
   {
           pm("File not found"); /* check that file has opened */
           return 0;
   }
   if(debug == 1)
   {
       pm("\nNow reading file, and printing to screen...\n\n");
   }
   ...
}

...

int llist::writefile()
{
   FILE *writing;

       pm ("Inside function writefile()");
   struct record *curr = start;
   pm("\nNow opening file...");

   if((writing = fopen(filename, "w")) == NULL) //<------------------------ here's the file
   {
       pm("File not found"); /* check that file has opened */
       return -1;
   }
...
0
 
luna621Author Commented:
>>  Place a breakpoint in writefile() and proceed step-by-step.

okay
0
 
luna621Author Commented:
Umm... the console blinked, then closed.  It was too fast, and I could see anything.  Is it supposed to do that?  I'm currently doing a seach on my computer to see if it got saved somewhere else.  Any ideas?
0
 
luna621Author Commented:
The search was unsuccessful.  The writefile() is definately not saving the information as it should.
0
 
nonubikCommented:
After palcing the breakpoint, *please* start in debug mode. (if you're using Visual C++ press F5).
0
 
luna621Author Commented:
That's what I tried, but the console blinks and closes when it reaches the writefile() breakpoint I put in.
0
 
nonubikCommented:
Of course. Then proceed step by step through the writefile() function. Does it reach the fprintf line? Does it then closes ok fclose()?
0
 
luna621Author Commented:
           case 6:
                    cout<<"\n\nThank you.  Now exiting...\n\n";
                    exit(0);
                    break;

I think that's my problem.  It doesn't call the writefile().  My writefile() looks okay... I think:



int llist::writefile()
{
   FILE *writing;

       pm ("Inside function writefile()");
   struct record *curr = start;
   pm("\nNow opening file...");

   if((writing = fopen(filename, "w")) == NULL)
   {
       pm("File not found"); /* check that file has opened */
       return -1;
   }
   while(curr != NULL)
   {
       /* write each record */
       fprintf(writing,"%s\n%s$\n%s\n%d\n", curr -> name , curr -> address , curr -> telno, curr -> yearofbirth);
       curr = curr->next;
   }
   pm("\n\nNow writing file, and printing to screen...\n\n");
   fclose(writing);
   return 1;
}
0
 
luna621Author Commented:
okay, added this:

            case 6:
                    cout<<"\n\nThank you.  Now exiting...\n\n";
                              list.writefile(); //<------------------------------------
                    exit(0);
                    break;


HOWEVER, writefile() is private.  How do I access it?
0
 
nonubikCommented:
> exit(0);
replace with
>return 0;
0
 
luna621Author Commented:
------ Build started: Project: Proj2, Configuration: Debug Win32 ------

Compiling...
UI.cpp

UI.cpp(32) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
UI.cpp(246) : error C2248: 'llist::writefile' : cannot access private member declared in class 'llist'
llist.h(10) : see declaration of 'llist::writefile'
llist.h(5) : see declaration of 'llist'
0
 
nonubikCommented:
case 6:
                    cout<<"\n\nThank you.  Now exiting...\n\n";
                         return 0;
                 
0
 
luna621Author Commented:
Nope.  I ran it again, but the list is still empty.  Maybe I didn't call the readfile()??  Let me check.
0
 
luna621Author Commented:
...
    /*debug = 0;*/
      
//    readfile(&start,"records.txt"); //<--------------------  Oh!  I had it commented out!

list.readfile(); // <----------------- added this in, but see error listed below...

    mainMenu(); /* displays the menu options to user */
      cin>>choice;

    while(choice != 6)
    {
        switch(choice)
        {
            case 1: /* Getting name */
...

-----------------------------------------------
errors:

------ Build started: Project: Proj2, Configuration: Debug Win32 ------

Compiling...
UI.cpp

UI.cpp(32) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
UI.cpp(115) : error C2248: 'llist::readfile' : cannot access private member declared in class 'llist'
llist.h(9) : see declaration of 'llist::readfile'
llist.h(5) : see declaration of 'llist'

---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped
0
 
luna621Author Commented:
Okay, readfile() is accessed in the constructor (I forgot):

llist::llist()
{
       pm("Inside Default Constructor");
       start = NULL;
       strcpy(filename,"records.txt");// default data file
       debug = 0; // true
       readfile();

}

I added writefile() in the deconstructor:

llist::~llist()
{
       pm("Inside Deconstructor ~llist()");
       // save data before object destroy
       writefile(); //<------------------------
       // clean the memory
       cleanup();

}


However, how the format that prints is weird...
0
 
nonubikCommented:
>However, how the format that prints is weird...

How weird?
0
 
itsmeandnobodyelseCommented:
I downloaded your prog and it compiles (seems as you removed #include "llist.cpp" in the meantime".

I have some comments and suggestions not necessarily touching the current problems you are discussing with nonubik.

(1) private readfile/writefile function

Private functions can be called only by member functions of the same class. You should make these functions public cause llist class itself could not decide when to read or write from/to file.

(2) readfile and writefile should be called by main

Make list.readfile() active *before* while loop and call list.writefile after any modification of the file (insert/modify/remove).

(3) Turn the 'while' loop to a 'do while' loop

    list.readfile(&start,"records.txt");

    do
    {
        mainMenu(); /* displays the menu options to user */
        cin>>choice;

        switch(choice)
        {
                ...
            case 6:
                    cout<<"\n\nThank you.  Now exiting...\n\n";
                    // exit(0);
                    break;
            ...
        }
     }
    while(choice != 6);  

    return(0);


Regards, Alex
0
 
luna621Author Commented:
Sorry, that's not my updated codes.  The readfile() and writefile() are private... they work, but only the 1st time you run the program.  The 2nd time I run it, the records I add are saved in a strange format:

----------------------------------------------------------------------------
1st time adding 2 records:
----------------------------------------------------------------------------
John
127 Hello$
586-2857
1968
John
182 Yellow$
294-5849
1978

----------------------------------------------------------------------------
2nd time adding 1 new record:
----------------------------------------------------------------------------

John

127 Hello$
$
586-2857

1968
John

182 Yellow$
$
294-5849

1978
John
38 Fish$
384-6739
1980



-------------------------------------------------------------------

As you can see the formating is very off.  That's why when I open it a 3rd time, the program crashes.  I'll upload the updated codes.
0
 
luna621Author Commented:
0
 
luna621Author Commented:
Okay, fprintf(writing, "%s\n%s$\n%s\n%d\n", curr -> name , curr -> address, curr -> telno, curr -> yearofbirth);

changed to:

fprintf(writing, "%s\n%s$\n%s\n%d\n", curr -> name , curr -> address , curr -> yearofbirth, curr -> telno);


Checking to see if that works...
0
 
luna621Author Commented:
Ok, it crashed and wouldn't write.
0
 
luna621Author Commented:
Whoops!  Forgot to change to this: fprintf(writing, "%s\n%s$\n%d\n%s\n",...

Let's see if it works now.
0
 
luna621Author Commented:
Nope, same problem:

----------------------------------------------------------------------------
Records stored:
----------------------------------------------------------------------------

John
                   <------------ extra space
127 Hello$
$                 <------------ extra $
586-2857
                  <------------ extra space
1968
John          
                  <------------ extra space
182 Yellow$
$                 <------------ extra $
294-5849
                   <------------ extra space

1978            <------------ supposed to look like this format
John
38 Fish$
384-6739
1980
0
 
nonubikCommented:
fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

Hence the 'extra space' you goth there (in fact a new line). You need to modify the readfile():

fgets(line,25,reading);
strcpy(temp->name,line);
int iLen = strlen(temp->name);
if(temp->name[iLen - 1] == '\n')
  temp->name[iLen - 1] = 0; //remove the last character if 'new line'

And wherever you use the fgets.
0
 
nonubikCommented:
/* load all the lines.... */
       while(1)
       {
           fgets(line, 80, reading);
           strcat(temp -> address, line);

           if(line[strlen(line) - 2] == '$')
           {
               break;
           }
       } /* end while */

change to

/* load all the lines.... */
       while(1)
       {
           fgets(line, 80, reading);
           strcat(temp -> address, line);

           if(line[strlen(line) - 2] == '$')
           {
               temp->address[strlen(temp->address) - 2] = 0; //remove the $ and the 'newline', too
               break;
           }
       } /* end while */
0
 
luna621Author Commented:
It saves file like this after I ran the program twice:

John Smith
                  <------------ extra space here
134 Street
AAA, BB$
$                <------------ extra $ sign here
374-5748
                  <------------ extra space here
1983
Jane Doe
57-4738 Star
USA$
385-7483
1956



It's supposed to save in this format:
-----------------------------------------

Jane Doe
57-4738 Star
USA$
385-7483
1956
0
 
luna621Author Commented:
0
 
luna621Author Commented:
Sorry, the records.txt is supposed to look like this:
---------------------------------------------------------

John Smith
134 Street
AAA, BB$
374-5748
1983
Jane Doe
57-4738 Star
USA$
385-7483
1956
0
 
luna621Author Commented:
Okay, I'm making another question since this is actually another topic.  Awarding points now.  Thank you.
0
 
luna621Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.