1

I found this similar problem at SO: error: ‘Page’ was not declared in this scope but it is a different situation since I'm not working on header files.

As you can see from the code below, I'm using PoDoFo library trying to print out a pdf file content to the screen

#include <iostream>
#include <podofo/podofo.h>
using namespace std;
int main()
{
  //load a new document
  //PoDoFo::PdfMemDocument pdf("myDoc.pdf");

  // this load a doc on my disk

  PoDoFo::PdfMemDocument doc;
  doc.Load("myDoc.pdf");

  //iterate over each page"
  for(int pn = 0; pn < doc.GetPageCount(); ++pn){
    PoDoFo::PdfPage* page = doc.GetPage(pn);
  }

  //
  PoDoFo::PdfContentsTokenizer tok(page);
  const char* token = nullptr;
  PoDoFo::PdfVariant var;
  PoDoFo::EPdfContentsType type;
  while (tok.ReadNext(type, token, var)) {
    if (type == PoDoFo::ePdfContentsType_Keyword) {

    }
  }
  if (var.IsArray()) {
    PoDoFo::PdfArray& a = var.GetArray();
    for (size_t i = 0; i < a.GetSize(); ++i)
      if (a[i].IsString()) { }

  }
}

This is the error:

/home/coder/QtProjects/finalProject/main.cpp:19: error: ‘page’ was not declared in this scope
  PoDoFo::PdfContentsTokenizer tok(page);

hope you can help me fix this.

Thanks!

Community
  • 1
  • 1
Hadi
  • 29
  • 7
  • 1
    The reason is obvious (for me): Declaration `PoDoFo::PdfPage* page` is done inside the scope (`{ }`) of the surrounding `for` loop. Thus, it is local there and stops to "live" outside of loop body. Therefore, you cannot access after the loop. Move the declaration (but not the assignment) before the loop and error will be solved. To be sure, I would declare and init: `PoDoFo::PdfPage* page = nullptr;` so that it has a defined value even if `GetPageCount()` returns `0`. – Scheff's Cat Apr 21 '17 at 06:12
  • Btw., I don't know who down voted and why. However, it is not kind to down vote without leaving a comment with a reason for... (Booh without explanation doesn't help anybody but makes frustration only...) – Scheff's Cat Apr 21 '17 at 06:15
  • @Scheff God bless you! i'v been facing this same problem but now i know where to go and check and get it solved. – Hadi Apr 23 '17 at 20:48

1 Answers1

0

I suspect your curly braces aren't in the correct place, as per @Scheff's comment the scope of the page variable is contained within the for loop and you are attempting to do more operations after the loop is over. I moved the } for the for loop where I think it probably should be in the code below and it should work, although I also suspect the closing } of the while loop and last if might also be in the wrong spot.

#include <iostream>
#include <podofo/podofo.h>
using namespace std;
int main()
{
  //load a new document
  //PoDoFo::PdfMemDocument pdf("myDoc.pdf");

  // this load a doc on my disk

  PoDoFo::PdfMemDocument doc;
  doc.Load("myDoc.pdf");
  PoDoFo::PdfPage* page 
  //iterate over each page"
  for(int pn = 0; pn < doc.GetPageCount(); ++pn){
    PoDoFo::PdfPage* page = doc.GetPage(pn);


  //
    PoDoFo::PdfContentsTokenizer tok(page);
    const char* token = nullptr;
    PoDoFo::PdfVariant var;
    PoDoFo::EPdfContentsType type;
    while (tok.ReadNext(type, token, var)) {
      if (type == PoDoFo::ePdfContentsType_Keyword) {

      }
    }
    if (var.IsArray()) {
      PoDoFo::PdfArray& a = var.GetArray();
      for (size_t i = 0; i < a.GetSize(); ++i)
        if (a[i].IsString()) { }

    }
 }  
}
The Puternerd
  • 632
  • 4
  • 12