Search code examples
c++eclipsenetbeanscodeblocksvisa

NI-VISA library programming in C++ - out of scope error


I am currently trying to program a Rigol DS1054 Oscilloscope to output waveform data. To create this program, I am writing in C++ using Code::Blocks, but I have tried this on four other compilers (Netbeans, Eclipse Mars, Eclipse Juno, Visual Basic 2012) but each one continuously results in an error. Here is what I have done so far:

  • Confirmed that C++ was not the problem by building and running a Hello World program
  • Linked the header file following this path (C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Include)
  • Linked the library using this path (C:\Program Files (x86)\IVI Foundation\VISA\WinNT\lib\msc\visa32.lib)
  • Applied the library for each project

I have also tried using the 64-bit version as well an I get the same error. This is my current code:

#include <iostream>
#include <visa.h>
using namespace std; 

int main () {

   ViSession rmSession;
   ViOpenDefaultRM(&rmSession);

   return 0;
}

The code is very simple, but when run, returns this error:

error: 'ViOpenDefaultRm' was not declared in this scope

This is really weird since it is clearly in the scope. This has been giving me trouble for so many days now - any help would be very appreciated. Thank you!


Solution

  • The function is called viOpenDefaultRM, not ViOpenDefaultRM.

    For the avoidance of your confusion by such errors in future, the ViOpenDefaultRM(&rmSession) in your code is not a declaration of a function, it is an invocation or call of a function (or would be, if such a function existed).

    In C++, the compiler must see a declaration of a function before it will permit invocations of that function, so that it can tell whether the invocation conforms to the function's signature (or to one of the signatures of an overloaded function), and the declaration must be still in scope at the point of invocation.

    The declaration of viOpenDefaultRM is:

    ViStatus _VI_FUNC  viOpenDefaultRM (ViPSession vi);
    

    You will find it in visa.h, and it is in scope at the point where you have attempted to call ViOpenDefaultRM because, by including visa.h before you define main, it is declared in the scope enclosing main, i.e. in global scope.