私は 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
正しいヘッダー ファイル名を使用していないと思うため、あいまいさがあります。助けてください