Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

error which is telling me i have defined something twice SOS

RIGHT_THENRIGHT_THEN Member Posts: 154
what to do with the error which is telling me i have defined something twice.


UnderstandingStructures.obj : warning LNK4006: "int __cdecl UnderstandingStructuresCPPmain1(int)" ([email protected]@[email protected]) already defined in onceagain1.obj; second definition ignored

this above stated error is my problem i am getting lot of them for various other functios too
i am practicing vc++ by making a project.i have started a workspace which is for
working in the console mode.and except for the "onceagain.cpp " file all other files i have
opened using the "New CLass" utility given in the menu bar of the vc++6 .so every New Class
created automatically brings two files one is with extention .h and other with the extention
.cpp but both have same names.with the idea that all the classes and its data will be defined
in .h file and there defination of the functions constructor and destructor would be done
in .cpp file with the same name as of.h that is what vc++6 does when we ask it to add a new class
this is the background of the project i am trying to work on.
now below i have given some files and lot of things i have deleted from them just to show
how and where are the functions and classes defined and declared.so please dont try to
understand the programm below. it is given just for refrence so that you
could see that i am telling the truth when i say i have not used any function name twice
or any other name for the purpose of declaring and defining .that thing has been done only once.
so please bear with me and have a look to see if including of some file is giving this problem
that is some header like
#include "onceagainClass.h" //cpp"
#include "PointerCheck.h" //cpp"
#include
#include "UnderstandingStructures.cpp"
or is there something else i should do with the compiler.
please dont try to understand what is going on in the programm
because it is working fine.you will get frustrated and irritated at understanding it
so please only have a look what include file is giving the problem and which function
name can appear twice when building the project.
once in "onceagain.obj" and other times in the file where it is declared hence resulting
in ambiguity.
i have even checked the dependency of the onceagain.cpp and "UnderstandingStructures.cpp"
appears twice in it but how.when i have included it only once is that the problem.if so how to
rectify it.89 billion thanks in advance and 99 billion after you show me the way.thanks

all thou there are many files i will put my problem like this
because solution to this would rectify the rest too.i believe


workspace name:-onceagain
projectname:-onceagain
main file from which the programm starts running:-onceagain.cpp

***********************************************************************
onceagain.cpp

#include "onceagainClass.h"//cpp"
#include "PointerCheck.h"//cpp"
#include
#include "UnderstandingStructures.cpp"

/*this is the main file;
when building the programm
i started getting the error that
"SOME OF THE THINGS I AM USING
ARE ALLREADY DEFINED IN ONCEAGAIN.OBJ"
say some function or structure;
i checked all around in my files
i did not get a duplicate defination anywhere
even the functions of the classes i defined and
declared inside the classes only.to be on the safe side
but still the problem persisted.
so i thought maybe i am using the same header files in other files
but even after that problem remained
i thought since my functions are having similar name with only
a numerical difference such as hey1();hey2();hey3();
might be compiler is doing it because of that;
solving it the problem still persists.
i saw the documentation.and checked FORCE option in the
properties box.to wave of these isssueless problems.
programm runs now but.it wont compile any changes in the things which it
finds allready defined in "onceagain.obj".for example:-
void test(int value)
{
cout<<value<<endl;
}
this would work properly when i will write it for the first time
suppose i want to make changes to it like
void test(int value)
{
value=value+10;
cout<<value<<endl;
}
it wont change the value of the value but carry on with the earlier wersion of it.
why is this happeing where is it finding these things duplicated .why is the obj
or the object file of my programm not getting updated.
and yes every file that i have added in my programm i have added it using the option
of NEW CLASS from the menu.so it builds one header".h" file and one ".cpp" file for it
and i am using those file whether or not to write classes .i am making functions
outside the classes and structures too.does that make any difference
everything works fine but only problem is the ambiguity.why is that happening*/

int x;
int *x1;
int *x2;
int x3 ;

PointerCheck *PointerCheck_Pointer_1;
PointerCheck PointerCheck_obj1;


void main()
{


x=UnderstandingStructuresCPPmain1(10); //
return;
}

/*************************************

onceagainclass.h

#include <stdio.h>
#include
#include
#include

*************************************/


/**********************************************
UnderstandingStructures.cpp


#include "UnderstandingStructures.h"


/*UnderstandingStructures::UnderstandingStructures()
{
i commented these constructor and destructor
because it too was giving the same problem
even when this file has been generated by
vc++ itself.i have declared them in the header file itself now
}

UnderstandingStructures::~UnderstandingStructures()
{

}*/

