3

我正在尝试编写自己的字符串类(出于理解目的)。我写的如下,文件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();
};
}

文件 2 字符串.cpp

#include<iostream>
#include"string.h"
using namespace std;
using MyString::string; // use string from MyString namespace only.
string::string()
{                       
ch = NULL;      
len = 0;
}                     
string::string(const char* ch)
{                       
this->ch = ch;  
}   
char* string::getString()
{
return ch;
}
int string::length()
{
return len;
}
int main()
{
string obj = "This is a test";
cout << obj.getstring<<endl;
return 0;
}

但是我的程序甚至无法编译,出现以下错误

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

我没有得到共鸣,为什么编译器使用 MyNamespace 中的字符串显式给出错误(即使用 MyNamespace::string;)?
在这方面的任何指示都会非常有帮助。

4

3 回答 3

10

using namespace std在您的 cpp 文件中。现在有一个字符串 instd和一个 in MySpace,所以编译器不知道该选择哪一个。使用限定名称来区分它们,例如MyString::string代替stringOR 摆脱using namespace std;OR 将整个实现放入namespace MyString { ... }.

于 2013-06-25T19:58:05.997 回答
1

您正在包括<string.h>哪个具有string标识符,并且您正在通过声明将MyClass其引入全局命名空间using。这导致名称冲突。

只需删除该<string.h>文件(无论如何您似乎都没有使用它)。

于 2013-06-25T19:57:43.760 回答
0

命名空间是该问题的解决方案但是,ai 建议始终保持所有签名明确。

   using namespace std;

是一个不错的捷径,如果您必须混合具有字符串、向量等的不同库,即 STL + BOOST,它将变得一团糟

删除 using 子句,并在每个 STL 对象上添加 std::。

于 2013-06-25T20:02:28.727 回答