# enumerated types in classes = trouble?

## Recommended Posts

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
{
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)

##### Share on other sites
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

##### Share on other sites
Like all things that can be defined within a class....scope applies. In the context above you need to use a fully qualified name:

##### Share on other sites
This

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

should be

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

##### Share on other sites
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
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

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

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by OpenGL_Guruok 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.cppLinking...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 externalsError 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

##### Share on other sites
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?

##### Share on other sites
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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628303
• Total Posts
2981923

• 10
• 11
• 11
• 10
• 10