链接器错误LNK1169&;LNK2005 我学习C++,同时尝试练习翻译单元和其他东西,但是我得到标题上列出的错误。这个程序是为了学习,我试图解释每个头文件和实现文件应该做什么

链接器错误LNK1169&;LNK2005 我学习C++,同时尝试练习翻译单元和其他东西,但是我得到标题上列出的错误。这个程序是为了学习,我试图解释每个头文件和实现文件应该做什么,c++,c++11,C++,C++11,--------CString.h-------- --------CString.cpp---- 在构造函数中接收C样式的字符串,并截短它,方法是取前三个字符并将其存储在mystring中,以便稍后通过函数display()显示 #包括 #包括“CString.h” #定义NUMBEROFCHARACTERS 3 使用名称空间w1; w1::CString::CString(字符样式字符串[]) { 如果(样式字符串[0]='\0') { mystring[0]=''; } 其他的 { for

--------CString.h--------

--------CString.cpp---- 在构造函数中接收C样式的字符串,并截短它,方法是取前三个字符并将其存储在mystring中,以便稍后通过函数display()显示

#包括
#包括“CString.h”
#定义NUMBEROFCHARACTERS 3
使用名称空间w1;
w1::CString::CString(字符样式字符串[])
{
如果(样式字符串[0]='\0')
{
mystring[0]='';
}
其他的
{
for(int i=0;istd::coutThe
如何构建它?嗯,我单击试图运行main.cpp的调试器。您的链接错误消息是什么?错误LNK1169:找到一个或多个多重定义符号即使您的程序要构建,它也会崩溃,因为您没有为
mystring
分配内存。请稍候,当您提到我多次定义它时。Ar这就是我定义它的两个场景?void w1::CString::display(std::ostream&os){std::cout std::cout顺便说一句,对于大多数编译器或链接器来说,从它们的数字中发现错误通常是很简单的,而且经常有例子……无论是在工具文档、在线帮助中,还是通过使用搜索引擎,如
google
bing
。啊哈!我现在明白了!非常感谢!
#ifndef CSTRING_H
#define CSTRING_H
#include <iostream> 

namespace w1
{
class CString
{
    public:
        char mystring[];
        CString(char cstylestring[]);
        void display(std::ostream &os);
};

std::ostream &operator<< (std::ostream &os, CString &c)
{
    c.display(os);
    return os;
}
}
#endif 
void process(char cstylestring[]); 
#include <iostream> 
#include "CString.h"
#define NUMBEROFCHARACTERS 3   

using namespace w1; 

w1::CString::CString(char stylestring[]) 
{
if (stylestring[0] == '\0')
{
    mystring[0] = ' ';
}
else
{
    for (int i = 0; i < NUMBEROFCHARACTERS; i++)
    {
        mystring[i] = stylestring[i];
    }
}
//strncpy(mystring, stylestring, NUMBEROFCHARACTERS);
}


void w1::CString::display(std::ostream &os)
{
std::cout << mystring << std::endl;
}
#include "process.h"
#include "CString.h"
#include <iostream> 

using namespace std; 

void process(char cstylestring[])
{
w1::CString obj(cstylestring); 
std::cout << obj << std::endl;
} 
#include <iostream> 
#include "process.h"

using namespace std; 

int main()
{
char themainstring[] = "hiiiii";
process(themainstring);
return 0;
}
char mystring[4];
std::ostream &operator<< (std::ostream &os, CString &c)
{
    c.display(os);
    return os;
}