使用下面的代码,当我尝试编译时
clang++ -std=c++11 -stdlib=libc++ -o test sales_function.cpp sales_prog.cpp
我收到以下错误
duplicate symbol __ZN10Sales_data7combineERKS_ in:
/var/folders/7f/9r4z5bs90bjfm3dy1k_g03xc0000gn/T/sales_functions-5G1FRA.o
/var/folders/7f/9r4z5bs90bjfm3dy1k_g03xc0000gn/T/sales_prog-82wDRv.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
现在为了让文件 sales_functions.cpp 中的非成员函数工作,我包含了“sales_dat.h”头文件,因为函数使用的结构 Sales_data 是在那里定义的。不过,在该文件中,我有非成员函数“read”,它在结构 Sales_data 的构造函数之一中调用。所以为了规避我在sales_dat.h中的struct定义之前转发声明的struct并声明了read函数。
我一直在尝试不同的事情,比如将 struct 的定义放在它自己的文件中,并将声明只放在头文件中。但这给了我其他问题,编译器无法在我的非成员函数文件中使用它的对象。
我得到了那个错误,但我不明白为什么会出现上述错误。我在想它可能与“sales_dat.h”标题有关结构 Sales_data 对象。
这里到底发生了什么?sales_functions.cpp
#include <iostream>
#include "sales_dat.h"
std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookName >> item.books_sold >> price;
item.revenue = price * item.books_sold;
return is;
}
std::ostream &print(std::ostream &os, Sales_data &item)
{
os << item.isbn() << " " << item.books_sold << " " << item.revenue;
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
sales_dat.h
#ifndef SALES_DAT_H
#define SALES_DAT_H
#include <iostream>
#include <string>
struct Sales_data;
std::istream &read(std::istream &, Sales_data &);
struct Sales_data {
std::string bookName;
std::string isbn() const { return bookName; }
Sales_data &combine(const Sales_data &);
unsigned books_available = 10;
unsigned books_sold = 0;
double revenue = 0;
unsigned total_sold = 0;
unsigned count = 1;
Sales_data() = default;
Sales_data(unsigned c) : count(c) {}
Sales_data(const std::string &s) : bookName(s) {}
Sales_data(const std::string &s, unsigned m, unsigned n, double p) : bookName(s), books_sold(m), books_available(n), revenue(p*m) {}
Sales_data(std::istream &inpst) { read(inpst, *this); }
};
std::ostream &print(std::ostream &, Sales_data &);
Sales_data add(const Sales_data &, const Sales_data &);
Sales_data &Sales_data::combine(const Sales_data &rs)
{
count += rs.count;
books_sold += rs.books_sold;
revenue += rs.revenue;
return *this;
}
#endif
sales_prog.cpp
#include <iostream>
#include <string>
#include "sales_dat.h"
int main()
{
std::cout << "Enter a transaction" << std::endl;
Sales_data total(std::cin);
if(std::cin) {
Sales_data trans;
while(read(std::cin,trans)) {
if(total.isbn() == trans.isbn()) {
total = add(total,trans);
}
else {
std::cout << "Number of " << total.isbn() << " transactions: " << total.count << std::endl;
std::cout << "Number of " << total.isbn() << " sold: " << total.books_sold << std::endl;
std::cout << "Revenue: " << total.revenue << std::endl;
total = trans;
}
}
if(total.books_sold != 0) {
std::cout << "Number of " << total.isbn() << " transactions: " << total.count << std::endl;
std::cout << "Number of " << total.isbn() << " sold: " << total.books_sold << std::endl;
std::cout << "Revenue: " << total.revenue << std::endl;
}
else
std::cout << "Entry: " << total.isbn() << " has zero sold" << std::endl;
}
else
std::cerr << "No transaction" << std::endl;
return 0;
}