我有一个简单的测试控制台应用程序。我正在尝试使用在simple.h文件中定义的nike2函数,实现在simple.cpp文件中。simple.h和simple.cpp文件都与主项目在不同的目录中。
我已将simple.h添加到“头文件”中,将simple.cpp添加到“源文件”中,这样做是否有必要我不确定。
控制台应用程序:
#include "stdafx.h"
#include "..\..\simple.h"
int _tmain(int argc, _TCHAR* argv[])
{
nike2(5);
return 0;
}
Simple.h
#include <cstdlib>
#include <iostream>
#ifndef MEMORY
#define MEMORY
int var;
int nike2(int f);
#endif /*MEMORY*/
Simple.cpp
#include <cstdlib>
#include <iostream>
#include "simple.h"
int nike2(int f)
{
return 0;
}
编译时我遇到了错误:
Error 4 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source? c:\c\simple.cpp 11 1 usingHeader
为什么要使用神奇的"StdAfx.h"?
更新:
现在simple.cpp看起来是这样的,但仍然有相同的错误。
#include "C:\c\usingHeader\usingHeader\stdafx.h"
#include <cstdlib>
#include <iostream>
#include "simple.h"
int nike2(int f)
{
return 0;
}
#include "C:\c\usingHeader\usingHeader\stdafx.h"
的内容吗? - vico#include "stdafx.h"
即可。该文件将位于您的项目目录中,不需要完整路径。 - Bo Persson