Click to See Complete Forum and Search --> : What's wrong with this code??
crptcblade
May 7th, 2001, 06:32 PM
I've been struggling to figure out what's wrong with this. Can somebody help? Using VC++ I have this code :
#include <stdio.h>
#include <iostream.h>
#include <string.h>
string sMyStr;
int main()
{
sMyStr = "Hello";
return 0;
}
I get these errors :
C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : error C2146: syntax error : missing ';' before identifier 'sMyStr'
C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : error C2501: 'string' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : fatal error C1004: unexpected end of file found
:mad::(:mad:
Wynd
May 7th, 2001, 06:49 PM
Try this:
#include <stdio.h> //Why do you need this for what you are doing?
#include <iostream.h>
#include <string>
std::string sMyStr;
int main()
{
sMyStr = "Hello";
return 0;
}
Megatron
May 7th, 2001, 06:54 PM
You could use char*, which is a string.
#include <iostream.h>
char* sMyStr;
int main()
{
sMyStr = "Hello";
return 0;
}
HarryW
May 7th, 2001, 08:06 PM
It's usually better to use the standard headers. If you use <iostream.h> then a lot of the stuff that's in the standard namespace usually is accessible without taking any notice of namespaces, like cout for instance. I'm not sure whether that's because the M$ header puts you in the standard namespace (instead of the global namespace that is) or because it moves some things from the standard namespace into the global namespace, but... well anyway it makes things unclear.
So, use the standard headers and explicitly resolve the scope:
#include <cstdio> //I don't know why you need this either
#include <iostream>
#include <string>
std::string sMyStr;
int main()
{
sMyStr = "Hello";
return 0;
}
or, alternatively, use the standard namespace instead of the global namespace, so that the scope becomes implicit:
#include <cstdio> //I don't know why you need this either
#include <iostream>
#include <string>
using namespace std;
string sMyStr;
int main()
{
sMyStr = "Hello";
return 0;
}
namespaces can make things confusing, but they're pretty fly when you need them. They save all kinds of hassle in big projects and reusable code.
Wynd
May 8th, 2001, 05:37 PM
Why would they make their own header if the standard one is basically the same thing? Like you said, it would only serve to confuse people.
HarryW
May 8th, 2001, 06:57 PM
Yes, it would be unlike Microsoft to make something non-standard wouldn't it? :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.