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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with "include" / linker error LNK2005

Status
Not open for further replies.

agadir

Programmer
Oct 29, 2001
18
DE
Hi,
actually i'm writing a program that's storing data in a XML-File.
Therefor i've written a library that reads and stores data in XML.
It looks like these:


#include <afx.h>
#include <afxtempl.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>

#ifndef myXML_cpp
#define myXML_cpp

/* Functionprototypes */
class TAG;

int readXMLFile (ifstream File, TAG &Tag);
int readTag (ifstream File, TAG &NewTag, char already_read);
int CheckClosingTag (ifstream File, CString closeTag);
int writeXMLFile (ofstream File, TAG &Tag);
int writeTag (ofstream File, TAG &Tag);

class TAG
{
private:
CString s_TagName;
CString s_TagContent;
public:
TAG (void);
TAG (TAG const &NewTag);
TAG (CString TagName);
~TAG(void);
CString GetTagContent (void) const;
CString GetTagName (void) const;
void SetTagName (CString TagName);
void SetTagContent (CString TagContent);
void Reset (void);
void operator = (TAG const &source);
CArray <TAG, TAG> SubTags;
};

// class memberfunctions and functionbodies

#endif

But if i include this file (via #include &quot;myXML.cpp&quot;) in another file with main function, the linker return for each memberfunction and function the errorcode &quot;LNK2005: <functionname> is already defined in myXML.obj.&quot;, although the functions are only defined in this file.
I've spend a lot of time but i could not find the failure so i hope someone can help me.
Thanks!
 
A function's definition is its implementation. In a .H file, what we typically find is its declaration (the prototypes).

You probably include myXML.obj in your project (as you should), and also the .c/.cpp files with the actual code for your functions via #include &quot;myXML.cpp&quot;.

#include are for .H files (function declarations) only. Do not #include a .cpp file!

Vincent
 
Hi, I've spotted an error in your assignment operator as well, it's simply not going to work! It needs to return a Tag* not a void.

Just thought I'd mention it in the passing. :)
William
Software Engineer
ICQ No. 56047340
 
Thanks for your answers!
@VincentP:
The prototypes have to be insert in a .h-File (e.g. myXML.h).
But where do I have to place the functionbodies (myXML.cpp)?

@williamu
The operator works fine. A return value is not needed because it's a binary operator.
 
Yeah fair comment about the assignment operator but don't you think that :

TAG a, b ;
a = b ;

is more intuitive than

TAG a, b ;
a.operator = (b) ;

Hey it's my opionin what do you care. :)

William
Software Engineer
ICQ No. 56047340
 
Hi again,
o.k., it's your opinion. But writing a.operator=(b) is equivalent to writing a = b, isn't it ;-).
Back to my posted problem: Now I've put the prototypes and class definition in a .h-file (myXML.h) and the function- and
memberfunctionbodies in a .cpp-file (myXML.cpp), but I'm still getting the same error-message. Does anyone know why?
 
From the looks of your error message, I would say that the compiler generates a .obj file from your .cpp file, and you also have a .lib file from another project you want to use.

quoting from you:
&quot;actually i'm writing a program that's storing data in a XML-File.
Therefor i've written a library that reads and stores data in XML.&quot;

If this is what you really want to do, you should not include the .cpp file in your project environment (go to the FileView panel, then Source Files and remove myXML.cpp).

The idea behind building a library: you code some functions in some .cpp files, and you want others to be able to use them without having to know all the details about how the functions work. So you send them a .lib file (containing all the compiled &quot;bodies&quot;) along with the appropriate .h files so that they (programmer) and their compilers know how to call these functions. Hence this other project should know only about the .h and .lib files.

I hope that nails it.

Vincent
 
Me again!

Just to point out in your case the assignment of a = b will not work of you try to:

TAG a, b ;
a = b ;

In this instance your operator must return a TAG*. If you have any doubts try it.

:)



William
Software Engineer
ICQ No. 56047340
 
That's it! I forgot to remove &quot;myXML.cpp&quot; from the project environment. Now it works! Thanks a lot for your aid!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top