1

I still new in GIS and C++ may someone help regarding this. The code below will display the :

"Error 3 error C4700: uninitialized local variable 'pszFilename' used"

IT is the pszFilename is UTF-8 encoded?

How to initialize it?

int main(int argc, char** argv)
{

    class GDALDataset;

    const char* pszFilename;


    GDALDataset *poDataset;
    void CPL_DLL CPL_STDCALL GDALAllRegister(void);

    poDataset = (GDALDataset *) GDALOpen(pszFilename, GA_ReadOnly )CPL_WARN_UNUSED_RESULT;
    if( poDataset == NULL )
    {}

}

I try tutorial in getting the data from the raster map by follow tutorial in https://www.gdal.org/gdal_tutorial.html It display this kind of error that make my head headache. This error shows that my gdal already set succesfully right?

 Error  3   error C2027: use of undefined type 'GDALDataset'    
    Error   6   error C2027: use of undefined type 'GDALDataset'    
    Error   9   error C2027: use of undefined type 'GDALDataset'    
    Error   12  error C2027: use of undefined type 'GDALDataset'    
    Error   14  error C2027: use of undefined type 'GDALDataset'    t
    Error   16  error C2027: use of undefined type 'GDALDataset'    
    Error   18  error C2027: use of undefined type 'GDALDataset'    
    Error   5   error C2227: left of '->GetDescription' must point to class/struct/union/generic type
    Error   4   error C2227: left of '->GetDriver' must point to class/struct/union/generic type 
    Error   7   error C2227: left of '->GetDriver' must point to class/struct/union/generic type 
    Error   19  error C2227: left of '->GetGeoTransform' must point to  class/struct/union/generic type
    Error   8   error C2227: left of '->GetMetadataItem' must point to class/struct/union/generic type
    Error   15  error C2227: left of '->GetProjectionRef' must point toclass/struct/union/generic type
    Error   17  error C2227: left of '->GetProjectionRef' must point to class/struct/union/generic type 
    Error   13  error C2227: left of '->GetRasterCount' must point to class/struct/union/generic type
    Error   10  error C2227: left of '->GetRasterXSize' must point to  class/struct/union/generic type
    Error   11  error C2227: left of '->GetRasterYSize' must point to class/struct/union/generic type
    Warning 2   warning C4627: '#include "cpl_conv.h"': skipped when looking for precompiled header use 
    Warning 1   warning C4627: '#include "gdal_priv.h"': skipped when looking for precompiled header use    

    #include "gdal_priv.h"
    #include "cpl_conv.h" // for CPLMalloc()
    #include "stdafx.h"
    #include "gdal.h"
    #include <stdlib.h>
    #include "iostream"

    using namespace std;
    class GDALMajorObject;
    class GDALDataset;
    class GDALRasterBand;
    class GDALDriver;
    class GDALRasterAttributeTable;
    class GDALProxyDataset;
    class GDALProxyRasterBand;
    class GDALAsyncReader;

    int main(int argc, char** argv)
    {
         GDALAllRegister(); 

        class GDALDataset *poDataset;
        char* Raster = new char[256];
        double  adfGeoTransform[6];

        Raster = "C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map 
     Data\\Raster\\4257.tiff";

       poDataset = (GDALDataset *) GDALOpen(Raster, GA_ReadOnly )CPL_WARN_UNUSED_RESULT;
    if( poDataset == NULL )
    {
        cout<<"Damn it"<<endl;
    }         

    printf( "Driver: %s/%s\n",
        poDataset->GetDriver()->GetDescription(),
        poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );

    printf( "Size is %dx%dx%d\n",
        poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
        poDataset->GetRasterCount() );

    if( poDataset->GetProjectionRef()  != NULL )
    printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );

    if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
    {
        printf( "Origin = (%.6f,%.6f)\n",
            adfGeoTransform[0], adfGeoTransform[3] );
        printf( "Pixel Size = (%.6f,%.6f)\n",
            adfGeoTransform[1], adfGeoTransform[5] );
    }


    // make it 256 bytes for a string length of 255 plus null (\0) terminator



    // your code
    delete[] Raster;// release the memory VERY IMPORTANT

}
2

