Connecting Tech Pros Worldwide Forums | Help | Site Map

Multiple definition error

zfareed@mail.com
Guest
 
Posts: n/a
#1: Apr 11 '07
Similar problem to the previous post. I have created a project with
about 7 files including 3 header files and 3 implementation files. I
am getting a multiple definition error when compiling for one specific
file of class. I have checked that if there is more than one
allocation of storage for identifiers, this error arises. But I don't
see where the problem lies. Any suggestions?

<code>
//ItemType.h
#ifndef ItemType_hpp // I was told to include this directives
#define ItemType_hpp

#include <fstream>
#include <iostream>
#include "StrTypeAugmented.h"

const int MAX_ITEMS = 25;

enum RelationType {LESS, EQUAL, GREATER};
class HouseType
{
public:
void GetFromFile(std::ifstream&);
void WriteToFile(std::ofstream&);
void GetFromUser();
void PrintHouseToScreen();
void GetNameFromUser();
void PrintNameToScreen();
RelationType ComparedTo(HouseType);

private:
StrType lastName;
StrType firstName;
StrType address;
float price;
int squareFeet;
int bedRooms;
};
#endif
// ItemType.cpp
#include "ItemType.h"

typedef HouseType ItemType;

using namespace std;
// ItemType.cpp is the implementation file for data for the real
// estate manipulation program.

void HouseType::GetFromFile(std::ifstream& file)
{
lastName.GetStringFile(true, NOT_NEW, file);
firstName.GetStringFile(true, NOT_NEW, file);
address.GetStringFile(true, NOT_NEW, file);
file >price >squareFeet >bedRooms;
}

void HouseType::WriteToFile(std::ofstream& file)
{

lastName.PrintToFile( false, file);
firstName.PrintToFile(true, file);
address.PrintToFile(true, file);
file << endl << price << endl;
file << squareFeet << endl;
file << bedRooms << endl;
}

void HouseType::GetFromUser()
{

cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
cout << "Enter address; press return." << endl;
address.GetString(true, NOT_NEW);
cout << "Enter price, square feet, number of bedrooms;"
<< " press return." << endl;
cin >price >squareFeet >bedRooms;
}

void HouseType::PrintHouseToScreen()
{

firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
address.PrintToScreen(true);
cout << endl << "Price: " << price << endl;
cout << "Square Feet: " << squareFeet << endl;
cout << "Bedrooms: " << bedRooms << endl;
}

void HouseType::GetNameFromUser()
{

cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
}

void HouseType::PrintNameToScreen()
{

firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
cout << endl;
}

RelationType HouseType::ComparedTo(HouseType house)
{
if (lastName < house.lastName)
return LESS;
else if (house.lastName < lastName)
return GREATER;
else if (firstName < house.firstName)
return LESS;
else if (house.firstName < firstName)
return GREATER;
else return EQUAL;
}

</code>
ERROR:
multiple definition of
`HouseType::GetFromFile(std::basic_ifstream<char,
std::char_traits<char&)'
first defined here
multiple definition of
`HouseType::WriteToFile(std::basic_ofstream<char,
std::char_traits<char&)'
etc..


Ian Collins
Guest
 
Posts: n/a
#2: Apr 11 '07

re: Multiple definition error


zfareed@mail.com wrote:

<snip>
Quote:
ERROR:
multiple definition of
`HouseType::GetFromFile(std::basic_ifstream<char,
std::char_traits<char&)'
first defined here
multiple definition of
`HouseType::WriteToFile(std::basic_ofstream<char,
std::char_traits<char&)'
etc..
>
You must have the function defined in another source file.

--
Ian Collins.
Ian Collins
Guest
 
Posts: n/a
#3: Apr 11 '07

re: Multiple definition error


Ian Collins wrote:
Quote:
zfareed@mail.com wrote:
>
<snip>
>
Quote:
>>ERROR:
> multiple definition of
>>`HouseType::GetFromFile(std::basic_ifstream<char ,
>>std::char_traits<char&)'
> first defined here
> multiple definition of
>>`HouseType::WriteToFile(std::basic_ofstream<char ,
>>std::char_traits<char&)'
>>etc..
>>
>
You must have the function defined in another source file.
>
Or are you linking the same source file twice?

--
Ian Collins.
nishit.gupta@st.com
Guest
 
Posts: n/a
#4: Apr 11 '07

re: Multiple definition error


