Classes 
Author Message
 Classes

Hi there.

I have a question on C++ classes. I have created
a class in one file and a function in another
file. For some reason, my function cannot access
this class. Is there a way to define a class
as "external" or somehow make it available to
functions in other files?

Josh

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Thu, 27 Jun 2002 03:00:00 GMT  
 Classes
a) You need to put the class definition in a header file and then #include
that header in the file with the function in it.

b) You need to post code illustrating your problem.

NeilB

Quote:

> Hi there.

> I have a question on C++ classes. I have created
> a class in one file and a function in another
> file. For some reason, my function cannot access
> this class. Is there a way to define a class
> as "external" or somehow make it available to
> functions in other files?

> Josh

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Thu, 27 Jun 2002 03:00:00 GMT  
 Classes
That depends on a couple of things.  Unfortunately you didn't give quite
enough details to be sure which situation you're really asking.

Does your function really need to access the class and not an object of that
class?

Does your function need to operate on *any* object of your class?

Does your function only operate on a single instance of your class?

Does your function operate on static class data (no objects?)

Does your function need to break encapsulation and see the private members
of the objects of your class?

The answer depends.  It could be to declare the function a friend.  It could
mean to simply #include the class header in the file that defines the
function.  It could mean you need to change the function to take as a
parameter a pointer to an instance of your class.  It possibly (though I
wouldn't encourage it) be solved with a global object, which you would
declare as extern.

If you can be more specific, so can I.  Good luck.

Chris

Quote:

>Hi there.

>I have a question on C++ classes. I have created
>a class in one file and a function in another
>file. For some reason, my function cannot access
>this class. Is there a way to define a class
>as "external" or somehow make it available to
>functions in other files?

>Josh

>Sent via Deja.com http://www.deja.com/
>Before you buy.



Thu, 27 Jun 2002 03:00:00 GMT  
 Classes

Quote:

>Hi!

>I apologize for not being as specific, as it turned out all I needed to
>do was to add an #include to my file......

Glad you got it to work. :)

Quote:
>Anyway, that's taken care of. Thanks for your help! However I do seem
>to be having one more problem indirectly related to this....

>I have a class implamentation file(CMainFrame.cpp) that implements the
>class CMainFrame(that ships with the framework). I have an object in

class CEMDoc : public ...
{
  int var;

Quote:
}

-- elsewhere --
CEMDoc::var = 2;

The error is correct, you cannot assign a value to a non-staic member by
qualifying the symbol with the classname.   Since var is not a static
member, you can only assign it in an *object*, not directly on the class.
Does it make sense to have one instance of var shared between every object
of type CEMDoc?  If so, make it static.  If not, you have to change your
code that assigns the value to receive an object and make the assignment on
it.  (I avoid MFC and have no idea if CEMDoc would ever have multiple
instantiations, etc.)

Chris



Thu, 27 Jun 2002 03:00:00 GMT  
 Classes
Hi!

I apologize for not being as specific, as it turned out all I needed to
do was to add an #include to my file......

Anyway, that's taken care of. Thanks for your help! However I do seem
to be having one more problem indirectly related to this....

I have a class implamentation file(CMainFrame.cpp) that implements the
class CMainFrame(that ships with the framework). I have an object in
the CEMDoc class(that ships with the framework - inherits CDocument) in
CEMDoc.cpp. Anyway, I wanted to be able to create a variable which will
be written to file, so I created this variable in CEMDoc(so I could
Serialize it). However what I wanted to do was to change this variable
in CMainFrame. So this is what I did, but it doesn't work:

CEMDoc.h

class CEMDoc : public ...
int var;

CMainFrame.cpp

#include CEMDoc.h

CEMDoc::var = 2;

but this doesnt work and the compiler reports an error of:

error C2597: illegal reference to data member 'CEMDoc::var' in a static
member function

Any help would be GREATLY appriciated!!!!!

Josh

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 28 Jun 2002 03:00:00 GMT  
 Classes
Hey this is me, just on a different news program...

You are right, I needed to create an object and work with it as an object.
Thanks!

Josh


Quote:


> >Hi!

> >I apologize for not being as specific, as it turned out all I needed to
> >do was to add an #include to my file......

> Glad you got it to work. :)

> >Anyway, that's taken care of. Thanks for your help! However I do seem
> >to be having one more problem indirectly related to this....

> >I have a class implamentation file(CMainFrame.cpp) that implements the
> >class CMainFrame(that ships with the framework). I have an object in

> class CEMDoc : public ...
> {
>   int var;
> }

> -- elsewhere --
> CEMDoc::var = 2;

> The error is correct, you cannot assign a value to a non-staic member by
> qualifying the symbol with the classname.   Since var is not a static
> member, you can only assign it in an *object*, not directly on the class.
> Does it make sense to have one instance of var shared between every object
> of type CEMDoc?  If so, make it static.  If not, you have to change your
> code that assigns the value to receive an object and make the assignment
on
> it.  (I avoid MFC and have no idea if CEMDoc would ever have multiple
> instantiations, etc.)

> Chris



Fri, 28 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Class in class OR class derived from class?

2. class class-name: base-class : interface

3. Web Services: Proxy class make own class definition for user class

4. Class Wizard won't let me derive a class from my class

5. Trying to add a class with no base class in Class Wizard

6. class design: Class.ToString() ro Class.Value?

7. looking for class structure differences between smalltalk class and c# class

8. Reference problem when using VB classes with C# classes

9. VC++ class inheriting C# class loses base functions

10. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

11. TreeNode : Cast base class into sub class

12. V6 Class View sometimes has classes missing

 

 
Powered by phpBB® Forum Software