[英]Visual Studio 2010 C++ not including files that are in #include
我對多個文件C ++程序是相當陌生的,但是我遇到的問題是我不確定我是否可以充分解釋。 這就是問題所在,我的.cpp文件之一不允許我使用其#include列表中包含的任何功能。
這是我做的事情:首先,我在main.cpp中編寫了代碼。 一切工作正常,可以編譯並完全按照我的要求執行。 現在,我試圖將代碼移到client.cpp中,並且無法聲明字符串,流或其他在main.cpp中正常工作的東西。
這是正常工作的代碼:
#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;
}
現在,我唯一要做的就是右鍵單擊“源文件”文件夾(在VS中),添加一個新的.cpp文件,將其命名為client.cpp,將上面的確切代碼復制並粘貼到該文件中,現在它沒有不行
#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;
}
我從上面的代碼中獲得30個編譯錯誤,這是一個示例:
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
編譯器告訴我,現在突然無法創建字符串或流或其他任何內容。 請注意,在代碼的#include部分中沒有出現任何錯誤,因此它並不是在告訴我它找不到庫。
我什至不知道在這種情況下我什至需要在這里尋找什么,為什么當我創建一個未命名為main的.cpp文件時我的包含項不起作用?
編輯:發現了問題,主要using namespace std
,我在client.cpp中沒有該行。
標准庫中的諸如string
, ofstream
類的名稱必須以命名空間std::
,您發布的內容都沒有using namespace std;
在include或std::
下,嘗試使用的類/函數(字符串,ofstream,getline)之前
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.