For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Basic Classes question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Basic Classes question
fima

2005-11-26, 7:57 am

hi,
im using c++...

if i have a project with a main cpp file and another secondery cpp and they
both have header files but my problem is how can i connect them both to a
new header file ive created that containes a class ive created and should be
used by both main and secondery files...

ive tried many options but all of them returned the following resault...
"
MainWnd.obj : error LNK2005: "class LFile PFile" (?PFile@@3VLFile@@A)
already defined in ProtLab.obj

"


David Wilkinson

2005-11-26, 7:57 am

fima wrote:

> hi,
> im using c++...
>
> if i have a project with a main cpp file and another secondery cpp and they
> both have header files but my problem is how can i connect them both to a
> new header file ive created that containes a class ive created and should be
> used by both main and secondery files...
>
> ive tried many options but all of them returned the following resault...
> "
> MainWnd.obj : error LNK2005: "class LFile PFile" (?PFile@@3VLFile@@A)
> already defined in ProtLab.obj
>
> "
>
>


fima:

This does not seem like a typical problem with including header files.
Try the following:

1. Rebuild the entire project.

2. Read the documentation on LNK2005 and check your project settings.

HTH,

David Wilkinson
Scott McPhillips [MVP]

2005-11-26, 7:57 am

fima wrote:
> hi,
> im using c++...
>
> if i have a project with a main cpp file and another secondery cpp and they
> both have header files but my problem is how can i connect them both to a
> new header file ive created that containes a class ive created and should be
> used by both main and secondery files...
>
> ive tried many options but all of them returned the following resault...
> "
> MainWnd.obj : error LNK2005: "class LFile PFile" (?PFile@@3VLFile@@A)
> already defined in ProtLab.obj
>
> "
>
>


Your new header file must declare the class (LFile) but not define an
object (PFile). Make a new cpp file where the object is defined and add
it to the project. The cpp file will only be compiled once, so there
will only be one copy of the object. The h file will be compiled
multiple times, so it must not allocate any memory objects.

--
Scott McPhillips [VC++ MVP]

fima

2005-11-26, 7:01 pm

hi,
but when i try to use...
the class is named LFile
and it has one function currently for checking called mess...
so to call i write LFile.mess()

but all i get is..

i:\ProtLab\Prot Lab3\Prot Lab\ProtLab.cpp(19) : error C2143: syntax error :
missing ';' before '.'


Scott McPhillips [MVP]

2005-11-26, 7:01 pm

fima wrote:
> hi,
> but when i try to use...
> the class is named LFile
> and it has one function currently for checking called mess...
> so to call i write LFile.mess()
>
> but all i get is..
>
> i:\ProtLab\Prot Lab3\Prot Lab\ProtLab.cpp(19) : error C2143: syntax error :
> missing ';' before '.'
>
>


It seems you are having trouble with the difference between a class and
an object. If LFile is a class then it is a mistake to use
LFile.mess(). A class (LFile) is like a blueprint for something that
has not yet been built. In your previous post you also had an object
PFile, of class LFile, but you defined the object in the wrong place.
An object is constructed from the class blueprint.

First construct an object in a cpp file:
#include "LFile.h"
....
LFile PFile;

Then you can call the object's methods:
PFile.mess();

--
Scott McPhillips [VC++ MVP]

fima

2005-11-26, 7:01 pm

Also, when i declere the object as the class
(LFile PFile)
if i put inside the cpp file it isnt reachable and i get the "unrecgonized
identifier" message and when i put in the header i get the lnk2005
message...
my target is to declere as sort of globel veriable(if speaking in visual
basic mode =) ) so that every function int he program can access it and use
the SAME function and veriables (IF function a changes VERiable A the
function b that uses the veriable a in the class will be have the changed
a )
is there any way to do it?
thanks.

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:Od6DA2o8FHA.632@TK2MSFTNGP10.phx.gbl...
> fima wrote:
>
> Your new header file must declare the class (LFile) but not define an
> object (PFile). Make a new cpp file where the object is defined and add
> it to the project. The cpp file will only be compiled once, so there will
> only be one copy of the object. The h file will be compiled multiple
> times, so it must not allocate any memory objects.
>
> --
> Scott McPhillips [VC++ MVP]
>



fima

2005-11-26, 7:01 pm

but if i put the LFile PFile in one cpp file then other cpp files caannot
access the same object(pfile) ive created or can i?

thanks.
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:esMS5vp8FHA.1000@tk2msftngp13.phx.gbl...
> fima wrote:
>
> It seems you are having trouble with the difference between a class and an
> object. If LFile is a class then it is a mistake to use LFile.mess(). A
> class (LFile) is like a blueprint for something that has not yet been
> built. In your previous post you also had an object PFile, of class
> LFile, but you defined the object in the wrong place. An object is
> constructed from the class blueprint.
>
> First construct an object in a cpp file:
> #include "LFile.h"
> ...
> LFile PFile;
>
> Then you can call the object's methods:
> PFile.mess();
>
> --
> Scott McPhillips [VC++ MVP]
>



David Webber

2005-11-26, 7:01 pm


"fima" <fkpkot@gmail.com> wrote in message
news:%23GpB56p8FHA.2616@TK2MSFTNGP15.phx.gbl...

> but if i put the LFile PFile in one cpp file then other cpp files
> caannot access the same object(pfile) ive created or can i?


If it is a global variable then they can access it with an "extern"
declaration.

Whether the *should* do is another question entirely - to which the
answer is almost always "no".

It is most often better to declare it within a function and pass it
to others by means of a reference in the argument list.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


fima

2005-11-26, 7:01 pm

i thought about using a reference but i wasnt sure
thanks.
"David Webber" <dave@musical.demon.co.uk> wrote in message
news:uEQYDXq8FHA.3380@tk2msftngp13.phx.gbl...
>
> "fima" <fkpkot@gmail.com> wrote in message
> news:%23GpB56p8FHA.2616@TK2MSFTNGP15.phx.gbl...
>
>
> If it is a global variable then they can access it with an "extern"
> declaration.
>
> Whether the *should* do is another question entirely - to which the answer
> is almost always "no".
>
> It is most often better to declare it within a function and pass it to
> others by means of a reference in the argument list.
>
> Dave
> --
> David Webber
> Author MOZART the music processor for Windows - http://www.mozart.co.uk
> For discussion/support see http://www.mozart.co.uk/mzusers/mailinglist.htm
>
>



Scott McPhillips [MVP]

2005-11-26, 7:01 pm

fima wrote:

> Also, when i declere the object as the class
> (LFile PFile)
> if i put inside the cpp file it isnt reachable and i get the "unrecgonized
> identifier" message and when i put in the header i get the lnk2005
> message...
> my target is to declere as sort of globel veriable(if speaking in visual
> basic mode =) ) so that every function int he program can access it and use
> the SAME function and veriables (IF function a changes VERiable A the
> function b that uses the veriable a in the class will be have the changed
> a )
> is there any way to do it?
> thanks.


// LFile.h
class LFile
{ ...
};
extern LFile PFile; // describes PFile so other modules can access it.

// LFile.cpp
#include "stdafx.h"
#include "LFile.h"
LFile PFile; // creates global PFile
....LFile functions

--
Scott McPhillips [VC++ MVP]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com