多重定义错误 [英] Multiple definition error
问题描述
与上一篇文章类似的问题。我用
创建了一个项目,大约有7个文件,包括3个头文件和3个实现文件。我在编译一个特定的
类文件时遇到多重定义错误。我已经检查过,如果标识符的存储分配不止一个,那么就会出现此错误。但我不会看到问题出在哪里。有什么建议吗?
< code>
//ItemType.h
#ifndef ItemType_hpp //我被告知要包括这个指令
#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 ComparisonTo(HouseType);
private:
StrType lastName;
StrType firstName;
StrType地址;
浮动价格;
int squareFeet ;
int bedRooms;
};
#endif
// Ite mType.cpp
#include" ItemType.h"
typedef HouseType ItemType;
using namespace std ;
// ItemType.cpp是真实的
//庄园操作程序数据的实现文件。
void HouseType :: GetFromFile(性病:: ifstream的和放;文件)
{
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<<价格<< endl;
file<< squareFeet<< endl;
file<< bedRooms<<结束;
}
void HouseType :: GetFromUser()
{
cout<< 输入姓氏;按回车键。 << endl;
lastName.GetString(true,NOT_NEW);
cout<< 输入名字;按回车键。 << endl;
firstName.GetString(true,NOT_NEW);
cout<< 输入地址;按回车键。 << endl;
address.GetString(true,NOT_NEW);
cout<< 输入价格,平方英尺,卧室数量;
<< "按回车键。 <<结束;
cin>价格> squareFeet> bedRooms;
}
void HouseType :: PrintHouseToScreen()< br $>
{
firstName.PrintToScreen(false);
cout<< " " ;;
lastName.PrintToScreen(false);
address.PrintToScreen(true);
cout<< endl<< 价格: <<价格<< endl;
cout<< 平方英尺: << squareFeet<< endl;
cout<< 卧室: << bedRooms<<结束;
}
void HouseType :: GetNameFromUser()
{
cout<< 输入姓氏;按回车键。 << endl;
lastName.GetString(true,NOT_NEW);
cout<< 输入名字;按回车键。 << endl;
firstName.GetString(true,NOT_NEW);
}
void HouseType :: PrintNameToScreen()
{
firstName.PrintToScreen(false);
cout<< " " ;;
lastName.PrintToScreen(false);
cout<<结束;
}
RelationType HouseType :: ComparisonTo(HouseType house)
{
if (lastName< house.lastName)
返回LESS;
else if(house.lastName< lastName)
return GREATER;
else if(firstName< house.firstName)
返回LESS;
else if(house.firstName< firstName)
返回GREATER;
否则返回EQUAL;
}
< / code>
错误:
多个定义
`HouseType :: GetFromFile(std :: basic_ifstream< char,
std :: char_traits< char& ;)''
首先在这里定义
多个定义
`HouseType :: WriteToFile(std :: basic_ofstream< char,
std :: char_traits< char&)''
等..
zf*****@mail.com 写道:
<剪辑>
错误:
多个定义
`HouseType :: GetFromFile(std :: basic_ifstream< char,
std :: char_traits< char&)''
首先在这里定义
多重定义
`HouseType :: WriteToFile(std :: basic_ofstream< char,
std :: char_traits< char&)''
等..
你必须在另一个源文件中定义函数。
-
Ian Collins。
< blockquote> Ian Collins写道:
zf ***** @ mail.com 写道:
< snip>
>>错误:
`HouseType :: GetFromFile(std :: basic_ifstream< char,
std :: char_traits< char&)''
首先在这里定义
`HouseType的多重定义: :将writeToFile(STD :: basic_ofstream< char,
std :: char_traits< char&)''
等..
你必须在另一个来源中定义函数文件。
或者您是否链接了相同的源文件两次?
-
Ian Collins。
基本规则:成员函数应该在实现文件中,或者应该是
是静态/内联(如果在.h文件中)
zf*****@mail.com 写道:
与上一篇文章类似的问题。我用
创建了一个项目,大约有7个文件,包括3个头文件和3个实现文件。我在编译一个特定的
类文件时遇到多重定义错误。我已经检查过,如果标识符的存储分配不止一个,那么就会出现此错误。但我不会看到问题出在哪里。有什么建议吗?
< code>
//ItemType.h
#ifndef ItemType_hpp //我被告知要包括这个指令
#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 ComparisonTo(HouseType);
private:
StrType lastName;
StrType firstName;
StrType地址;
浮动价格;
int squareFeet;
int bedRooms;
};
#endif
// ItemType.cpp
#include" ItemType.h"
typedef HouseType ItemType;
using namespace std ;
// ItemType.cpp是真实的
//庄园操作程序数据的实现文件。
void HouseType :: GetFromFile(性病:: ifstream的和放;文件)
{
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<<价格<< endl;
file<< squareFeet<< endl;
file<< bedRooms<<结束;
}
void HouseType :: GetFromUser()
{
cout<< 输入姓氏;按回车键。 << endl;
lastName.GetString(true,NOT_NEW);
cout<< 输入名字;按回车键。 << endl;
firstName.GetString(true,NOT_NEW);
cout<< 输入地址;按回车键。 << endl;
address.GetString(true,NOT_NEW);
cout<< 输入价格,平方英尺,卧室数量;
<< "按回车键。 <<结束;
cin>价格> squareFeet> bedRooms;
}
void HouseType :: PrintHouseToScreen()< br $>
{
firstName.PrintToScreen(false);
cout<< " " ;;
lastName.PrintToScreen(false);
address.PrintToScreen(true);
cout<< endl<< 价格: <<价格<< endl;
cout<< 平方英尺: << squareFeet<< endl;
cout<< 卧室: << bedRooms<<结束;
}
void HouseType :: GetNameFromUser()
{
cout<< 输入姓氏;按回车键。 << endl;
lastName.GetString(true,NOT_NEW);
cout<< 输入名字;按回车键。 << endl;
firstName.GetString(true,NOT_NEW);
}
void HouseType :: PrintNameToScreen()
{
firstName.PrintToScreen(false);
cout<< " " ;;
lastName.PrintToScreen(false);
cout<<结束;
}
RelationType HouseType :: ComparisonTo(HouseType house)
{
if (lastName< house.lastName)
返回LESS;
else if(house.lastName< lastName)
return GREATER;
else if(firstName< house.firstName)
返回LESS;
else if(house.firstName< firstName)
返回GREATER;
否则返回EQUAL;
}
< / code>
错误:
多个定义
`HouseType :: GetFromFile(std :: basic_ifstream< char,
std :: char_traits< char& ;)''
首先在这里定义
多个定义
`HouseType :: WriteToFile(std :: basic_ofstream< char,
std :: char_traits< char&)''
等..
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..
zf*****@mail.com wrote:
<snip>
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 wrote:zf*****@mail.com wrote:
<snip>
>>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.
basic rule: member function should be in implementation file or should
be static/inline(if in .h file)
zf*****@mail.com wrote: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..
这篇关于多重定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!