struct UnStruct1 UnStruct1_Obj1;
struct UnStruct1 *UnStruct1_Pointer1;
struct UnStruct1 &UnStruct1_Refrence1=UnStruct1_Obj1;
struct UnStruct2 UnStruct2_Obj1;
struct UnStruct2 *UnStruct2_Pointer1;
struct UnStruct2 &UnStruct2_Refrence1=UnStruct2_Obj1;
struct UnStruct3 UnStruct3_Obj1;
struct UnStruct3 *UnStruct3_Pointer1;
struct UnStruct3 &UnStruct3_Refrence1=UnStruct3_Obj1;

int UnderstandingStructuresCPPmain1(int value)//
{
char String1[]="aman";
cout<<"String1:-hello:-"<<String1<<endl;
return (0);
}
**********************************************/
/********************************************

UnderstandingStructures.h

#include <stdio.h>
#include
#include


#if !defined(AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_)
#define AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class UnderstandingStructures
{
public:
UnderstandingStructures()
{ }
virtual ~UnderstandingStructures()
{ }

};
#endif //!defined(AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_)

struct UnStruct1
{
private:

int intVARIABLEofUnStruct1;
char charVARIABLEofUnStruct1;
long longVARIABLEofUnStruct1;
int *intPOINTERofUnStruct1;
char *charPOINTERofUnStruct1;
long *longPOINTERofUnStruct1;

public:
UnStruct1Function1(int value1,char value2)
{
}

};

struct UnStruct2
{

int intVARIABLEofUnStruct2;
char charVARIABLEofUnStruct2;
long longVARIABLEofUnStruct2;
int *intPOINTERofUnStruct2;
char *charPOINTERofUnStruct2;
long *longPOINTERofUnStruct2;

public:
UnStruct2Function1(int value1,char value2)
{
}

};

struct UnStruct3
{

protected:
int intVARIABLEofUnStruct3;
char charVARIABLEofUnStruct3;
long longVARIABLEofUnStruct3;
int *intPOINTERofUnStruct3;
char *charPOINTERofUnStruct3;
long *longPOINTERofUnStruct3;

public:
UnStruct3Function1(int value1,char value2)
{
}

};

struct
{
int intVARIABLEofAynonomus;
int *intPOINTERofAynonomus;
};//just for checking if like unions anoynymous structures could be built

****************************************************
/*************************************
PointerCheck.h

#include
#include
#include
#include

#if !defined(AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_)
#define AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class PointerCheck
{
public:
PointerCheck()
{
cout<<"Inside The Constructor Of PointerCheck"<<" this "<<this<<endl;
}
virtual ~PointerCheck()
{
cout<<"Inside The Destructor Of PointerCheck"<<" this "<<this<<endl;
}
};
void (*PointerToFunction)();
int PointerCheckCPPFunction(int value,long value1);
int PointerCheckCPPFunction(int value,long value1)
{
cout<<"inside void PointerCheckCPPFunction(void)"<<" value="<<value<<endl;

int *intPointer_var1;
int *intPointer_var2[30];
int intPointer_var3;

cout<<"now printing the values from within PointerCheckCPPFunction"<<endl<<& intPointer_var1<<" intPointer_var1 "<<endl<<&intPointer_var2<<" intPointer_var2"<<endl;
return (value+10);
}

void justchecking(void)
{
PointerToFunction=(void(*)())(&PointerCheckCPPFunction);
cout<<" then and now "<<endl<<endl<<endl;
cout<<PointerToFunction<<" <--->"<<&PointerCheckCPPFunction<<endl<<" <--PointerToFunction inside PointerCheck.cpp"<<endl<<endl;
}
struct StructureInCPlusPlus1
{
int *intPointer1;
char *charPointer1;
int *intPoinerArray1[10];
long *longPointer1;
}StructureInCPlusPlus3;
struct StructureInCPlusPlus2
{
int *intPointer1;
char *charPointer1;
int *intPoinerArray1[10];
long *longPointer1;

};
int& ObjectItself(int& value) //int &ObjectIteself(int &value)
{ //can be written like this also
//which makes it more clear
//that these are refrencing the
//objects that is taking and sending
//using addresses.pointers could have
//been used for the same purpose too
int *sendvalue;

cout<<" inside ObjectItself sendvalue is="<<sendvalue<<endl;
sendvalue=&value;
cout<<"sendvalue holds";
cout<<" the addressof value which is sendvalue="<<sendvalue<<endl;
cout<<"and address of is &sendvalue:-"<<&sendvalue<<endl;
cout<<"letussee what is *(sendvalue):-"<<*(sendvalue)<<endl;
cout<<"letussee what is *(&sendvalue):-"<<*(&sendvalue)<<endl;
//cout<<"letussee *(33412):-"<<*(333412);
return *sendvalue;
cout<<"leaving ObjectItself"<<endl;
}

#endif
//!defined(AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_)


***********************************************************************************/





