3

I am trying to write my own string class(for understanding purpose). I have written it as follows, file 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();
};
}

file 2 string.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;
}

But my program failed to even compile, with following error

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

I am not getting the reson, why compiler gives errors wven explicitly using string from MyNamespace, (i.e. using MyNamespace::string;) ?
Any pointers in this regard will be very helpful.

3
  • I would recommend changing your class from string to something like String (with caps). Basically, the class string is already defined and it is additionally problematic because you have "using namespace std"... Commented Jun 25, 2013 at 19:57
  • 2
    using namespace std; - please NOOOOOOOOO!!! Commented Jun 25, 2013 at 19:59
  • Thanks for posting the compiler errors but, I have to ask: Did you not read them yourself? They told you quite clearly what was wrong: the compiler can't tell which "string" you mean because it sees two definitions in the namespaces you have told it to import to global. Commented Jun 26, 2013 at 1:45

3 Answers 3

10

You are using namespace std inside your cpp file. Now there is a string in std and one in MySpace, so the compiler does not know which one to choose. Use qualified names to differ them, like MyString::string instead of string OR get rid of using namespace std; OR put your whole implementation into a namespace MyString { ... }.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You... For your quick reply... I added implementation in namespce MyString{...} block and it worked for me... Thank You very much...
1

You're including <string.h> which has a string identifier, and you're introducing MyClass into the global namespace with your using declaration. This is causing a name clash.

Just remove the <string.h> file (you don't seem to be using it anyway).

Comments

0

Namespace are a solution to that problem But, ai would recommend to keep all signature explicit all the time.

   using namespace std;

is a nice shortcut it will become a mess if your have to mix different lib having string, vector, etc i.e STL + BOOST

remove the using clause, and add std:: on each STL object.

2 Comments

Thank All You... For your quick replies... I added implementation in namespce MyString{...} block and it worked for me...
Hi All, I am getting confused with using directive. I modified my .cpp as follows,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.