| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 898 人关注过本帖
标题:我写了3个程序,包括一个头文件,请问怎么运行它们呢?
只看楼主 加入收藏
flyingni
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-9-8
收藏
 问题点数:0 回复次数:10 
我写了3个程序,包括一个头文件,请问怎么运行它们呢?
谢谢!
搜索更多相关主题的帖子: 文件 运行 
2007-09-13 20:39
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

#ifndef *.h
#define *.h


//..........

#endif

和*.CPP放在同一个文件下编译


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-13 20:44
flyingni
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-9-8
收藏
得分:0 
回复:(flyingni)我写了3个程序,包括一个头文件,请...

我分成3个文件写的,怎么运行呢

2007-09-13 20:57
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 


#ifndef *.h
#define *.h

//头文件
#endif

保存文件名为 *。h


同时在*.cpp文件内加上 #include"*.h"

几个文件放在一起编译就可以了


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-13 21:16
flyingni
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-9-8
收藏
得分:0 
回复:(flyingni)回复:(flyingni)我写了3个程序,...

第一个文件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

这是什么原因?

2007-09-13 21:34
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

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


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-13 21:58
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

一个文件也可以


#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编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-13 22:22
flyingni
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-9-8
收藏
得分:0 
回复:(flyingni)回复:(flyingni)回复:(flying...
还是不行,我都郁闷了,你用的什么编译器?
2007-09-13 22:33
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

VC++6.0 , dev c++测试通过


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-13 22:35
jbwang
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-5-22
收藏
得分:0 
#include "stdafx.h"
#include "action.h"
#include "Sales.h"
#include<iostream>
using namespace std;
int main()
{
SALES::Sales Sales1; //注意命名空间的添加,你可能错在这个地方了。
SALES::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-09-13 23:36
快速回复:我写了3个程序,包括一个头文件,请问怎么运行它们呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017661 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved