Thread: Syntax Issue - Namespace and Proper Way to Declare

  1. #1
    Registered User TangoOversway's Avatar
    Join Date
    Mar 2013
    Posts
    20

    Syntax Issue - Namespace and Proper Way to Declare

    I'm working step by step with simple things to make sure I'm getting C++ syntax correct (especially with namespaces, and pointers!).

    I'm working with Poppler, a library for working with PDF files. There is, apparently, no constructor for a Poppler Document. Once I have the document object, I can load_from_file(), though.

    The problem I'm having is that I am trying to declare the object pDoc, then use it in a loop. In each iteration, I'm going to load a new file into pDoc.

    Here is the link to the docs that cover the poppler::document class:

    https://oinksoft.com/doc/poppler/cpp...1document.html

    I do notice that the load_from_file() is static, so my assumption is that I use it to create a document (since there's no constructor), but I'm not clear on the syntax and formatting.

    Right now I have:

    Code:
    ...
    #include "poppler/cpp/poppler-document.h"
    using namespace std;
    ...
    void addPDFs(list<string> fList) {
       list<string>::iterator iList;
       poppler::document pDoc;      //Problem Line #1
       int x = 1;
       int pNum = -1;
    
    
       for (iList = fList.begin(); iList != fList.end(); iList++) {
          pDoc.load_from_file(*iList);
          pNum = pDoc.pages();      //Problem Line #2
          cout << "Item : " << x << " : " << *iList << ", Total Pages: "
                << pNum << endl;
       }
    }
    (There may be other errors in my code - I can look them up and find them through debugging and I'll remember them better that way, but I'm having trouble with this one because I can't find any example code for this class.)

    I'm having problems with two lines (marked with comments). In the first I'm declaring pDoc (and eventually it will be used outside of the loop). When I compiled this program, Eclipse gave me the error, "no matching function for call to 'poppler::document::document()'" (The point that it seems to know what's available makes me think I'm at least referring to the poppler::document object correctly.)

    What I don't see is how to declare pDoc here properly (I don't want to load a file in yet).

    Then, on the 2nd problem line, I think that should be something like:

    Code:
    pDoc = poppler::document::load_from_file(filename)
    I'm willing to bet that the answer is pretty obvious - I'm just not sure how those two lines should be written to declare pDoc and, later, to load in the file and create a new instance.
    Last edited by TangoOversway; 03-06-2013 at 01:57 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    poppler::document *pDocument = poppler::document::load_from_file(...);
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User TangoOversway's Avatar
    Join Date
    Mar 2013
    Posts
    20
    Okay, I see - and it makes sense. I missed the obvious. Thank you, brewbuck.

    So this is something I can't really declare until I create it with that statement?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TangoOversway View Post
    Okay, I see - and it makes sense. I missed the obvious. Thank you, brewbuck.

    So this is something I can't really declare until I create it with that statement?
    No, you could just initialize the pointer to NULL until you're ready to open the document. But if you try, you can usually find a way to not actually declare that variable until right before it gets used the first time. There are times when that's not possible, but I doubt this is one of those times.

    Also, I don't know the Poppler API in detail, but you probably need to call delete on that pointer when you are done with the document and ready to open the next one -- otherwise you'll leak memory like crazy.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should let them know, if possible, that it's better to throw an exception on failure than returning a pointer, and if returning a raw pointer is a must, then it should absolutely be declared in the documentation if it should be deleted or not.
    I can't really seem to find any reference to the poppler library from the main page...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User TangoOversway's Avatar
    Join Date
    Mar 2013
    Posts
    20
    Quote Originally Posted by Elysia View Post
    You should let them know, if possible, that it's better to throw an exception on failure than returning a pointer, and if returning a raw pointer is a must, then it should absolutely be declared in the documentation if it should be deleted or not.
    I can't really seem to find any reference to the poppler library from the main page...
    Let's just say I'm still getting used to pointers. (I worked with them in 65C02 Assembler in the 1980s, but haven't touched them in over 20 years.) So we won't go into the idiot newbie mistake about the person who mis-read the documentation and didn't notice that until after brewbuck pointed it all out.

    Yep, that was part of my mistake - one I'll get right next time. (I have no problem understanding the concept of pointers - it's getting used to the syntax that I'm working on.)

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    You should let them know, if possible, that it's better to throw an exception on failure than returning a pointer, and if returning a raw pointer is a must, then it should absolutely be declared in the documentation if it should be deleted or not.
    I can't really seem to find any reference to the poppler library from the main page...
    There are significant issues throwing exceptions across DLL boundaries. Every component involved must be linked against the same C++ runtime. That is often impossible to assure when using collections of third party code. I wish it wasn't so... But it is.

    Poppler is derived from xpdf, which itself was written in a very C-like way. I made some relatively minor contributions to that code base in a past life (having to do with how it identifies, handles, and reports errors). xpdf, and I assume Poppler, are good solid code, but not the best example I've seen of perfect API design. It could be much worse, though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with namespace function declared as friend of class
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2012, 12:46 PM
  2. Replies: 13
    Last Post: 02-13-2012, 03:01 AM
  3. namespace :: syntax
    By rodrigorules in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2010, 05:50 PM
  4. proper function call syntax
    By jerrykelleyjr in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2004, 08:43 PM
  5. proper syntax for const array of strings?
    By cozman in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 01:38 PM

Tags for this Thread