#ifndef *.h
#define *.h
//..........
#endif
和*.CPP放在同一个文件下编译
第一个文件9-4-1。h
namespace SALES
{
const int QUARTERS=4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales (Sales & s, const double ar[], int n);
void setSales (Sales & s);
void showSales (const Sales & s);
}
第二个9-4-2。cpp
#include <iostream>
#include "9-4-1.h"
namespace SALES
{
using std::cout;
using std::cin;
void setSales (Sales & s, const double ar[],int n)
{
double sum;
sum=0;
int i;
for (i=0;i<4,n<1;i++,n--)
{
s.sales[i]=ar[i];
if (s.sales[i]>s.max)
s.max=s.sales[i];
if (s.sales[i]<s.min)
s. min=s.sales[i];
sum+=s.sales[i];
}
s.average=sum/i;
if (n!=0)
{
for (;i<4;i++)
s.sales[i]=0;
}
}
void setSales (Sales & s)
{
double sum;
sum=0;
for(int i=0;i<4;i++)
{
std::cout<<"please input the "<<i+1<<"th sale";
std::cin>>s.sales[i];
if (s.sales[i]>s.max)
s.max=s.sales[i];
if (s.sales[i]<s.min)
s.min=s.sales[i];
sum+=s.sales[i];
}
s.average=sum/4;
}
void showSales(const Sales & s)
{
for (int i=0;i<4;i++)
{
cout<<"the "<<i+1<<"th sale is "<<s.sales[i]<<"\n";
}
cout<<"the average is "<<s.average;
cout<<"the max is "<<s.max;
cout<<"the min is "<<s.min;
}
}
第三个9-4-3。cpp
#include <iostream>
#include "9-4-1.h"
using namespace std;
using namespace SALES;
int main()
{
Sales Sales1;
Sales Sales2;
double ar[3]={234,123,53.2};
int n=3;
setSales(Sales1,ar,n);
setSales(Sales2);
showSales(Sales1);
showSales(Sales2);
}
编译9-4-3时提示
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccEDbaaa.o(.text+0x195) In function `main':
[Linker error] undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'
[Linker error] undefined reference to `SALES::setSales(SALES::Sales&)'
[Linker error] undefined reference to `SALES::showSales(SALES::Sales const&)'
[Linker error] undefined reference to `SALES::showSales(SALES::Sales const&)'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ccEDbaaa.o(.text+0x195) ld returned 1 exit status
这是什么原因?
sorry! #ifndef *.h->#ifndef *_h
#define *.h->#define *_h
#ifndef head_h
#define head_h
namespace SALES
{
const int QUARTERS=4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales (Sales & s, const double ar[], int n);
void setSales (Sales & s);
void showSales (const Sales & s);
}
#endif
一个文件也可以
#include<iostream>
using namespace std;
const int QUARTERS=4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales (Sales & s, const double ar[], int n);
void setSales (Sales & s);
void showSales (const Sales & s);
void setSales (Sales & s, const double ar[],int n)
{
double sum;
sum=0;
int i;
for (i=0;i<4,n<1;i++,n--)
{
s.sales[i]=ar[i];
if (s.sales[i]>s.max)
s.max=s.sales[i];
if (s.sales[i]<s.min)
s. min=s.sales[i];
sum+=s.sales[i];
}
s.average=sum/i;
if (n!=0)
{
for (;i<4;i++)
s.sales[i]=0;
}
}
void setSales (Sales & s)
{
double sum;
sum=0;
for(int i=0;i<4;i++)
{
std::cout<<"please input the "<<i+1<<"th sale";
std::cin>>s.sales[i];
if (s.sales[i]>s.max)
s.max=s.sales[i];
if (s.sales[i]<s.min)
s.min=s.sales[i];
sum+=s.sales[i];
}
s.average=sum/4;
}
void showSales(const Sales & s)
{
for (int i=0;i<4;i++)
{
cout<<"the "<<i+1<<"th sale is "<<s.sales[i]<<"\n";
}
cout<<"the average is "<<s.average;
cout<<"the max is "<<s.max;
cout<<"the min is "<<s.min;
}
int main()
{
Sales Sales1;
Sales Sales2;
double ar[3]={234,123,53.2};
int n=3;
setSales(Sales1,ar,n);
setSales(Sales2);
showSales(Sales1);
showSales(Sales2);
return 0;
}
[此贴子已经被作者于2007-9-13 22:23:09编辑过]