-1

我对 C++ 有点陌生,并且使用带有 g++ 作为编译器的 linux 平台。以上三者的混合让我陷入了困境。

这是(非常简单的)程序:

#include<iostream>
#include<string.h>
#include<stdlib.h>

using namespace std;

class string
{
private:
    char str[20];
public:
    string()
    {
        str[0]='\0';
    }
    string(char*s)
    {
        strcpy(str,s);
    }
    string(int a)
    {
        itoa(a,str,10);
    }
    operator int()//overloaded cast operator,converts string to int
    {
        int i=0,l,ss=0,k=1;
        for(i=strlen(str)-1;i>=0;i--)
        {
            ss=ss+(str[i]-48)*k;
            k=k*10;
        }
        return ss;
    }
    void displayData()
    {
        cout<<str;
    }
};

int main()
{
    string s1=123;
    cout<<endl<<"s1=";
    s1.displayData();

    s1=150;
    cout<<endl<<"s1=";
    s1.displayData();

    string s2("123");
    int i=int(s2);
    cout<<endl<<"i="<<i;

    string s3("456");
    i=s3;
    cout<<endl<<"i="<<i;
}

我得到的错误

naveen@linuxmint ~/Desktop/C++ $ g++ int2string.cpp -o int2string
int2string.cpp: In constructor ‘string::string(int)’:
int2string.cpp:22:16: error: ‘itoa’ was not declared in this scope
int2string.cpp: In function ‘int main()’:
int2string.cpp:42:2: error: reference to ‘string’ is ambiguous
int2string.cpp:7:7: error: candidates are: class string
In file included from /usr/include/c++/4.7/iosfwd:41:0,
                 from /usr/include/c++/4.7/ios:39,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from int2string.cpp:1:
/usr/include/c++/4.7/bits/stringfwd.h:65:33: error:                 typedef class std::basic_string<char> std::string
int2string.cpp:42:9: error: expected ‘;’ before ‘s1’
int2string.cpp:44:2: error: ‘s1’ was not declared in this scope
int2string.cpp:50:2: error: reference to ‘string’ is ambiguous
int2string.cpp:7:7: error: candidates are: class string
In file included from /usr/include/c++/4.7/iosfwd:41:0,
                 from /usr/include/c++/4.7/ios:39,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from int2string.cpp:1:
/usr/include/c++/4.7/bits/stringfwd.h:65:33: error:                 typedef class std::basic_string<char> std::string
int2string.cpp:50:9: error: expected ‘;’ before ‘s2’
int2string.cpp:51:12: error: ‘s2’ was not declared in this scope
int2string.cpp:54:2: error: reference to ‘string’ is ambiguous
int2string.cpp:7:7: error: candidates are: class string
In file included from /usr/include/c++/4.7/iosfwd:41:0,
                 from /usr/include/c++/4.7/ios:39,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from int2string.cpp:1:
/usr/include/c++/4.7/bits/stringfwd.h:65:33: error:                 typedef class std::basic_string<char> std::string
int2string.cpp:54:9: error: expected ‘;’ before ‘s3’
int2string.cpp:55:4: error: ‘s3’ was not declared in this scope

我认为我没有使用正确的头文件名,因此存在歧义。请帮助

4

2 回答 2

4

从第一个错误开始,没有itoa标准函数,但是在“新”C++11 标准中有一个std::to_string可以使用的函数(或者,std::strtol如果您使用的是没有 C++ 的旧编译器,则可以使用11 支持)。当然,标准库中也有将数值转换为字符串的函数,例如std::stoi.

这让我想到另一件事,如果你想学习 C++,你应该开始使用例如std::string字符串。将来会对你有很大帮助。不要重新发明标准库中已有的东西。

至于其他一些问题,有一些可能是因为您将整个命名空间std导入到全局命名空间中,这意味着std::string现在只是string,这当然与您自己的string类的名称冲突。如果你不想写例如std::cout,那么你可以只导入你想要的名字

using std::cout;
于 2013-08-09T11:58:34.897 回答
3

头文件中有一个名为std::string定义的标准库容器<string>。标头<iostream>隐含地包含它。

然后将整个std命名空间导入全局范围并定义自己的string类。现在您string在全局范围内有两个符号,编译器理所当然地抱怨它不知道您说的是哪个符号string s1=123;

最好的办法是根本不使用using namespace std;看看为什么

那么,itoa就不是标准功能了。

其余的错误主要是第一个错误的后果。

于 2013-08-09T11:57:58.310 回答