在Codeblocks中使用多文件结构时,报错“multiple definition of `****'”

bacon524 发布于 2017/04/13 14:54
阅读 612
收藏 0

main.cpp

#include <iostream>
#include "head.h"
using namespace std;
int main()
{
  product pro[3]={product(101,5,23.5),product(101,12,24.56),product(103,100,21.5)};
    int i;
    for(i=0;i<3;i++)
        pro[i].total();
    product::display();
    return 0;
}
 

head.h

#ifndef PRICE_H_INCLUDED
#define PRICE_H_INCLUDED

#include "product.cpp"

#endif // PRICE_H_INCLUDED
 

product.cpp

#include <iostream>
using namespace std;
class product
{
public:
    product(int a,int q,double p):num(a),quantity(q),price(p){};
    void total();
    static double average();
    static void display();
private:
    int num;
    int quantity;
    double price;
    static double discount;
    static double sum;
    static int n;
};
void product::total()
{
    double dis=1.00;
    if(quantity>10) dis=0.98*dis;
    sum=sum+quantity*price*dis*(1-discount);
    n=n+quantity;
}
double product::average()
 {
    return (sum/n);
}
void product::display()
{
    cout<<sum<<endl;
    cout<<average()<<endl;
}
 

在编译的时候就报错

D:\project\price\product.cpp|19|multiple definition of `product::total()'|

D:\project\price\product.cpp|26|multiple definition of `product::average()'|

D:\project\price\product.cpp|30|multiple definition of `product::display()'|

obj\Debug\main.o:main.cpp|| undefined reference to `product::discount'|

obj\Debug\main.o:main.cpp|| undefined reference to `product::sum'|

obj\Debug\product.o:product.cpp|| undefined reference to `product::n'|

请问这是什么原因,要怎么修改?

加载中
0
GestureWei
GestureWei
源文件被当做头文件引入了,自然会产生重定义
返回顶部
顶部