compiler help?

demagogue

Baseband Member
Messages
21
Hello, I'm trying to compile a small test program I've written to A) see if I remember anything of c++, and B) get used to using gcc to compile. The program has 7 lines of code, which will ask for a name, and return it. here's the program:

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

//those two are there becasue I don't exactly know which header file I need to use.

main ()
{
string FirstName="";
string LastName="";
//initialized that way because if I don't do it that way, gcc returns a "FirstName not initialized in this scope" error.

cout<<"What is the first name?";
cin>>FirstName;
cout<<"What is the last name?";
cin>>LastName;
cout<<"The name is "<<Firstname<<" "<<Lastname;
return 0;

}

When I runn gcc with the -c option, it compiles fine. But then, when I take that object file, and try to compile it into a program using the -o option, I get a bunch of "Unreferenced something or other , std::cout" and "cin" errors. Any ideas what could be causing it? And I don't have a ready file with all the errors in it, if anyone could tell me how to send the errors to a file so I can read them later, it would be much appreciated.
 
Ok, first thing first, there is no point in including the same file twice. both of those includes are the same. Some compilers require the .h others do not.

the std errors are probaly related to you not including the standard library. to do this just do

#include<stdlib.h> again, the .h may not be required.
 
yes, you must change it to "int main()"
you can remove one of the included iostreams (probably "#include<iostream.h>")
and because you are using strings you need to add "#include <string>" at the top

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

int main()
{
string FirstName;
string LastName;

cout<<"What is the first name? ";
cin>>FirstName;
cout<<"What is the last name? ";
cin>>LastName;
cout<<"The name is "<<Firstname<<" "<<Lastname;
return 0;
}
 
Back
Top Bottom