架构x86_64的2个重复符号

时间:2015-05-18 00:24:01

标签: c++11

我把所有代码都放在main.cpp中,运行成功。但是,我将它们分成不同的头文件和cpp文件,出现了一条错误消息: 架构x86_64的2个重复符号。我想我在马科斯犯了错误。 有错误消息: 重复符号__ZN2w13MAXE:     /var/folders/f0/_q5s1yn17g55dkdhg7bd80xr0000gn/T/w1-7ce6b5.o     /var/folders/f0/_q5s1yn17g55dkdhg7bd80xr0000gn/T/CString-b6e225.o ld:1个用于体系结构x86_64的重复符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

// CString.h

#ifndef _CSTRING_H_
#define _CSTRING_H_
#include<iostream>
namespace w1{
extern const int MAX = 3;   
  class CString{
    char* _s; 
public:
    CString(const char *);
    void display(std::ostream&) const;
    ~CString(){if (_s != nullptr) delete [] _s;}
  };

std::ostream& operator<<(std::ostream&, CString&);
}

#endif

// CString.cpp

#include"CString.h"

namespace w1{

CString::CString(const char* s){
    if (s != nullptr){
        _s = new char [MAX];
        for(int i = 0; i < MAX; i++)
            _s[i] = s[i];
    }
    else
        _s = nullptr;
}

void CString::display(std::ostream& os) const{
    if(_s != nullptr)
        os << _s << std::endl;
    else
        os << "The string is empty." << std::endl;
}

std::ostream& operator<<(std::ostream& os, CString& target) {
    target.display(os);
    return os;
}
}

// main.cpp中

#include"CString.h"
using namespace w1; 
void process(const char* );

void process( const char* s){
    w1::CString instance(s);
    std::cout << instance << std::endl;
}

int main(int argc, char *argv[]){    
    if (argc == 1)
        std::cout << "Insufficient number of arguments (min 1)" << std::endl;
    else{
        std::cout << "Maximum number of characters stored : " << w1::MAX <<std::endl;
        for (int i = 1; i < argc; i++){
            std::cout << i << ": ";
            process(argv[i]);
        }
    }

    return 0;
}

0 个答案:

没有答案