编写我自己的字符串类,但获取“引用'字符串'是不明确的”编译错误

时间:2013-06-25 19:54:48

标签: c++ string namespaces

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

file 2 string.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;)? 这方面的任何指示都会非常有用。

3 个答案:

答案 0 :(得分:8)

您的cpp文件中是using namespace std。现在std中有一个字符串,MySpace中有一个字符串,因此编译器不知道选择哪一个。使用限定名称来区分它们,例如MyString::string而不是string或删除using namespace std;或将整个实现放入namespace MyString { ... }

答案 1 :(得分:1)

您的<string.h>包含string标识符,并且您使用MyClass声明将using引入全局命名空间。这导致名称冲突。

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

答案 2 :(得分:0)

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

   using namespace std;

是一个很好的捷径,如果你必须混合不同的lib,包含字符串,矢量等,即STL + BOOST

,它将变得一团糟

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