'FILE fp is private within this context'

This is a discussion on 'FILE fp is private within this context' within the C++ Programming forums, part of the General Programming Boards category; i have two .cpp files (actually three but one isnt giving me this problem) in ISBN.cpp the decode function is ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    'FILE fp is private within this context'

    i have two .cpp files (actually three but one isnt giving me this problem)

    in ISBN.cpp the decode function is calling ISBNPrefix's FILE fp
    ISBN.cpp: <<this is the implementation file where ISBNPrefix's FILE fp is being called
    Code:
    #include <cstdlib>
    #include <cstdio>
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <algorithm>
    #include <functional>
    #include <cctype>
    using namespace std;
    #include "ISBN.h"
    
    //code....
    .
    .
    .
    int ISBN::decode(const ISBNPrefix& list){
      /* *DECODE THE ISBN* */
      int area1 = 0, b=1, n=0, min, length, publoop = 0, pub, checkpublenght, pubcheckloop=0, publenght, publisherloop=0, area_pub, titlelenght, ret=0;
      char o[11], areatostring[7], publishercheck[8], arealoop[7], pub1loop[7], pub2loop[7], pubsend[8], title1[8];
      strcpy(o,ISBN1);
      area1 = o[0] - 48;
      if(valid(ISBN1)==0){/*check if number is valid first*/
       ret = 0;
      }
      else {/*if valid start decoding*/
       while (n!=1){
        if(list.registered(area1)==1) {
         min = list.minNoDigits(area1);/*get the minimum digits for publisher*/
         sprintf(areatostring, "%i", area1); 
         length = strlen(areatostring);
         strcpy(publishercheck-length,o);/*take the area out str for checking*/
         sprintf(areatostring, "%i", area1);
         rewind(list.fp);
         while (!feof(list.fp)) {/*find the amount of time the area to check comes up*/
          fscanf(list.fp, "%s %s %s", arealoop, pub1loop, pub2loop);  
          if(strcmp(areatostring,arealoop)==0){
           publoop++;
          }
         }
         if(publoop==8){
          publoop=7;
         }
         while (publisherloop!=1) {
          strncpy(pubsend,publishercheck,min);
          pubsend[min] = '\0';
          pub = atoi(pubsend);
          fscanf(list.fp, "%s %s %s", arealoop, pub1loop, pub2loop);
          if(list.registered(area1, pubsend)==1){/*if publisher is found*/
           strcpy(area,areatostring);/*area equals the area sent*/
           strcpy(publisher,pubsend);
           area_pub = strlen(area) + strlen(publisher);
           titlelenght = 10 - area_pub - 1;	
           strcpy(title1-area_pub,o);/*find lenght to determine the title and take out the check digit*/
           strncpy(title,title1,titlelenght);
           title[titlelenght] = '\0';
           n = 1;/*stop loop, everything is found*/
           publisherloop = 1;
           ret = 1;/*return is now true*/
          }
          else {	
           min++;
          }                
         }                                      
        }
        else{/*if check is false add an addition digits to the area*/
         area1 = area1 * 10 + (o[b] - 48);
         b++; 
         if(b>5){/*if the area goes over 5 checks there is no valid area*/		
          n = 1;
          ret = 0;
         }
        }
       }
      }
      return ret;    
    }
    .
    .
    .
    ISBN.h:
    Code:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #ifndef ISBN_H
    #define ISBN_H
    #endif
    #include "ISBNPrefix.h"
    class ISBNPrefix;
    int valid(const char* str);
    class ISBN{
      char ISBN1[11];
      char area[6];
      char publisher[8];
      char title[8];
      char s;
      int isRegistered;
      int decode(const ISBNPrefix& list);
     public:
      ISBN(); 
      ISBN(const char* str, const ISBNPrefix& list);
      ~ISBN();
      void style(char c);
      int empty() const;
      int registered() const;
      void toStr(char* isbn) const;
      void toStrWithStyle(char* isbn) const;
      void display(ostream& os) const; /* not */
      int accept(istream& is, const ISBNPrefix& list);
      friend int operator==(const ISBN& left, const ISBN& right);  
    };
    ISBNPrefix.h: <<this is the header file for ISBNPREFIX where FILE fp is declared
    Code:
    /*Matthew Faria*/
    /*Assignment 2*/
    /*ISBNPrefix.h*/
    /*Header file for IBSNPrefix.h*/
    class ISBNPrefix{
      FILE* fp;
     public:
      ISBNPrefix(const char* filename);  
      ISBNPrefix(const ISBNPrefix& source);
      ~ISBNPrefix();
      int registered(int area) const;
      int minNoDigits(int area) const;
      int registered(int area, const char* publisher) const;
      ISBNPrefix& operator=(const ISBNPrefix& source);    
    };
    anyone know how i can fix this?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Cheap way: Make fp not private.
    Proper way: Have the class handle the details of fp, or properly give the pointer by means of a get() member function.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i was going to go the cheap way but my prof would probably want me to redo this assignment again. I don't know how to use the proper way i tried googling get() but i don't know exactly what i am looking for.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Another cheap way: declare ISBN as a friend to ISNBPrefix.

    But the real question is: why does something called ISBNPrefix have a FILE* member in the first place?

    Edit:
    I don't know how to use the proper way i tried googling get() but i don't know exactly what i am looking for.
    A getter: a public user-defined function that returns the FILE* in question.
    Last edited by anon; 07-20-2008 at 09:36 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You make it yourself.....

    Code:
    class something
    {
        private:
            int x;
        public:
            /* Constructors and anything else goes here */
            int getX()
            {
                return x;
            }
    };
    In the same vein, people also have a set() method usually that allows you to make your private variables equal to something.

    This is all about class design and such, depending on what you want your clases used for and how you want them used.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    oh, i see know

    btw anon, thats how my prof wanted it made im just following his rules (in reponse to "why does something called ISBNPrefix have a FILE* member in the first place?")

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Getting a new problem now, my implementation files have no error anymore but now my main won't compile, i attached every file need to run this if anyone can see what i should omit or add.

    ill post the header files here:
    isbn.h:
    Code:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #ifndef ISBN_H
    #define ISBN_H
    #include "ISBNPrefix.cpp"
    #endif
    int valid(const char* str);
    class ISBN{
      char ISBN1[11];
      char area[6];
      char publisher[8];
      char title[8];
      char s;
      int isRegistered;
      int decode(const ISBNPrefix& list);
     public:
      ISBN(); 
      ISBN(const char* str, const ISBNPrefix& list);
      ~ISBN();
      void style(char c);
      int empty() const;
      int registered() const;
      void toStr(char* isbn) const;
      void toStrWithStyle(char* isbn) const;
      void display(ostream& os) const;
      int accept(istream& is, const ISBNPrefix& list);
      friend int operator==(const ISBN& left, const ISBN& right);
      friend class Order;  
    };
    isbnprefix.h:
    Code:
    #ifndef ISBNPrefix_H
    #define ISBNPrefix_H
    #endif
    class ISBNPrefix{
      FILE* fp;
     public:
      ISBNPrefix(const char* filename);  
      ISBNPrefix(const ISBNPrefix& source);
      ~ISBNPrefix();
      int registered(int area) const;
      int minNoDigits(int area) const;
      int registered(int area, const char* publisher) const;
      ISBNPrefix& operator=(const ISBNPrefix& source);  
      friend class ISBN;  
    };
    order.h:
    Code:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #ifndef Order_H
    #define Order_H
    #include "ISBN.h"
    #endif
    class Order{
      char s;
      int ordered;
      int delivered;
      char ISBN1[11];
     public:
      Order();
      Order(const ISBN& isbn);
      void style(char c);
      int has(const ISBN& isbn) const;
      int onOrder() const;
      int augment(istream& is); //
      int augment(int add);
      int receive(istream& is); //
      void display(ostream& os) const; //
    };
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> now my main won't compile
    What errors do you get?

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    here is a screenshot of the errors
    Code:
    http://img296.imageshack.us/img296/8057/51693028ww2.png
    edit: dev-c++ is the program im using

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing, you still don't have your inclusion guards right.

    Everything within a header goes between #ifndef/#define and #endif.

    And next time, hit Compile more often. Don't write tons of code without compiling and testing it so far.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    like this?
    Code:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include "ISBNPrefix.h"
    #ifndef ISBN_H
    #define ISBN_H
    int valid(const char* str);
    class ISBN{
      char ISBN1[11];
      char area[6];
      char publisher[8];
      char title[8];
      char s;
      int isRegistered;
      int decode(const ISBNPrefix& list);
     public:
      ISBN(); 
      ISBN(const char* str, const ISBNPrefix& list);
      ~ISBN();
      void style(char c);
      int empty() const;
      int registered() const;
      void toStr(char* isbn) const;
      void toStrWithStyle(char* isbn) const;
      void display(ostream& os) const;
      int accept(istream& is, const ISBNPrefix& list);
      friend int operator==(const ISBN& left, const ISBN& right);
      friend class Order;  
    };
    #endif

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, everything. Otherwise, I assume the preprocessor might go on endlessly including the part of the header that is not protected.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    oh alright i got it now, now that they all have no syntax error i still get the "undefined reference to" that i was getting in the beginning.

    Every function called in main that is part of one of the three classes says "undefined reference to..."

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Sounds like you're not linking everything together at once.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    anyways to fix that, or would you have to compile yourself

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21