[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem Compiling

Posted on 2005-04-20
41
Medium Priority
?
256 Views
Last Modified: 2010-04-01
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!
0
Comment
Question by:luna621
  • 27
  • 13
41 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 13822029
You'll have to remove
  #include "llist.cpp"
from llist.h file
0
 

Author Comment

by:luna621
ID: 13822055
------ 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
 
LVL 16

Expert Comment

by:nonubik
ID: 13822074
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:luna621
ID: 13822093
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
 
LVL 16

Accepted Solution

by:
nonubik earned 2000 total points
ID: 13822102
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
 

Author Comment

by:luna621
ID: 13822111
OMG!!  Order matters?!  I didn't know that!!  Okay, let me see if the program runs correctly.   I'll post back soon.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13822133
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
 

Author Comment

by:luna621
ID: 13822176
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13822207
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
 

Author Comment

by:luna621
ID: 13822228
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13822235
Place a breakpoint in writefile() and proceed step-by-step.
0
 

Author Comment

by:luna621
ID: 13822243
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
 

Author Comment

by:luna621
ID: 13822250
>>  Place a breakpoint in writefile() and proceed step-by-step.

okay
0
 

Author Comment

by:luna621
ID: 13822270
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
 

Author Comment

by:luna621
ID: 13822287
The search was unsuccessful.  The writefile() is definately not saving the information as it should.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13822323
After palcing the breakpoint, *please* start in debug mode. (if you're using Visual C++ press F5).
0
 

Author Comment

by:luna621
ID: 13822341
That's what I tried, but the console blinks and closes when it reaches the writefile() breakpoint I put in.
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13822386
Of course. Then proceed step by step through the writefile() function. Does it reach the fprintf line? Does it then closes ok fclose()?
0
 

Author Comment

by:luna621
ID: 13822402
           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
 

Author Comment

by:luna621
ID: 13822411
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13822413
> exit(0);
replace with
>return 0;
0
 

Author Comment

by:luna621
ID: 13822457
------ 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
 
LVL 16

Expert Comment

by:nonubik
ID: 13822482
case 6:
                    cout<<"\n\nThank you.  Now exiting...\n\n";
                         return 0;
                 
0
 

Author Comment

by:luna621
ID: 13822558
Nope.  I ran it again, but the list is still empty.  Maybe I didn't call the readfile()??  Let me check.
0
 

Author Comment

by:luna621
ID: 13822580
...
    /*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
 

Author Comment

by:luna621
ID: 13822672
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13823223
>However, how the format that prints is weird...

How weird?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13824429
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
 

Author Comment

by:luna621
ID: 13827332
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
 

Author Comment

by:luna621
ID: 13827343
0
 

Author Comment

by:luna621
ID: 13827478
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
 

Author Comment

by:luna621
ID: 13827500
Ok, it crashed and wouldn't write.
0
 

Author Comment

by:luna621
ID: 13827511
Whoops!  Forgot to change to this: fprintf(writing, "%s\n%s$\n%d\n%s\n",...

Let's see if it works now.
0
 

Author Comment

by:luna621
ID: 13827563
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13831695
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13831708
/* 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
 

Author Comment

by:luna621
ID: 13840673
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
 

Author Comment

by:luna621
ID: 13840706
0
 

Author Comment

by:luna621
ID: 13840716
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
 

Author Comment

by:luna621
ID: 13840720
Okay, I'm making another question since this is actually another topic.  Awarding points now.  Thank you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question