我照着教材在dev c++中打了一个自定义数组的库,第一次对头文件点编译图标毫无反应,对cpp文件点运行说源文件未编译。我只好把dev c++卸载了重装。
第二次再对头文件点编译无误。但对实现头文件的cpp点编译报[Error] ld returned 1 exit status。对应用的cpp点编译也是同一个错,点运行还是说源文件未编译。
我把相同的代码复制到codeblocks里,完全正常。
代码应该没问题。因为从没运行成功过,所以也不存在后台运行未关闭。我的电脑也没有装杀毒软件。马上机考只能用dev c++,但我实在不知道问题出在哪。求各位大神帮帮小弟!
代码如下
array.h
#ifndef _array_h
#define _array_h
struct doublearray
{
int low,high;
double *storage;
};
bool initialize(doublearray &arr,int low,int high);
bool insert(const doublearray &arr,int index,double value);
bool fetch(doublearray &arr,int index,double &value);
void cleanup(const doublearray &arr);
#endif
array.cpp
#include <iostream>
using namespace std;
#include "array.h"
bool initialize(doublearray &arr,int low,int high)
{
arr.low=low;
arr.high=high;
arr.storage=new double[high-low+1];
if(arr.storage!=NULL)
return true;
else
return false;
}
bool insert(const doublearray &arr,int index,double value)
{
if(index<arr.low||index>arr.high)
return false;
arr.storage[index-arr.low]=value;
return true;
}
bool fetch(doublearray &arr,int index,double &value)
{
if(index<arr.low||index>arr.high)
return false;
value=arr.storage[index-arr.low];
return true;
}
void cleanup(const doublearray &arr)
{
if(arr.storage)
delete []arr.storage;
}
arrayapply.cpp
#include "array.h"
#include <iostream>
using namespace std;
int main()
{
doublearray array;
double value;
int low,high;
cout<<"low high"<<endl;
cin>>low>>high;
if(!initialize(array,low,high))
{
cout<<"get space failed"<<endl;
return 1;
}
for(int i=low;i<=high;i++)
{
cout<<"array["<<i<<"]= "<<endl;
cin>>value;
insert(array,i,value);
}
int j;
cout<<"第几个元素?"<<endl;
cin>>j;
if(fetch(array,j,value))
cout<<"array["<<j<<"]= "<<value<<endl;
else
cout<<"下标越界"<<endl;
cleanup(array);
return 0;
}