C++ 写我自己的字符串类,但得到;参考‘;字符串’;是含糊不清的;编译错误

C++ 写我自己的字符串类,但得到;参考‘;字符串’;是含糊不清的;编译错误,c++,string,namespaces,C++,String,Namespaces,我正在尝试编写我自己的字符串类(为了理解目的)。我写的如下,, 文件1 string.h #include<iostream> #include<string.h> namespace MyString { class string { char* ch; int len; public: string(); string(const char* ch); char* getString(); int length(); }; } #包括 #包括 名称空间MyStrin

我正在尝试编写我自己的字符串类(为了理解目的)。我写的如下,, 文件1 string.h

#include<iostream>
#include<string.h>
namespace MyString
{
class string
{
char* ch;
int len;
public:
string();
string(const char* ch);
char* getString();
int length();
};
}
#包括
#包括
名称空间MyString
{
类字符串
{
char*ch;
内伦;
公众:
字符串();
字符串(常量字符*ch);
char*getString();
int length();
};
}
文件2 string.cpp
#包括
#包括“string.h”
使用名称空间std;
使用MyString::string;//仅使用MyString命名空间中的字符串。
string::string()
{                       
ch=NULL;
len=0;
}                     
字符串::字符串(常量字符*ch)
{                       
这->ch=ch;
}   
char*string::getString()
{
返回ch;
}
int字符串::length()
{
回程透镜;
}
int main()
{
string obj=“这是一个测试”;
cout您正在包括具有
字符串的
标识符的
,并且您正在使用
声明将
MyClass
引入全局命名空间。这会导致名称冲突


只需删除
文件(您似乎根本没有使用它)将整个实现放入
名称空间MyString{…}

名称空间是该问题的解决方案,但ai建议始终保持所有签名的显式

   using namespace std;
这是一个很好的快捷方式,如果你不得不混合使用不同的库,比如字符串、向量等,比如STL+BOOST,它会变得一团糟


删除using子句,并在每个STL对象上添加std::我建议将您的类从string更改为类似string(带大写)的类。基本上,类字符串已经定义,而且由于您有“使用命名空间std”,因此它还有问题…
使用名称空间std;
-请不要这样!!!感谢您发布编译器错误,但我不得不问:您自己没有读过吗?他们非常清楚地告诉您出了什么问题:编译器无法分辨哪个“字符串”你的意思是因为它在名称空间中看到了两个定义,你告诉它要导入到全局。谢谢你…感谢你的快速回复…我在namespce MyString{…}块中添加了实现,它对我很有用…非常感谢你…感谢所有人…感谢你的快速回复…我在namespce MyString{…}中添加了实现块,它对我有效…大家好,我对使用指令感到困惑。我修改了我的.cpp,如下所示,
g++     string.cpp   -o string
string.cpp:6:1: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp:6:1: error: ‘string’ does not name a type
string.cpp:12:1: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp:12:1: error: ‘string’ does not name a type
string.cpp:23:7: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp:23:7: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp: In function ‘char* getString()’:
string.cpp:25:9: error: ‘ch’ was not declared in this scope
string.cpp: At global scope:
string.cpp:28:5: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp:28:5: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp: In function ‘int length()’:
string.cpp:30:9: error: ‘len’ was not declared in this scope
string.cpp: In function ‘int main()’:
string.cpp:35:2: error: reference to ‘string’ is ambiguous
string.h:5:8: error: candidates are: class MyString::string
/usr/include/c++/4.6/bits/stringfwd.h:65:33: error:                 typedef struct std::basic_string<char> std::string
string.cpp:35:9: error: expected ‘;’ before ‘obj’
make: *** [string] Error 1
   using namespace std;