enumerated types in classes = trouble?

Started by
7 comments, last by SirLuthor 18 years, 9 months ago
here is the class i am trying to call from, followed by my call from main and then the error message that i am getting.. is there something i should do? i dont see it... ive never had to deal with enumerated types before in all my years of C++, never really needed to use it.. anyway i appreciate your time! thanks! class

class NcFile
{
  public:

    virtual ~NcFile( void );

    enum FileMode 
	  {
	   ReadOnly,	// file exists, open read-only
	   Write,		// file exists, open for writing
           Replace,	// create new file, even if already exists
	   New		// create new file, fail if already exists
          };

    NcFile( const char * path, FileMode = ReadOnly);// ,
	   // size_t *chunksizeptr = NULL, // optional tuning parameters
	    //size_t initialsize = 0 );

    NcBool is_valid( void ) const;         // opened OK in ctr, still valid
......
} 
from main.cpp

//netcdf reader for use with the GDEM Software...
#include <iostream>
#include <sstream>
#include "netcdfcpp.h"

using namespace std;

int main()
{

 NcFile *a = new NcFile("sspgdemv3s01.nc", NcFile::FileMode = ReadOnly);
 
 return 0;
}
error(s): --------------------Configuration: netcdf_test - Win32 Debug-------------------- Compiling... main.cpp C:\Program Files\Microsoft Visual Studio\VC98\MyProjects\netcdf_test\main.cpp(11) : error C2065: 'ReadOnly' : undeclared identifier C:\Program Files\Microsoft Visual Studio\VC98\MyProjects\netcdf_test\main.cpp(11) : error C2275: 'NcFile::FileMode' : illegal use of this type as an expression Error executing cl.exe. netcdf_test.exe - 2 error(s), 0 warning(s)
heh
Advertisement
Hi.

Replace the line
NcFile *a = new NcFile("sspgdemv3s01.nc", NcFile::FileMode = ReadOnly);
by this:
NcFile *a = new NcFile("sspgdemv3s01.nc", NcFile::ReadOnly);
This should do the job.

Ghyslain Abel
Like all things that can be defined within a class....scope applies. In the context above you need to use a fully qualified name:


NcFile::FileMode mode = NcFile::ReadOnly;

This

NcFile *a = new NcFile("sspgdemv3s01.nc", NcFile::FileMode = ReadOnly);


should be

NcFile *a = new NcFile("sspgdemv3s01.nc", NcFile::ReadOnly);
ok i changed the code to what you said, which i have tried before...and this is what i get back from the compiler... WTF??? i am using Visual C++ 6. just if you are wondering... thanks again...

--------------------Configuration: netcdf_test - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall NcFile::NcFile(char const *,enum NcFile::FileMode)" (??0NcFile@@QAE@PBDW4FileMode@0@@Z)
Debug/netcdf_test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

netcdf_test.exe - 2 error(s), 0 warning(s)
heh
Hi again.

You must define the constructor:

NcFile::NcFile( const char * path, FileMode ReadOnly )
{
//...
}

The error message said that this function was declared but not defined.

Ghyslain Abel
Quote:Original post by OpenGL_Guru
ok i changed the code to what you said, which i have tried before...and this is what i get back from the compiler... WTF??? i am using Visual C++ 6. just if you are wondering... thanks again...

--------------------Configuration: netcdf_test - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall NcFile::NcFile(char const *,enum NcFile::FileMode)" (??0NcFile@@QAE@PBDW4FileMode@0@@Z)
Debug/netcdf_test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

netcdf_test.exe - 2 error(s), 0 warning(s)

Your linker can't find the NcFile constructor. Have you written a body for it yet? If so, is it included in your project? Perhaps it isn't properly named:
//in your source fileNcFile(char const*, NcFile::FileMode) {...}//rather thanNcFile::NcFile(char const*, NcFile::FileMode) {...}

CM
well the thing is is i didnt write this code.. i am working on reading in binary data and such through Netcdf format. it was originally a C version but now there is a C++ now and it only comes with a header file that references some old files from the C version. its a lot to read but it might explain better what is happening.

in essence all i have is a .h file but not the implementation of it, which i think is indirectly calling the C style functions from the C version instead of making the user do all the un-necessary calls.


can anyone help?
heh
You need the function definitions, either in the form of the source code, or as a library to link with your project, or else that stuff is not going to compile.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!

This topic is closed to new replies.

Advertisement