I have trouble understanding the compilers.
The following code does work in UNIX under g++, but under VC++ it would not even compile. Anyone can provide valid reasons why?

Code:

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


using namespace std;

int main()
{
	string tmp_nw_msg, crc_chksum, buffer;

	cout << "Enter the string : ";
	cin >> buffer;

	if (strlen(buffer.c_str()) >15 ) {
		tmp_nw_msg = buffer.substr(1,12);
		crc_chksum = buffer.substr(13,2);

		cout << " N/W msg : "<< tmp_nw_msg << endl;
		cout << " crc chksum : "<< crc_chksum << endl;
	}
	else {
		cout << "error" << endl;
	}
	
	std::cin.get();
	return 0;
	
}

The following error is thrown by VC++, but in g++ it does work fine.

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\documents and settings\my documents\visual studio 2005\projects\dummy_substr\dummy_substr\substr.cpp 13
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\documents and settings\my documents\visual studio 2005\projects\dummy_substr\dummy_substr\substr.cpp 19
Error 3 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\documents and settings\my documents\visual studio 2005\projects\dummy_substr\dummy_substr\substr.cpp 20
Error 4 fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\my documents\visual studio 2005\projects\dummy_substr\dummy_substr\substr.cpp(9)' was matched c:\documents and settings\my documents\visual studio 2005\projects\dummy_substr\dummy_substr\substr.cpp 29

Output from g++ :

Enter the string : BD2d1100mayor47E
N/W msg : D2d1100mayor
crc chksum : 47

change #include <string.h> to : #include <string> This should never have not compiled in G++, so that's strange..

change #include <string.h> to : #include <string>

That did solve the problem and I agree that's the C++ way of doing things, but VC++ did accept <stdio.h>, so why not <string.h> as well?

And one more strange thing that I noticed in VC++(VS 2005 actually, I believe also in VS 2008), there is no syntactic highlighting for the 'string ' datatype, which is available for int or double or char or any other datatype. Is it coz' string is not a 'primitive' datatype?

This should never have not compiled in G++, so that's strange..

:P Nice wordings there...

That did solve the problem and I agree that's the C++ way of doing things, but VC++ did accept <stdio.h>, so why not <string.h> as well?

It does accept string.h, but <string> and <string.h> are two very different headers. <string.h> has nothing to do with std::strings.

:P Nice wordings there...

Whoops, brainfart :) You should remove the extra "not" from that sentence.

I believe that <string.h> is the old C string header and is now called <cstring> in C++. This is completely different from the C++ strings library. In GCC (g++) you don't need to declare <string> and it will still compile. I guess that it must add it automatically for you, whereas in visual studio if you do not include it explicitly then your program will not compile.

Based on the book that I'm currently reading about C++ you shouldn't mix the old C style <string.h> with the new C++ ones <cstring>. Aparently this is because the new libraries are templated and the old ones aren't. This can mess up your programs although I can't remember any more than that at the moment. So, you should have declared:

<cstdlib>, <string>, and <iostream>.

Actually, you should try to avoid using c-functions in C++.
If you change if (strlen(buffer.c_str()) >15 ) to if (buffer.length() >15 ) , you don't need cstdlib at all. Just <string> and <iostream>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.