help with sting and include files ?

M

mark

this program compiles fine with g++ on linux. But on VC++ 6.0 I am
getting errors like below. I have tried including <cstring> as well

error C2065: string undeclared identifier
s1 undeclared identifier and similarly for s2 as well.

again the same code works just fine with g++ on linux.

#include <iostream>
#include <string>

int main()
{
string s1("hello");
string s2; cin>>s2;
cout<<s2;
cout<<s1<<endl;
return 0;
}
 
V

Victor Bazarov

mark said:
this program compiles fine with g++ on linux.

Whatever version of g++ you got, it's broken.
But on VC++ 6.0 I am
getting errors like below. I have tried including <cstring> as well

error C2065: string undeclared identifier
s1 undeclared identifier and similarly for s2 as well.

again the same code works just fine with g++ on linux.

#include <iostream>
#include <string>

Names of types and objects in C++ Library are declared inside the
'std' namespace. You have to specify it in the name or bring the
used names into the scope where they will become accessible. BTW,
what book on C++ are you reading that doesn't explain that? Was
it published before 1998? Throw it away and get a newer one.
int main()
{
string s1("hello");
string s2; cin>>s2;
cout<<s2;
cout<<s1<<endl;

The four lines above should look like this in Standard C++:

std::string s1("hello");
std::string s2; std::cin>>s2;
std::cout<<s2;
std::cout<<s1<<std::endl;

Or, if you are a bit more attentive to proper use of whitespace:


std::string s1("hello"), s2;
std::cin >> s2;
std::cout << s2;
std::cout << s1 << std::endl;
return 0;
}

Read up on 'namespace' and 'using declaration' ("using" is a keyword)

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top