Comments

  • VilanyeVilanye Member Posts: 684
    #include "UnderstandingStructures.cpp"

    Don't #include .cpp files, only header files.
  • IDKIDK Member Posts: 1,784
    The code looks funny with all the angry faces, that's maybe why the
    compiler don't like the code.
    [code]
    : onceagain.cpp
    :
    : #include "onceagainClass.h"//cpp"
    : #include "PointerCheck.h"//cpp"
    : #include
    : #include "UnderstandingStructures.cpp"
    :
    : /*this is the main file;
    : when building the programm
    : i started getting the error that
    : "SOME OF THE THINGS I AM USING
    : ARE ALLREADY DEFINED IN ONCEAGAIN.OBJ"
    : say some function or structure;
    : i checked all around in my files
    : i did not get a duplicate defination anywhere
    : even the functions of the classes i defined and
    : declared inside the classes only.to be on the safe side
    : but still the problem persisted.
    : so i thought maybe i am using the same header files in other files
    : but even after that problem remained
    : i thought since my functions are having similar name with only
    : a numerical difference such as hey1();hey2();hey3();
    : might be compiler is doing it because of that;
    : solving it the problem still persists.
    : i saw the documentation.and checked FORCE option in the
    : properties box.to wave of these isssueless problems.
    : programm runs now but.it wont compile any changes in the things which it
    : finds allready defined in "onceagain.obj".for example:-
    : void test(int value)
    : {
    : cout<<value<<endl;
    : }
    : this would work properly when i will write it for the first time
    : suppose i want to make changes to it like
    : void test(int value)
    : {
    : value=value+10;
    : cout<<value<<endl;
    : }
    : it wont change the value of the value but carry on with the earlier wersion of it.
    : why is this happeing where is it finding these things duplicated .why is the obj
    : or the object file of my programm not getting updated.
    : and yes every file that i have added in my programm i have added it using the option
    : of NEW CLASS from the menu.so it builds one header".h" file and one ".cpp" file for it
    : and i am using those file whether or not to write classes .i am making functions
    : outside the classes and structures too.does that make any difference
    : everything works fine but only problem is the ambiguity.why is that happening*/
    :
    : int x;
    : int *x1;
    : int *x2;
    : int x3 ;
    :
    : PointerCheck *PointerCheck_Pointer_1;
    : PointerCheck PointerCheck_obj1;
    :
    :
    : void main()
    : {
    :
    :
    : x=UnderstandingStructuresCPPmain1(10); //
    : return;
    : }
    :
    : /*************************************
    :
    : onceagainclass.h
    :
    : #include <stdio.h>
    : #include
    : #include
    : #include
    :
    : *************************************/
    :
    :
    : /**********************************************
    : UnderstandingStructures.cpp
    :
    :
    : #include "UnderstandingStructures.h"
    :
    :
    : /*UnderstandingStructures::UnderstandingStructures()
    : {
    : i commented these constructor and destructor
    : because it too was giving the same problem
    : even when this file has been generated by
    : vc++ itself.i have declared them in the header file itself now
    : }
    :
    : UnderstandingStructures::~UnderstandingStructures()
    : {
    :
    : }*/
    :
    : struct UnStruct1 UnStruct1_Obj1;
    : struct UnStruct1 *UnStruct1_Pointer1;
    : struct UnStruct1 &UnStruct1_Refrence1=UnStruct1_Obj1;
    : struct UnStruct2 UnStruct2_Obj1;
    : struct UnStruct2 *UnStruct2_Pointer1;
    : struct UnStruct2 &UnStruct2_Refrence1=UnStruct2_Obj1;
    : struct UnStruct3 UnStruct3_Obj1;
    : struct UnStruct3 *UnStruct3_Pointer1;
    : struct UnStruct3 &UnStruct3_Refrence1=UnStruct3_Obj1;
    :
    : int UnderstandingStructuresCPPmain1(int value)//
    : {
    : char String1[]="aman";
    : cout<<"String1:-hello:-"<<String1<<endl;
    : return (0);
    : }
    : **********************************************/
    : /********************************************
    :
    : UnderstandingStructures.h
    :
    : #include <stdio.h>
    : #include
    : #include
    :
    :
    : #if !defined(AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_)
    : #define AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_
    :
    : #if _MSC_VER > 1000
    : #pragma once
    : #endif // _MSC_VER > 1000
    :
    : class UnderstandingStructures
    : {
    : public:
    : UnderstandingStructures()
    : { }
    : virtual ~UnderstandingStructures()
    : { }
    :
    : };
    : #endif //!defined(AFX_UNDERSTANDINGSTRUCTURES_H__D431F924_045F_11DA_8002_8E5213F52D55__INCLUDED_)
    :
    : struct UnStruct1
    : {
    : private:
    :
    : int intVARIABLEofUnStruct1;
    : char charVARIABLEofUnStruct1;
    : long longVARIABLEofUnStruct1;
    : int *intPOINTERofUnStruct1;
    : char *charPOINTERofUnStruct1;
    : long *longPOINTERofUnStruct1;
    :
    : public:
    : UnStruct1Function1(int value1,char value2)
    : {
    : }
    :
    : };
    :
    : struct UnStruct2
    : {
    :
    : int intVARIABLEofUnStruct2;
    : char charVARIABLEofUnStruct2;
    : long longVARIABLEofUnStruct2;
    : int *intPOINTERofUnStruct2;
    : char *charPOINTERofUnStruct2;
    : long *longPOINTERofUnStruct2;
    :
    : public:
    : UnStruct2Function1(int value1,char value2)
    : {
    : }
    :
    : };
    :
    : struct UnStruct3
    : {
    :
    : protected:
    : int intVARIABLEofUnStruct3;
    : char charVARIABLEofUnStruct3;
    : long longVARIABLEofUnStruct3;
    : int *intPOINTERofUnStruct3;
    : char *charPOINTERofUnStruct3;
    : long *longPOINTERofUnStruct3;
    :
    : public:
    : UnStruct3Function1(int value1,char value2)
    : {
    : }
    :
    : };
    :
    : struct
    : {
    : int intVARIABLEofAynonomus;
    : int *intPOINTERofAynonomus;
    : };//just for checking if like unions anoynymous structures could be built
    :
    : ****************************************************
    : /*************************************
    : PointerCheck.h
    :
    : #include
    : #include
    : #include
    : #include
    :
    : #if !defined(AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_)
    : #define AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_
    :
    : #if _MSC_VER > 1000
    : #pragma once
    : #endif // _MSC_VER > 1000
    :
    : class PointerCheck
    : {
    : public:
    : PointerCheck()
    : {
    : cout<<"Inside The Constructor Of PointerCheck"<<" this "<<this<<endl;
    : }
    : virtual ~PointerCheck()
    : {
    : cout<<"Inside The Destructor Of PointerCheck"<<" this "<<this<<endl;
    : }
    : };
    : void (*PointerToFunction)();
    : int PointerCheckCPPFunction(int value,long value1);
    : int PointerCheckCPPFunction(int value,long value1)
    : {
    : cout<<"inside void PointerCheckCPPFunction(void)"<<" value="<<value<<endl;
    :
    : int *intPointer_var1;
    : int *intPointer_var2[30];
    : int intPointer_var3;
    :
    : cout<<"now printing the values from within PointerCheckCPPFunction"<<endl<<& intPointer_var1<<" intPointer_var1 "<<endl<<&intPointer_var2<<" intPointer_var2"<<endl;
    : return (value+10);
    : }
    :
    : void justchecking(void)
    : {
    : PointerToFunction=(void(*)())(&PointerCheckCPPFunction);
    : cout<<" then and now "<<endl<<endl<<endl;
    : cout<<PointerToFunction<<" <--->"<<&PointerCheckCPPFunction<<endl<<" <--PointerToFunction inside PointerCheck.cpp"<<endl<<endl;
    : }
    : struct StructureInCPlusPlus1
    : {
    : int *intPointer1;
    : char *charPointer1;
    : int *intPoinerArray1[10];
    : long *longPointer1;
    : }StructureInCPlusPlus3;
    : struct StructureInCPlusPlus2
    : {
    : int *intPointer1;
    : char *charPointer1;
    : int *intPoinerArray1[10];
    : long *longPointer1;
    :
    : };
    : int& ObjectItself(int& value) //int &ObjectIteself(int &value)
    : { //can be written like this also
    : //which makes it more clear
    : //that these are refrencing the
    : //objects that is taking and sending
    : //using addresses.pointers could have
    : //been used for the same purpose too
    : int *sendvalue;
    :
    : cout<<" inside ObjectItself sendvalue is="<<sendvalue<<endl;
    : sendvalue=&value;
    : cout<<"sendvalue holds";
    : cout<<" the addressof value which is sendvalue="<<sendvalue<<endl;
    : cout<<"and address of is &sendvalue:-"<<&sendvalue<<endl;
    : cout<<"letussee what is *(sendvalue):-"<<*(sendvalue)<<endl;
    : cout<<"letussee what is *(&sendvalue):-"<<*(&sendvalue)<<endl;
    : //cout<<"letussee *(33412):-"<<*(333412);
    : return *sendvalue;
    : cout<<"leaving ObjectItself"<<endl;
    : }
    :
    : #endif
    : //!defined(AFX_POINTERCHECK_H__B4207805_02D4_11DA_8002_BE20EC158654__INCLUDED_)
    :
    :
    :
    [/code]

    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • RIGHT_THENRIGHT_THEN Member Posts: 154
    well thanks mr. vilanye for teling not .cpp files to be included
    which i am trying and it is working.but what is i want to include
    .cpp files but you need not answer that i will bother you with some
    bigger trouble sometime.
    and thanks mr.IDE for telling me that it is actually the angry face
    which is causing the compiler not to run properly.sure sir there
    is more to machine then bits and bytes.


    and as promised gentlemen here are your pending billion thanks.take then
    they are yours.

    by mr-ide
    : The code looks funny with all the angry faces, that's maybe why the
    : compiler don't like the code.


  • tom_swtom_sw Member Posts: 828
    : well thanks mr. vilanye for teling not .cpp files to be included
    : which i am trying and it is working.but what is i want to include
    : .cpp files but you need not answer that i will bother you with some
    : bigger trouble sometime.
    : and thanks mr.IDE for telling me that it is actually the angry face
    : which is causing the compiler not to run properly.sure sir there
    : is more to machine then bits and bytes.
    :
    :
    : and as promised gentlemen here are your pending billion thanks.take then
    : they are yours.
    :
    : by mr-ide
    : : The code looks funny with all the angry faces, that's maybe why the
    : : compiler don't like the code.
    :

    Vilanye is correct when he says not to include .cpp files. The general rule is that .h files should contain definitions and declarations, while the .cpp (or .c) files contain code. That's not a hard and fsat rule, but is a good rule of thumb.

    The reason to not include .cpp files is that when you #include a file, the preprocessor effectively takes the entire contents of that file and duplicates it at that point in your code. Now if you have define function foo() in foo.cpp, then #include foo.cpp in bar.cpp this is what happens:
    The compiler compiles the foo.cpp code into an .obj file and "knows" where to go anytime foo() is called in your program. It now goes on to compile bar.cpp and the contents of foo.cpp are effectively copied into bar.cpp. Now it tries to compile the copy of foo(), but it has a second function with the same name. Which should be activated when foo() is called whinin your program? This is the duplicate error you are seeing.

    NOTE:
    When you use the environment to create a new class, notice that there are a couple of lines at the beginning of the header file like:
    #if !defined(FOO_H_INCLUDED)
    #define FOO_H_INCLUDED
    and a corresponding #endif at the end of the header file, with the contents of the header file in between. This prevents the preprocessor from making multiple declarations of the same set of functions/class/whatever since the first time the .h is #included, the value FOO_H_INCLUDED is not defined, so it gets defined. The next time it is included, the value is already defined, so the preprocessor skips down to the #endif. The entire contents are skipped so there is no duplicated code.

    You could add this sort of mechanism to your .cpp files if you want to keep including them, but that's not really a good idea. It would be better to have to things you want outside objects to know about your class in the .h file, but keep the details of how those things happen hidden within the .cpp file.

    BTW: the angry faces comment was a joke - lighten up! :-)

Sign In or Register to comment.