gpt4 book ai didi

c++ - 编写我自己的字符串类但得到 "reference to ‘string’ 是不明确的“编译错误

转载 作者:太空狗 更新时间:2023-10-29 19:53:08 25 4
gpt4 key购买 nike

我正在尝试编写自己的字符串类(为了理解目的)。我写的如下,文件 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;)时给出错误?
在这方面的任何指示都会非常有帮助。

最佳答案

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

关于c++ - 编写我自己的字符串类但得到 "reference to ‘string’ 是不明确的“编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com