免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2092 | 回复: 3
打印 上一主题 下一主题

[C++] 请教一个C++的问题,关于“multiple definiton” [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-09 20:00 |只看该作者 |倒序浏览
我准备将头文件,类成员定义和主函数放在三个不的文件中,这三个文件在同一个工程下,但是dev  c++却提示说“  multiple definition of `intarray::intarray(int)' ”,等等,所有的函数都“重复定义”了,这是什么问题呢,我百思不得其解,请教大家一下了:
附源代码
//cls_intarray.h:

class intarray{
      private:
              int *array;
              int maxsize;
              int realsize;
      public:
             intarray(int=20);
             ~intarray();                        //destructor
             int count()const;
             int insert(int);
             int emptest()const;
             int initiate(int);
             int del(int);
             void display()const;
};

clsdef_intarray.cpp:
#include<iostream>
#include"cls_intarray.h"
using namespace std;
intarray::intarray(int m){                                  //constructor
                       maxsize=m;
                       array=new int[maxsize];
                       int i;
                       for(i=0;i<maxsize;i++){
                                              *(array+i)=0;
                       }
                       cout<<"The "<<maxsize<<"-"<<"number "<<"array has been established!"<<endl;
}
intarray::~intarray(){
                      delete[] array;
}
int intarray::initiate(int num){
    if(num<=0){
               return 0;
    }else{
          realsize=num;
          cout<<"Please input "<<realsize<<" numbers"<<endl;
          int i;
          for(i=0;i<realsize;i++){
                                  cin>>*(array+i);
          }
          return 1;
    }
}
int intarray::emptest()const{
     if(realsize==0){
                    return 1;
     }else{
           return 0;
     }
}
int intarray::insert(int num){
     if(realsize==maxsize){
                           return 0;
     }else{
           array[realsize++]=num;
           return 1;
     }
}
int intarray::del(int n){
     if((n<0)||(n>=realsize)){
                              return 0;
     }else{
           int i;
           n--;
           for(i=n;i<realsize;i++){
                                   array=array[i+1];
           }
           array[realsize--]=0;
           return 1;
     }
}
int intarray::count()const{
    return realsize;
}
void intarray::display()const{
     int i;
     for(i=0;i<realsize;i++){
                             cout<<array<<"  ";
                             if((i+1)%4==0){
                                            cout<<endl;
                             }
     }
}
//main.cpp:
#include<iostream>
#include"clsdef_intarray.cpp"
using namespace std;
int main(){
    cout<<"Please intput the maxsize of the array you want!"<<endl;
    int size;
    cin>>size;
    intarray arr1(size);
    if(arr1.emptest()){
                          cout<<"The array now is empty!"<<endl;
    }
    cout<<"Please input the numbers of the elements!"<<endl;
    int elem;
    cin>>elem;
    if(arr1.initiate(elem)){
                          cout<<"The array now contains elements!"<<endl;
    }else{
          cout<<"INITIATE ERROR!!"<<endl;
    }
    cout<<"These are:"<<endl;
    arr1.display();
    cout<<"Please input an integer you want to insert:"<<endl;
    int insrt;
    cin>>insrt;
    if(arr1.insert(insrt)){
                           cout<<insrt<<" has been added to the end of the array"<<endl;
                           arr1.display();
    }else{
          cout<<"INSERT ERROR!!"<<endl;
    }
    cout<<"Now the array has "<<arr1.count()<<" numbers"<<endl;
    cout<<"Please input the submark of the element you want to delete"<<endl;
    int sbmrk;
    cin>>sbmrk;
    if(arr1.del(sbmrk)){
                        cout<<"Successfully Deleted"<<endl;
                        arr1.display();
    }else{
          cout<<"DELETE ERROR!!!"<<endl;
    }
    system("pause");
    return 0;
}
谢谢大家!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-09-09 21:13 |只看该作者
在你的.h文件中开始部分添加
#ifndef  __CLSDEF_INTARRAY_H__
#define __CLSDEF_INTARRAY_H__
文件最后加上
#endif __CLSDEF_INTARRAY_H__
原因请搜索“怎么防止头文件包含”。

论坛徽章:
0
3 [报告]
发表于 2007-09-09 22:02 |只看该作者
原帖由 lenovo 于 2007-9-9 21:13 发表
在你的.h文件中开始部分添加
#ifndef  __CLSDEF_INTARRAY_H__
#define __CLSDEF_INTARRAY_H__
文件最后加上
#endif __CLSDEF_INTARRAY_H__
原因请搜索“怎么防止头文件包含”。

标准做法

论坛徽章:
0
4 [报告]
发表于 2007-09-11 02:13 |只看该作者
原帖由 zwylinux 于 2007-9-9 22:02 发表

标准做法


即便算不上 industrial standard,也是 de facto standard 了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP