实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)

来源:互联网 发布:红苹果预算软件 编辑:程序博客网 时间:2024/04/30 16:00
#ifndef _UTILITY_H_//加static时,表示静态函数只在这个文件里用,那么编译器就在这个文件里找,没找到定义,就可以确定是未定义了;//不加static,表示静态函数可能在其他文件里,但是编译时没找到,又不能确定是哪个文件应该有,所以报没有链接错误。#define _UTILITY_H_//***ANSI C++#include <string>#include <iostream>#include <limits>#include <cmath>#include <fstream>#include <cctype>#include <ctime>#include <cstdlib>#include <cstdio>#include <iomanip>#include <cstdarg>#include <cassert>//****ANSI C++end//旧版C++标准库头文件/*#include <string.h>#include <iostream.h>#include <limits.h>#include <math.h>#include <fstream.h>#include <ctype.h>#include <time.h>#include <stdlib.h>#include <stdio.h>#include <iomanip.h>#include <stdarg.h>#include <assert.h>*///旧版C++标准库头文件endusing namespace std;#define MAX_ERROR_MESSAGE_LEN 20static char GetChar(istream &inStream);static bool UserSays_Yes();static void SetRand_Seed();static int GetRand(int n);static int GetRand();static int GetPoissionRand(double expectValue);template<class ElemType>void Swap(ElemType &e1,ElemType &e2)//交换e1,e2的值{ElemType temp;temp=e1;e1=e2;e2=temp;}template<class ElemType>void Display(ElemType elem[],int n)//显示数组各数据元素值{for(int i=0;i<n;i++)cout<<elem[i]<<" ";cout<<endl;}template<class ElemType>void Write(ElemType &e)//显示数据元素{cout<<e<<" ";}class Timer{private:clock_t startTime;public:Timer(){startTime=clock();}~Timer(){}double ElapsedTime()//返回已过的时间{clock_t endTime=clock();return (double)(endTime-startTime)/(double)CLK_TCK;}void  Reset()//重置开始的时间{startTime=clock();}};//通用的异常处理类class Error{private:char message[MAX_ERROR_MESSAGE_LEN];//异常处理信息public:Error(char mes[]="一般性异常!"){strcpy(message,mes);}~Error(){}void Show()const{cout<<message<<endl;}};#define DEFAULT_SIZE 100//默认元素个数#define DEFAULT_INFINITY 1000000//默认无穷大//自定义类型enum StatusCode{SUCCESS,FAIL,UNDER_FLOW,OVER_FLOW,RANGE_ERROR,DUPLICATE_ERROR,NOT_PRESENT,ENTRY_INSERTED,ENTRY_FOUND,VISITED,UNVISITED};//将函数说明为静态函数,使函数的作用域只局限于被包含的源程序文件,这样可以避免在一个程序中有多个源程序文件都包含改函数的定义时出现的程序连接错误//getchar()函数static char GetChar(istream &inStream){char ch;while((ch=(inStream).peek())!=EOF&&(ch=(inStream).get())==' '||ch=='\t')//peek函数从输入流中接受一个字符,流的当前位置不变,get函数从输入流中接受一个字符,流的当前位置向后移动一个位置;return ch;}//当用户肯定回答(yes)时,返回true;当用户否定回答(no)时,返回falsestatic bool UserSays_Yes(){char ch;bool initialResponse=true;do{if(initialResponse)cout<<"(y,n)?";elsecout<<"please answer y or n:";ch=GetChar(cin);initialResponse=false;}while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');if(ch=='y'||ch=='Y')return true;elsereturn false;}//定时器类//有关随机数的函数static void SetRand_Seed()//设置当前时间为随机数种子{srand((unsigned)time(NULL));}static int GetRand(int n)//生成0~n-1之间的随机数{return rand()%(n);}static int GetRand()//生成随机数{return rand();}static int GetPoissionRand(double expectValue)//生成期望值为expectValue的泊松随机数,泊松分布适用于事件驱动中模拟离散产生的事件,该函数可得到一个非负整数序列满足给定期希值的泊松分布{double x=rand()/(double)(RAND_MAX+1);//X均匀分布于[0,1)int k=0;double p=exp(-expectValue);//p为泊松分布值double s=0;while(s<=x){s+=p;k++;p=p*expectValue/k;}return k-1;}//其他实用函数#endif
#include "utility.h"//调试时#error : "eh.h is only for C++!"如果文件名是.c改为.cpp,测试#define NUM 280int main(){try//用try分装可能出现异常的代码{if(NUM>280)throw Error("NUM值太大了!");int a[NUM+1][NUM+1],b[NUM+1][NUM+1],c[NUM+1][NUM+1];bool isContinue=true;Timer objTimer;while(isContinue){int i,j,k;SetRand_Seed();objTimer.Reset();for(i=1;i<=NUM;i++)for(j=1;j<=NUM;j++){a[i][j]=GetRand();b[i][j]=GetRand();}//求c=abfor(i=1;i<=NUM;i++)for(j=1;j<=NUM;j++){c[i][j]=0;for(k=1;k<=NUM;k++)c[i][j]=c[i][j]+a[i][k]*b[k][j];}cout<<"using time:"<<objTimer.ElapsedTime()<<"second."<<endl;cout<<"if you want to continue";isContinue=UserSays_Yes();}}catch(Error my_error){my_error.Show();}system("PAUSE");return 0;}


0 0