basic rule: member function should be in implementation file or should
be static/inline(if in .h file)
zfareed@mail.com wrote:
Quote:
Similar problem to the previous post. I have created a project with
about 7 files including 3 header files and 3 implementation files. I
am getting a multiple definition error when compiling for one specific
file of class. I have checked that if there is more than one
allocation of storage for identifiers, this error arises. But I don't
see where the problem lies. Any suggestions?
>
<code>
//ItemType.h
#ifndef ItemType_hpp // I was told to include this directives
#define ItemType_hpp
>
#include <fstream>
#include <iostream>
#include "StrTypeAugmented.h"
>
const int MAX_ITEMS = 25;
>
enum RelationType {LESS, EQUAL, GREATER};
class HouseType
{
public:
void GetFromFile(std::ifstream&);
void WriteToFile(std::ofstream&);
void GetFromUser();
void PrintHouseToScreen();
void GetNameFromUser();
void PrintNameToScreen();
RelationType ComparedTo(HouseType);
>
private:
StrType lastName;
StrType firstName;
StrType address;
float price;
int squareFeet;
int bedRooms;
};
#endif
// ItemType.cpp
#include "ItemType.h"
>
typedef HouseType ItemType;
>
using namespace std;
// ItemType.cpp is the implementation file for data for the real
// estate manipulation program.
>
void HouseType::GetFromFile(std::ifstream& file)
{
lastName.GetStringFile(true, NOT_NEW, file);
firstName.GetStringFile(true, NOT_NEW, file);
address.GetStringFile(true, NOT_NEW, file);
file >price >squareFeet >bedRooms;
}
>
void HouseType::WriteToFile(std::ofstream& file)
{
>
lastName.PrintToFile( false, file);
firstName.PrintToFile(true, file);
address.PrintToFile(true, file);
file << endl << price << endl;
file << squareFeet << endl;
file << bedRooms << endl;
}
>
void HouseType::GetFromUser()
{
>
cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
cout << "Enter address; press return." << endl;
address.GetString(true, NOT_NEW);
cout << "Enter price, square feet, number of bedrooms;"
<< " press return." << endl;
cin >price >squareFeet >bedRooms;
}
>
void HouseType::PrintHouseToScreen()
{
>
firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
address.PrintToScreen(true);
cout << endl << "Price: " << price << endl;
cout << "Square Feet: " << squareFeet << endl;
cout << "Bedrooms: " << bedRooms << endl;
}
>
void HouseType::GetNameFromUser()
{
>
cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
}
>
void HouseType::PrintNameToScreen()
{
>
firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
cout << endl;
}
>
RelationType HouseType::ComparedTo(HouseType house)
{
if (lastName < house.lastName)
return LESS;
else if (house.lastName < lastName)
return GREATER;
else if (firstName < house.firstName)
return LESS;
else if (house.firstName < firstName)
return GREATER;
else return EQUAL;
}
>
</code>
ERROR:
multiple definition of
`HouseType::GetFromFile(std::basic_ifstream<char,
std::char_traits<char&)'
first defined here
multiple definition of
`HouseType::WriteToFile(std::basic_ofstream<char,
std::char_traits<char&)'
etc..
Ian Collins
Guest
 
Posts: n/a
#5: Apr 11 '07

re: Multiple definition error


nishit.gupta@st.com wrote:
Quote:
basic rule: member function should be in implementation file or should
be static/inline(if in .h file)
Please don't top post.

The member definitions are in the source file.
Quote:
zfareed@mail.com wrote:
>
Quote:
>>Similar problem to the previous post. I have created a project with
>>about 7 files including 3 header files and 3 implementation files. I
>>am getting a multiple definition error when compiling for one specific
>>file of class. I have checked that if there is more than one
>>allocation of storage for identifiers, this error arises. But I don't
>>see where the problem lies. Any suggestions?
>>
>><code>
>>//ItemType.h
>>#ifndef ItemType_hpp // I was told to include this directives
>>#define ItemType_hpp
>>
>>#include <fstream>
>>#include <iostream>
>>#include "StrTypeAugmented.h"
>>
>>const int MAX_ITEMS = 25;
>>
>>enum RelationType {LESS, EQUAL, GREATER};
>>class HouseType
>>{
> public:
> void GetFromFile(std::ifstream&);
> void WriteToFile(std::ofstream&);
> void GetFromUser();
> void PrintHouseToScreen();
> void GetNameFromUser();
> void PrintNameToScreen();
> RelationType ComparedTo(HouseType);
>>
> private:
> StrType lastName;
> StrType firstName;
> StrType address;
> float price;
> int squareFeet;
> int bedRooms;
>>};
>>#endif
>>// ItemType.cpp
>>#include "ItemType.h"
>>
>>typedef HouseType ItemType;
>>
>>using namespace std;
>>// ItemType.cpp is the implementation file for data for the real
>>// estate manipulation program.
>>
>>void HouseType::GetFromFile(std::ifstream& file)
>>{
> lastName.GetStringFile(true, NOT_NEW, file);
> firstName.GetStringFile(true, NOT_NEW, file);
> address.GetStringFile(true, NOT_NEW, file);
> file >price >squareFeet >bedRooms;
>>}
>>
>>void HouseType::WriteToFile(std::ofstream& file)
>>{
>>
> lastName.PrintToFile( false, file);
> firstName.PrintToFile(true, file);
> address.PrintToFile(true, file);
> file << endl << price << endl;
> file << squareFeet << endl;
> file << bedRooms << endl;
>>}
>>
>>void HouseType::GetFromUser()
>>{
>>
> cout << "Enter last name; press return." << endl;
> lastName.GetString(true, NOT_NEW);
> cout << "Enter first name; press return." << endl;
> firstName.GetString(true, NOT_NEW);
> cout << "Enter address; press return." << endl;
> address.GetString(true, NOT_NEW);
> cout << "Enter price, square feet, number of bedrooms;"
> << " press return." << endl;
> cin >price >squareFeet >bedRooms;
>>}
>>
>>void HouseType::PrintHouseToScreen()
>>{
>>
> firstName.PrintToScreen( false);
> cout << " ";
> lastName.PrintToScreen( false);
> address.PrintToScreen(true);
> cout << endl << "Price: " << price << endl;
> cout << "Square Feet: " << squareFeet << endl;
> cout << "Bedrooms: " << bedRooms << endl;
>>}
>>
>>void HouseType::GetNameFromUser()
>>{
>>
> cout << "Enter last name; press return." << endl;
> lastName.GetString(true, NOT_NEW);
> cout << "Enter first name; press return." << endl;
> firstName.GetString(true, NOT_NEW);
>>}
>>
>>void HouseType::PrintNameToScreen()
>>{
>>
> firstName.PrintToScreen( false);
> cout << " ";
> lastName.PrintToScreen( false);
> cout << endl;
>>}
>>
>>RelationType HouseType::ComparedTo(HouseType house)
>>{
> if (lastName < house.lastName)
> return LESS;
> else if (house.lastName < lastName)
> return GREATER;
> else if (firstName < house.firstName)
> return LESS;
> else if (house.firstName < firstName)
> return GREATER;
> else return EQUAL;
>>}
>>
>></code>
>>ERROR:
> multiple definition of
>>`HouseType::GetFromFile(std::basic_ifstream<char ,
>>std::char_traits<char&)'
> first defined here
> multiple definition of
>>`HouseType::WriteToFile(std::basic_ofstream<char ,
>>std::char_traits<char&)'
>>etc..
>
>

--
Ian Collins.
nishit.gupta@st.com
Guest
 
Posts: n/a
#6: Apr 11 '07

re: Multiple definition error


