Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with class instatiation

Status
Not open for further replies.

sleze

Programmer
Jun 15, 2005
7
0
0
US
I am new to Visual c++ (having used c, java and VB, I thought the transition would be easy) and keep running into annoying errors. Here is the latest one.

I have created a class, FILESCHEMA and created an object in the main function

FILESCHEMA myfile(SEARCHPATH, true);

for use with testing. After manual testing, it appears it is working as planned so I tried to call it from another function. This time, however, it doesn't like its instantiation in that new function. When I put the line as is into the other function and compile, I get this error:

error C2146: syntax error : missing ';' before identifier 'myfile'

I have looked over the code and there does not appear to be any missing semicolons(also I commented out a few lines that are clearly syntactically correct so I know it isn't a rogue or missing ';') Then I tried using the new operator:

FILESCHEMA myfile = new FILESCHEMA (SEARCHPATH, true);

and got 103 errors(90 more than before) so that is going in the wrong direction. Anyone have any ideas?
 
Are you correctly including the header that declares FILESCHEMA in the source file that has the function that tries to use it? What about SEARCHPATH? Is that propertly declared?
 
I have this declared at the top of the actual .cpp file

class FILESCHEMA(filename currentfile, bool dirstatus);

filename is a typedef string that I created
 
Why do you have that there? I'm not sure what you are trying to do but it makes no sense. Assuming the FILESCHEMA class is defined in a header file somewhere, then you need to include that header file. Remove that line (or tell us why you have it there and we'll help change it to what it should be).
 
Now I am going crazy...I moved the class out into its own .cpp and created a definition in a header file(like good programmers should) and it is still giving me the error.

The reason I am going crazy is that now it is doing the same thing in main().

After commenting a TON of stuff out, this is what I am left with in the main class file.

#include <iostream.h>
#include <fstream>
#include <stdio.h>
#include <windows.h>
//#include <dirent.h>
#include "directory.h"
#include "FILESCHEMA.h"
#include <string.h>
using namespace std;

#define _WIN32_WINNT 0x0501


int main ()
{
FILESCHEMA myfile (SEARCHPATH, true);
return 0;
}

which results in these 3 errors:
error C2146: syntax error : missing ';' before identifier 'myfile'
warning C4551: function call missing argument list
error C2065: 'myfile' : undeclared identifier

...all about the same line. I went a step further and commented out the VAST majority of FILESCHEMA and I still get the same error.

Any ideas?
 
What does FILESCHEMA.h look like (with all the commented out stuff removed)?
 
FILECHEMA.h is just a standard .h file created automatically by VC++ compiler

the one that is doing all the work is directory.h:

#define SEARCHPATH "c:\\archives\\test"
#define ALLFILES "\\*"
#define ALLFILESNUM 3
#define BATFILES "\\*.bat"
#define BATFILESNUM 6
#define ALLDIR "\\*." //this is a lie. This is actually all files with no extension
#define ALLDIRNUM 4


#define NAMELENGTH 32
#define MAXNUMOFFILES 32

typedef char filename [NAMELENGTH]; //name of a file
class FILESCHEMA(filename currentfile, bool dirstatus);
 
Again, what is this supposed to be?
Code:
class FILESCHEMA(filename currentfile, bool dirstatus);
It doesn't define a class called FILESCHEMA, which you said you created. It is just illegal syntax. To define a class, you would do something like this:
Code:
class FILESCHEMA
{
public:
  FILESCHEMA(filename currentfile, bool dirstatus);

  // Other public methods here.

private:
  // All the member variables here.
};
and then put the implementations in the cpp file. You should probably go to your C++ book or whatever reference you have on C++ to learn about classes if you aren't sure about how to define one. There is a lot more to talk about than can really be discussed on a forum like this.
 
In Java, that is called prototyping. I just assumed that you do the same thing in C++. Here is the fully definition:

class FILESCHEMA
{
filename thisfile;
bool is_dir;

public:
struct subdir
{
FILESCHEMA * subfiles [MAXNUMOFFILES]; //array containing subdirectory filenames
bool is_used [MAXNUMOFFILES];
bool is_dir [MAXNUMOFFILES]; //if not then it is a .bat file
}thissubdir;

FILESCHEMA (filename currentfile, bool dirstatus)
{
int index;
strcpy (thisfile, currentfile);
is_dir = dirstatus;
//now set all subfiles to NULL
for(index=0;index<MAXNUMOFFILES;index++)
{
// strcpy(thissubdir.subfiles[index]->thisfile,"");
thissubdir.is_dir[index] = false;
thissubdir.is_used[index] = false;
}
}

//adds a file and returns whether or not it was succesfully added
bool add_sub(filename newsubfile, bool dirstatus)
{
int index;
bool added = false;
for(index=0;index<MAXNUMOFFILES;index++)
if (!thissubdir.is_used[index])
{
thissubdir.subfiles[index] = new FILESCHEMA(newsubfile,dirstatus);
thissubdir.is_used[index] = true;
added = true;
}
return added;
}

void getfilename(filename returnstring)
//you can't return an array so you need to modify a string
//that is outside this class
{
strcpy(returnstring, thisfile);
}

bool dir_test()
{
return is_dir;
}

};
 
Ok, so as long as that definition is in the header file, you are fine.

What you were thinking of as a prototype is not that in C++. You can prototype functions by giving similar syntax as you gave, and you can forward declare a class. You can also separate the class definition from the implementation of the class methods. You would put this in the header file:
Code:
class FILESCHEMA
{                  
    filename thisfile;
    bool is_dir;

  public:
    struct subdir
    {
     FILESCHEMA * subfiles [MAXNUMOFFILES]; //array containing subdirectory
                                            // filenames
     bool is_used [MAXNUMOFFILES];
     bool is_dir [MAXNUMOFFILES];  //if not then it is a .bat file
    }thissubdir;

    FILESCHEMA (filename currentfile, bool dirstatus);
    bool add_sub(filename newsubfile, bool dirstatus);
    void getfilename(filename returnstring);
    bool dir_test();
};
And in the cpp file you would implement the methods, for example"
Code:
#include "FILESCHEMA.h"

// ...

//you can't return an array so you need to modify a string
//that is outside this class
void FILESCHEMA::getfilename(filename returnstring)
{
    strcpy(returnstring, thisfile);
}

bool FILESCHEMA::dir_test()
{
    return is_dir;
}
Notice the "FILESCHEMA::" before the method name to indicate you are implementing a method of the FILESCHEMA class.

If you do it that way, you can just #include "FILESCHEMA.h" in any file that needs to use the FILESCHEMA class.
 
Not about classes (it's uolj's area;)...

Don't use old headers like <iostream.h>. All standard C++ headers have simple names w/o .h (<iostream>, for example). All C library headers look like <cstdio>, <cstring> (<string> for C++ library std::string class) etc. Never mix old and standard headers!
 
You guys are not gonna believe this.

In frustration, I tried creating a new class that did nothing and boom it works. I then named that class FILESCHEMA and boom, it broke. I renamed the class I was having trouble with to myfilescema...boom it works.

So apparently there is something wrong with my Visual C++ 6.0 because it does NOT like uppercase classes.

Thanx for you help.
 
You're right. I don't believe it. [bugeyed]

>So apparently there is something wrong with my Visual C++ 6.0

I actually doubt it.

>because it does NOT like uppercase classes.

Perhaps it just has a good sense of naming style. ;-)

/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top