C++的中使用函数的文件如何找到函数定义
头文件#ifndefSALES_ITEM_H_INCLUDED#defineSALES_ITEM_H_INCLUDED#include<iostream>#include...
头文件
#ifndef SALES_ITEM_H_INCLUDED
#define SALES_ITEM_H_INCLUDED
#include <iostream>
#include <string>
class Sales_item
{
public:
std::istream& input(std::istream& in);
std::ostream& output(std::ostream& out) const;
double avg_price()const;
bool same_isbn(const Sales_item &rhs)const
{
return isbn==rhs.isbn;
}
Sales_item():units_sold(0),revenue(0.0){}
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
#endif // SALES_ITEM_H_INCLUDED
对类成员函数定义的源文件
#include "Sales_item.h"
std::istream& Sales_item::input(std::istream& in)
{
double price;
in>>isbn>>units_sold>>price;
//检验是否读入成功
if(in)
revenue=units_sold*price;
else{
units_sold=0;
revenue=0.0;
}
return in;
}
double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}
std::ostream& Sales_item::output(std::ostream& out) const
{
out<<isbn<<"\t"<<units_sold<<"\t"
<<revenue<<"\t"<<avg_price();
return out;
}
主函数源文件
#include "Sales_item.h"
#include <iostream>
using namespace std;
int main()
{
Sales_item item;
//读入并输出一组交易
cout<<"Enter some transactions(Ctrl +Z to end):"
<<endl;
while(item.input(cin)){
cout<<"The transaction readed is:"<<endl;
item.output(cout);
cout<<endl;
}
return 0;
}
出现两个错误
E:\C++练习\C++primer\第七章\7.31.o:7.31.cpp|| undefined reference to `Sales_item::output(std::ostream&) const'|
E:\C++练习\C++primer\第七章\7.31.o:7.31.cpp|| undefined reference to `Sales_item::input(std::istream&)'|
也就是没有找到主函数的定义,求大神指教 展开
#ifndef SALES_ITEM_H_INCLUDED
#define SALES_ITEM_H_INCLUDED
#include <iostream>
#include <string>
class Sales_item
{
public:
std::istream& input(std::istream& in);
std::ostream& output(std::ostream& out) const;
double avg_price()const;
bool same_isbn(const Sales_item &rhs)const
{
return isbn==rhs.isbn;
}
Sales_item():units_sold(0),revenue(0.0){}
private:
std::string isbn;
unsigned units_sold;
double revenue;
};
#endif // SALES_ITEM_H_INCLUDED
对类成员函数定义的源文件
#include "Sales_item.h"
std::istream& Sales_item::input(std::istream& in)
{
double price;
in>>isbn>>units_sold>>price;
//检验是否读入成功
if(in)
revenue=units_sold*price;
else{
units_sold=0;
revenue=0.0;
}
return in;
}
double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}
std::ostream& Sales_item::output(std::ostream& out) const
{
out<<isbn<<"\t"<<units_sold<<"\t"
<<revenue<<"\t"<<avg_price();
return out;
}
主函数源文件
#include "Sales_item.h"
#include <iostream>
using namespace std;
int main()
{
Sales_item item;
//读入并输出一组交易
cout<<"Enter some transactions(Ctrl +Z to end):"
<<endl;
while(item.input(cin)){
cout<<"The transaction readed is:"<<endl;
item.output(cout);
cout<<endl;
}
return 0;
}
出现两个错误
E:\C++练习\C++primer\第七章\7.31.o:7.31.cpp|| undefined reference to `Sales_item::output(std::ostream&) const'|
E:\C++练习\C++primer\第七章\7.31.o:7.31.cpp|| undefined reference to `Sales_item::input(std::istream&)'|
也就是没有找到主函数的定义,求大神指教 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询