我对C ++有点新,使用linux平台和g ++作为编译器。以上三种方法的混合使我陷入困境。
这是(非常简单)的程序:
#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
我认为我没有使用正确的头文件名,因此存在歧义。请帮助
答案 0 :(得分:4)
从第一个错误开始,没有itoa
标准功能,但是在&#34; new&#34; C ++ 11标准可以使用std::to_string
函数(如果您使用的是没有C ++ 11支持的旧编译器,则可以使用std::strtol
)。当然,标准库中还有一些函数可以将数值转换为字符串,例如std::stoi
。
这导致我另一件事,如果你想学习C ++,你应该开始使用,例如std::string
表示字符串。它将来会对你有所帮助。不要重新发明标准库中的内容。
至于其他一些问题,有一些可能是因为您将整个命名空间std
导入全局命名空间,这意味着std::string
现在只是string
,这当然与您自己的string
类的名称相冲突。如果你不想写,例如std::cout
然后您可以导入您想要的名称
using std::cout;
答案 1 :(得分:3)
在<string>
标题中定义了一个名为std::string
的标准库容器。标题<iostream>
隐含地包含它。
然后将整个std
命名空间导入全局范围并定义自己的string
类。现在你在全局范围内有两个string
个符号,编译器正在抱怨当你说string s1=123;
最好的办法是不要使用using namespace std;
。 See why
然后,itoa
不是标准函数。
其余的错误主要是第一个错误的结果。