OK sorry imessed...
code seems fine....
problem i guess will be in Makefile asIon said, multiple inclusion of
source file
Ian Collins wrote:
Quote:
nishit.gupta@st.com wrote:
Quote:
basic rule: member function should be in implementation file or should
be static/inline(if in .h file)
>
Please don't top post.
>
The member definitions are in the source file.
>
Quote:
zfareed@mail.com wrote:
Quote:
>Similar problem to the previous post. I have created a project with
>about 7 files including 3 header files and 3 implementation files. I
>am getting a multiple definition error when compiling for one specific
>file of class. I have checked that if there is more than one
>allocation of storage for identifiers, this error arises. But I don't
>see where the problem lies. Any suggestions?
>
><code>
>//ItemType.h
>#ifndef ItemType_hpp // I was told to include this directives
>#define ItemType_hpp
>
>#include <fstream>
>#include <iostream>
>#include "StrTypeAugmented.h"
>
>const int MAX_ITEMS = 25;
>
>enum RelationType {LESS, EQUAL, GREATER};
>class HouseType
>{
public:
void GetFromFile(std::ifstream&);
void WriteToFile(std::ofstream&);
void GetFromUser();
void PrintHouseToScreen();
void GetNameFromUser();
void PrintNameToScreen();
RelationType ComparedTo(HouseType);
>
private:
StrType lastName;
StrType firstName;
StrType address;
float price;
int squareFeet;
int bedRooms;
>};
>#endif
>// ItemType.cpp
>#include "ItemType.h"
>
>typedef HouseType ItemType;
>
>using namespace std;
>// ItemType.cpp is the implementation file for data for the real
>// estate manipulation program.
>
>void HouseType::GetFromFile(std::ifstream& file)
>{
lastName.GetStringFile(true, NOT_NEW, file);
firstName.GetStringFile(true, NOT_NEW, file);
address.GetStringFile(true, NOT_NEW, file);
file >price >squareFeet >bedRooms;
>}
>
>void HouseType::WriteToFile(std::ofstream& file)
>{
>
lastName.PrintToFile( false, file);
firstName.PrintToFile(true, file);
address.PrintToFile(true, file);
file << endl << price << endl;
file << squareFeet << endl;
file << bedRooms << endl;
>}
>
>void HouseType::GetFromUser()
>{
>
cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
cout << "Enter address; press return." << endl;
address.GetString(true, NOT_NEW);
cout << "Enter price, square feet, number of bedrooms;"
<< " press return." << endl;
cin >price >squareFeet >bedRooms;
>}
>
>void HouseType::PrintHouseToScreen()
>{
>
firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
address.PrintToScreen(true);
cout << endl << "Price: " << price << endl;
cout << "Square Feet: " << squareFeet << endl;
cout << "Bedrooms: " << bedRooms << endl;
>}
>
>void HouseType::GetNameFromUser()
>{
>
cout << "Enter last name; press return." << endl;
lastName.GetString(true, NOT_NEW);
cout << "Enter first name; press return." << endl;
firstName.GetString(true, NOT_NEW);
>}
>
>void HouseType::PrintNameToScreen()
>{
>
firstName.PrintToScreen( false);
cout << " ";
lastName.PrintToScreen( false);
cout << endl;
>}
>
>RelationType HouseType::ComparedTo(HouseType house)
>{
if (lastName < house.lastName)
return LESS;
else if (house.lastName < lastName)
return GREATER;
else if (firstName < house.firstName)
return LESS;
else if (house.firstName < firstName)
return GREATER;
else return EQUAL;
>}
>
></code>
>ERROR:
multiple definition of
>`HouseType::GetFromFile(std::basic_ifstream<cha r,
>std::char_traits<char&)'
first defined here
multiple definition of
>`HouseType::WriteToFile(std::basic_ofstream<cha r,
>std::char_traits<char&)'
>etc..
>
>
--
Ian Collins.
Ian Collins
Guest
 
Posts: n/a
#7: Apr 11 '07

re: Multiple definition error


nishit.gupta@st.com wrote:
Quote:
OK sorry imessed...
code seems fine....
problem i guess will be in Makefile asIon said, multiple inclusion of
source file
*Please* don't top-post!

--
Ian Collins.
nishit.gupta@st.com
Guest
 
Posts: n/a
#8: Apr 11 '07

re: Multiple definition error



Ian Collins wrote:
Quote:
nishit.gupta@st.com wrote:
Quote:
OK sorry imessed...
code seems fine....
problem i guess will be in Makefile asIon said, multiple inclusion of
source file
>
*Please* don't top-post!
>
--
Ian Collins.
ok

Ian Collins
Guest
 
Posts: n/a
#9: Apr 11 '07

re: Multiple definition error


nishit.gupta@st.com wrote:
Quote:
Ian Collins wrote:
>
Quote:
>>nishit.gupta@st.com wrote:
>>
Quote:
>>>OK sorry imessed...
>>>code seems fine....
>>>problem i guess will be in Makefile asIon said, multiple inclusion of
>>>source file
>>
>>*Please* don't top-post!
>>
Or quote signatures!
Quote:
ok
>
--
Ian Collins.
zfareed@mail.com
Guest
 
Posts: n/a
#10: Apr 11 '07

re: Multiple definition error


Thanks for the help guys; but I have tried to inline the function and
even commented the one function out and it still returns as a multiple
definition. Now you mentioned another source file, I dont have any
other file in the project where the function is defined. Can someone
help? I can send all my files if necessary.

Howard
Guest
 
Posts: n/a
#11: Apr 11 '07

re: Multiple definition error



<zfareed@mail.comwrote in message
news:1176319995.132964.208410@d57g2000hsg.googlegr oups.com...
Quote:
Thanks for the help guys; but I have tried to inline the function and
even commented the one function out and it still returns as a multiple
definition. Now you mentioned another source file, I dont have any
other file in the project where the function is defined. Can someone
help? I can send all my files if necessary.
>
(Please quote enough of what you're talking about that we don't have to
search previous posts to know what you're referring to.)

Are you including "ItemType.cpp" somewhere, instead of "ItemType.h"?

If not, then how about seeing the rest of the error messages? It looks like
you left out the info about what file was including what other file when you
copied the error messages here. Also, maybe some other error exists,
causing these errors as a side effect.

-Howard


Closed Thread