简体   繁体   中英

Visual Studio 2010 C++ not including files that are in #include

I'm fairly new to multiple file C++ programs, and I'm having an issue that I'm not sure I can even adequately explain. Here's the problem, one of my .cpp files is not letting me use any functions that are included in its #include list.

Here's what I did: First I wrote my code in main.cpp. Everything worked fine, it compiles and does exactly what I'm telling it to do. Now I'm trying to move that code into client.cpp, and I'm unable to declare strings, streams, or anything else that was working just fine in main.cpp.

Here's the code that worked just fine:

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <direct.h>
#include <string>

#define SAVE_FILE_LOC "C:\\Saves\\"
int main()        
{        

    ofstream saveFile;    
    string loc;    
    string userName;    
    printf("Please enter your user name:\n");    

    getline(cin, userName);    

    loc = SAVE_FILE_LOC;    
    loc = loc + userName;    
    if (_mkdir(loc.c_str()) == -1){    
        printf("Location Already Exists!\n");
    }    
    else{    
        loc = loc + "\\Profile.txt";
        saveFile.open(loc.c_str());
        saveFile << "Test";
        saveFile.close();
    }    
    return 0;    
}        

Now, the only thing that I did was right click on my "Source Files" folder (In VS) add a new .cpp file, name it client.cpp, copied and pasted the exact code above into the file, and now it doesn't work.

#include <stdio.h>        
#include <tchar.h>        
#include <iostream>        
#include <fstream>        
#include <direct.h>        
#include <string>        

#define SAVE_FILE_LOC "C:\\Saves\\"        

int login(void);        

int login(void)        
{        
    ofstream saveFile;    
    string loc;    
    string userName;    
    printf("Please enter your user name:\n");    

    getline(cin, userName);    

    loc = SAVE_FILE_LOC;    
    loc = loc + userName;    
    if (_mkdir(loc.c_str()) == -1){    
        printf("Location Already Exists!\n");
    }    
    else{    
        loc = loc + "\\Profile.txt";
        saveFile.open(loc.c_str());
        saveFile << "Test";
        saveFile.close();
    }    
    return 0;    
}        

I get 30 compile errors from the above code, here's an example:

Error   1   error C2065: 'ofstream' : undeclared identifier ***\Client.cpp  14  1   ConsoleApplication4

Error   2   error C2146: syntax error : missing ';' before identifier 'saveFile'    ***\Client.cpp  14  1   ConsoleApplication4

The compiler is telling me that now suddenly it can't create strings or streams or anything else. Note that I'm not getting any errors in the #include part of code, so it's not telling me that it can't find the libraries.

I have no idea what I need to even look for here in this case, why are my includes not working when I create a .cpp file that isn't named main?

Edit: Found the issue, main used using namespace std and I didn't have that line in client.cpp.

Names like string , ofstream from standard library need to be preceded with the namespace std:: , what you posted doesn't either have using namespace std; under the includes or std:: before classes/functions that you try to use (string, ofstream, getline)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM