Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string header not working, wha????

Status
Not open for further replies.

gabeman

Programmer
Jun 10, 2004
1
CA
using MS Visual Studio 6.0. I checked to see if the file 'string' exists, and indeed it does. It is also specified in the right folder in my include options. I cant seem to initialize anything with the string or basic_string classes. here's my code:

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

int main()
{
string mystring;
return 0;
}

debug:
C:\Documents and Settings\Gabe\Desktop\Learning C++\sdjfhwe\dfg.cpp(8) : error C2065: 'string' : undeclared identifier
C:\Documents and Settings\Gabe\Desktop\Learning C++\sdjfhwe\dfg.cpp(8) : error C2146: syntax error : missing ';' before identifier 'mystring'
C:\Documents and Settings\Gabe\Desktop\Learning C++\sdjfhwe\dfg.cpp(8) : error C2065: 'mystring' : undeclared identifier
 
Use
Code:
std::string mystring

Plus, you're mixing old and new style headers
#include <string> // this is new, which uses namespaces
#include <iostream.h> // this is old, which doesn't use namespaces

--
 
Alternatively, if you don't want to use std::string, you can always declare using namespace std;...

Code:
#include <iostream>
#include <string>

using namespace std;

void main()
{
  String mystring; [green]//no need for std here[/green]
}
 
use string instead of String. They are case sensitive. Also don't mix different types of STL libraries (with .h extension and without extension). Because of mixing them you could came compilation(good if only compilation) and runtime problems very difficult to find. Use STL only withould extension. In VisualStudio 2003 STL with .h extensions are not suported anymore. To not expose the whole namespace (in big projects it is dangerous) you can expose only what you need:

#include <iostream>
#include <string>

using std::string;//exposing only string

void main()
{
string mystring; //no need for std here
}

Ion Filipski
1c.bmp
 
Ack! yea ... Good eyes there, IonFilipski!

It should be [red]string[/red] instead of [red]String[/red].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top