2017-07-10 129 views
1

希望有人能幫助,因爲我迷路了。當運行我的程序時,它工作得很好,現在由於某種原因我得到錯誤信息預編譯頭文件的問題?

致命錯誤C1010:尋找預編譯頭時的文件意外結束。你忘了添加'#include「stdafx.h」'到你的源?

我確實包括了標題。目標是創建一個打印時間和日期的程序,並能夠輸入具有兩個不同文件的窗口大小以及字體和背景色。第一個文件如下。

#include "TimeClass.h" 
//////////////////////////////////////////////////////////////////////////// 
// Class to help with the usage of time. 

////////////////////////////////////////////////////////////////////////// 
CTimeClass::CTimeClass() 
{ 
// Notice that the time is stored whenever this time object is created. 
// Use this to your advantage in "main". 

// "m_st" holds the time and date information. 
GetLocalTime(&m_st); 
} 

//////////////////////////////////////////////////////////////////////// 
CTimeClass::~CTimeClass() 
{ 
// Empty - Nothing to destroy here. 
} 
short CTimeClass::Year() 
{ 
// Place your code here to return the year as a short. 
ConsoleTime ct; 
short year = ct.wYear; 
return year; 
} 

/////////////////////////////////////////////////////////////////////////// 
string CTimeClass::Month() 
{ 
ConsoleTime ct; 
int month = cm.wMonth; 
if (month == 0) 
    return "January"; 
else if (month == 1) 
    return "February"; 
else if (month == 2) 
    return "March"; 
else if (month == 3) 
    return "April"; 
else if (month == 4) 
    return "May"; 
else if (month == 5) 
    return "June"; 
else if (month == 6) 
    return "July"; 
else if (month == 7) 
    return "August"; 
else if (month == 8) 
    return "September"; 
else if (month == 9) 
    return "October"; 
else if (month == 10) 
    return "November"; 
else if (month == 11) 
    return "December"; 
else 
    return "error"; 
// Place your code here to return the month as a string. 
} 
////////////////////////////////////////////////////////////// 
short CTimeClass::Day() 
{ 
ConsoleTime ct; 
short day = ct.wDay; 
return day; 

// Place your code here to return the day as a short. 
} 
////////////////////////////////////////////////////////////////////// 
    string CTimeClass::DayOfWeek() 
{ 
ConsoleTime ct; 
int day == st.wDayOfWeek; 

if (day == 0) 
    return "Sunday"; 
else if (day == 1) 
    return "Monday"; 
else if (day == 2) 
    return "Tuesday"; 
else if (day == 3) 
    return "Wednesday"; 
else if (day == 4) 
    return "Thursday"; 
else if (day == 5) 
    return "Friday"; 
else if (day == 6) 
    return "Saturday"; 
else 
    return "error"; 
// Place your code here to return the day of the week as a string. 
} 
////////////////////////////////////////////////////////////////// 
short CTimeClass::Hour() 
{ 
ConsoleTime ct; 
short hour = ct.wHour; 
return hour; 
// Place your code here to return the hour as a short. 
} 
/////////////////////////////////////////////////////////////////////////// 
short CTimeClass::Minute() 
{ 
ConsoleTime ct; 
short minute = ct.wMinute; 
return minute; 
// Place your code here to return the minute as a short. 
} 
/////////////////////////////////////////////////////////////////////////// 
short CTimeClass::Second() 
{ 
ConsoleTime ct; 
short second = ct.wSecond; 
return second; 
// Place your code here to return the second as a short. 
} 

下面是我的第二個文件。

// ConsoleWindowClock.cpp : Defines the entry point for the console 
    application. 
// 
#include "stdafx.h" 

#include "ConsoleClass.h" 
#include "TimeClass.h" 
#include <iomanip> 
#include <iostream> 
#include <windows.h> 
#include <stdlib.h> 

using namespace std; 


int main() 
{ 
int width; 
int height; 
int fontsize; 
int text; 
int background; 
int userclass; 
int time; 

userclass.ConsoleColor(text, background); 
userclass.ConsoleWindowSize(width, height); 
userclass.FontSize(fontsize); 

cout << "This is the console window clock" << endl; 

cout << "Please enter the window size in characters: " << endl; 
cin >> width; 
cin >> height; 

cout << "Please enter the font size: " << endl; 
cin >> fontsize; 

cout << "Enter the Text color (0=Red, 1=Green, 2=Blue, -1=Random): " << 
endl; 
cin >> text; 

cout << "Enter the background color (0=Red, 1=Green, 2=Blue, -1=Random): " 
<< endl; 
cin >> background; 


while (true) 
    { 
    cout << "The time is: " << time.Hour() << ":" << time.Minute() << ":" << 
time.Second() << endl; 
    cout << "The day of the week is: " << time.DayOfWeek() << endl; 
    cout << "The month, day, and year are: " << time.Month() << " " << 
time.Day() << ", " << time.Year() << endl; 

    Sleep(1000); 

    } 


return 0; 
} 

任何幫助將不勝感激,抱歉的長篇。

+0

您是否將'stdafx.h'包含在兩個源文件中?第一個似乎不是這種情況。 – iehrlich

+0

我試過了,仍然是相同的問題 –

回答

0

您是否瀏覽了「TimeClass.h」?我看到了類似的錯誤,這源於.h文件。

我知道這應該是一個評論,但沒有足夠的聲望爲:(

+0

我做到了,不幸的是我沒有看到任何東西。奇怪的是,它運行的更早,然後我關閉了程序,重新打開它,現在發生了這種情況。 –

+0

請檢查#if/#ifdef是否與#endif匹配。很多時候我們錯過了一雙 – Harsh

+0

@CollinLynch,你有沒有檢查過[msdn](https://msdn.microsoft.com/en-us/library/d7fz9ckx(v = vs.90).aspx) – Harsh

0

你搞砸評論:

// ConsoleWindowClock.cpp : Defines the entry point for the console 
    application. 
// 

應該

// ConsoleWindowClock.cpp : Defines the entry point for the console 
// application. 

而且你應該在第一個文件中包含TimeClass.h之前的預編譯頭文件

#include "stdafx.h" 

#include "TimeClass.h"