Your problem is you've declared the variable as a pointer but haven't pointed it to anything. You have choices here, either malloc or new:

using malloc (#include <malloc.h>)

// make it 256 bytes for a string length of 255 plus null (\0) terminator
// malloc returns a void pointer so convert to char pointer by direct cast
char* pszFilename = (char*) malloc(256);
pszFilename = "c:\\some\\path\\raster.ext";

// your code
free(pszFilename); // release the memory VERY IMPORTANT

using new (builtin, no include)

// make it 256 bytes for a string length of 255 plus null (\0) terminator
char* pszFilename = new char[256];
pszFilename = "c:\\some\\path\\raster.ext";

// your code
delete[] pszFilename;// release the memory VERY IMPORTANT

however, you're declaring as a const char* which is different, have a read of https://www.geeksforgeeks.org/difference-const-char-p-char-const-p-const-char-const-p/ about the const keyword with char and char*.

Either way, you're declaring something but not giving it a value so the function GDALOpen(pszFilename reads like GDALOpen(NULL at run time. If you were using C# your compiler would give you an error of using a variable before assinment but C++ assumes you know what you're doing and doesn't check.

Here is some working code from one of my programs:

char* Raster   =  new char[FullPathMax];// FullPathMax is a const int
wcstombs(Raster,argv[2],FullPathMax); // convert Unicode to char, Windows!
GDALAllRegister(); // get ready to start using GDAL
GDALDataset* SourceRasterDS = (GDALDataset*) GDALOpenShared(Raster,GA_ReadOnly);
double GeoTransform[6];
SourceRasterDS->GetGeoTransform(GeoTransform);
GDALRasterBand* XBnd = SourceRasterDS->GetRasterBand(1);
// do some stuff
// close the dataset which will also close the bands
GDALClose((GDALDatasetH)SourceRasterDS); 
delete[] Raster; // release the memory, I cannot stress enough how VERY IMPORTANT this is!

There is an excellent tutorial https://www.gdal.org/gdal_tutorial.html in C++, ANSI C and python that covers the basics... I thoroughly recommend you read it. That is where I started from and it helped get kick started initially.

Another hint is to include gdal_i.lib as an additional dependency (Configuration Properties::Linker::Input) and ensure you include the include folder and lib folder in your Configuration Properties::VC++ Directories otherwise you will get build/link errors that are so frustrating.

Edit

Go to PROJECT:: properties and find these properties (modified values are bold): Include and library folders

Additional dependency

The additional dependencies in Linker::input is most likely to be the cause of your LNK2019 error. This will need to be set for both the debug and the release configuration.

  • The tutorial from gdal.org/gdal_tutorial.html , i need to make my own initialization and declaration right? I quite confuse when i look at their tutorial. – Mohamad Fathin Nov 2 '18 at 8:27
  • I got errorLNK2019, this is what u mean if i don't set the configuration properties correctly? – Mohamad Fathin Nov 2 '18 at 9:43
  • That sounds like you haven't included gdal_i.lib as an additional dependency, you should also set your GDAL paths (include your GDAL install in your PATH environment variable, you should also set your include and library directories to include your GDAL include and lib folders). Also make sure your compile target matches your GDAL install (64 bit GDAL for a 64 bit build target, 32 bit GDAL for a 32 bit build target). – Michael Stimson Nov 4 '18 at 22:56
  • Can you share to me instruction on how to set GDAL paths?. I still get the LNK2019 error – Mohamad Fathin Nov 7 '18 at 2:46
  • @MohamadFathin, see edit. You may also need to set GDAL system variables GDAL_DATA=C:\your\path\GDAL\gdal-data, GDAL_DRIVER_PATH=C:\your\path\GDAL\gdalplugins, GEOTIFF_CSV=C:\your\path\GDAL\gdal-data, PROJ_LIB=C:\your\path\GDAL\projlib to help the compiled tool find the required libs at runtime. – Michael Stimson Nov 7 '18 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.