in file included from main / string does not name a type

Hello guys. I'm currently learning C++ nice and slowly. However, I've ran into a problem. I've tried to do a little with the string class, but I am failing utterly.
Hope anyone can see why I get these errors:

Line 3: In file included from main.cpp
Line 1 (header): 'string' does not name a type
Line 2 (header): 'string' does not name a type
In function 'int main(int, char**)':
Line 9: 'firstname undeclared (first use this function)
Line 12: 'surname' undeclared (first use this function)
[Build Error] [main.o] Error 1



main.cpp:
#include <cstdlib>
#include <iostream>
#include "name2.h"

using namespace std;

int main(int argc, char *argv[])
{
    cout << "Enter your first name: "<< endl;
    cin >> firstname;
    
    cout << "Enter your surname: "<< endl;
    cin >> surname;
    
    string name = firstname+" "+surname;
    cout << "Your name is " << name << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


name2.h:
string firstname;
string surname;
EDIT: Put using namespace std; before #include "name2.h"
Last edited on
Thanks! Weird that I didn't have to do this before.

I have another question, if you'd like to answer. How do I make it so the string of surname can take more than one word at a time? Like if my name was Peter John Johnson? Now it only says "Your name is Peter John", and not including Johnson.

/edit
Thats a bad example as I could just do another string for a middle name. Lets just assume that it was a part of the surname.
Last edited on
Thank you Destroyer!
Maybe you mean this?
std::getline(std::cin, name);
(Use it in place of the cin's)
Yes exactly Ess! Thanks a lot!
;D
Topic archived. No new replies allowed.