标题:LNK2005,链接时提示构造函数和析构函数重复定义,新手求教
只看楼主
firmthinking
Rank: 3Rank: 3
来 自:西南交通大学
等 级:论坛游侠
帖 子:43
专家分:115
注 册:2012-12-9
结帖率:80%
已结贴  问题点数:20 回复次数:1 
LNK2005,链接时提示构造函数和析构函数重复定义,新手求教
------------头文件Str_my.h---------------
程序代码:
#ifndef Str_my_H
#define Str_my_H
#define Maxsize 20

class Str_my{
    private:
        char *data;
    public:
        Str_my();
        Str_my(char a[],int n);
        ~Str_my();
        int StrLen();
        char * StrCat();
        int StrCmp();
};

#endif


---------------源文件Str_my.cpp------------
程序代码:
#include<iostream>
#include<stdlib.h>
#include"Str_my.h"
using namespace std;

Str_my::Str_my(){
    data=(char * )malloc(sizeof(char)*Maxsize);
}

Str_my::Str_my(char a[],int n){
    data=(char * )malloc(sizeof(char)*Maxsize);
    for(int i=0;i<n;i++){
        *(data+i)=a[i];
    }
    *(data+n)='\0';
}

Str_my::~Str_my(){
    if(data!=NULL) free(data);
}


--------------主函数源程序Str_my_main.cpp------------
程序代码:
#include<iostream>
#include<stdlib.h>
#include"Str_my.h"
#include"Str_my.cpp"
using namespace std;

int main(){
    Str_my S1;
    char a[4]={'C','J','L','!'};
    Str_my S2(a,4);
    return 0;
}


错误提示在下面
程序代码:
Deleting intermediate files and output files for project '字符串的实现 - Win32 Debug'.
--------------------Configuration: 字符串的实现 - Win32 Debug--------------------
Compiling...
Str_my.cpp
Str_my_main.cpp
Linking...
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(void)" (??0Str_my@@QAE@XZ) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(char * const,int)" (??0Str_my@@QAE@QADH@Z) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::~Str_my(void)" (??1Str_my@@QAE@XZ) already defined in Str_my.obj
Debug/字符串的实现.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.

字符串的实现.exe - 1 error(s), 0 warning(s)

太久没摸代码了,请指导,在线等~
搜索更多相关主题的帖子: char data int include obj 
2018-05-14 20:58
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
得分:20 
#include"Str_my.cpp" 去掉再試一下?
2018-05-14 22:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486918-1-1.html




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

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