-1

I am a bit new to C++ and using linux platform with g++ as compiler.The mix of the above three have left me mired.

Here is the (very simple) program:

#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;
}

The errors I am getting

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

I think I am not using the correct header file name and hence there is an ambiguity.Please help

Prix
  • 19,417
  • 15
  • 73
  • 132
Naveen
  • 7,944
  • 12
  • 78
  • 165
  • 1
    Are you really trying to re-define the `std::string` class? –  Aug 09 '13 at 11:55
  • Is there already an inbuilt string class?The error suggests so.I was just creating a string object,thus created a `string` class. – Naveen Aug 09 '13 at 11:57
  • 5
    Related: [Why is `using namespace std;` considered bad practice?](http://stackoverflow.com/q/1452721/2040040) – johnchen902 Aug 09 '13 at 11:57
  • 3
    @InsaneCoder I hope you are not being serious. If you are programming in C++ and not aware of the `std::string` class, that's a big problem. –  Aug 09 '13 at 11:59
  • @MarcClaesen Or perhaps `#include `... – twalberg Aug 09 '13 at 15:20

2 Answers2

4

To start with the first error, there is no itoa standard function, there is however in the "new" C++11 standard a std::to_string function that can be used (or you can use std::strtol if you're on an older compiler with no C++11 support). There are of course functions in the standard library to convert numeric values to strings as well, such as std::stoi.

And that leads me to another thing, if you want to learn C++ you should start using e.g. std::string for strings. It will help you a lot in the future. Don't reinvent what's already in the standard library.

As for some of the other problems, there are some that can be because you imported the whole namespace std into the global namespace, that means that std::string is now just string, which of course collides with the name of your own string class. If you don't want to write e.g. std::cout then you can just import the names you want like

using std::cout;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

There's a standard library container called std::string defined in <string> header. The header <iostream> implicitly includes it.

Then you import the whole std namespace into global scope and define your own string class. Now you've got two string symbols in global scope and compiler is rightfully complaining that it doesn't know which one you mean when you say string s1=123;

The best thing to do is not use using namespace std; at all. See why.

Then, itoa is not a standard function.

The rest of the errors are mainly consequences of the first one.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141