这是我的头文件,auto vs static.h
#include "stdafx.h"
#include <iostream>
void IncrementAndPrint()
{
using namespace std;
int nValue = 1; // automatic duration by default
++nValue;
cout << nValue << endl;
} // nValue is destroyed here
void print_auto()
{
IncrementAndPrint();
IncrementAndPrint();
IncrementAndPrint();
}
void IncrementAndPrint()
{
using namespace std;
static int s_nValue = 1; // fixed duration
++s_nValue;
cout << s_nValue << endl;
} // s_nValue is not destroyed here, but becomes inaccessible
void print_static()
{
IncrementAndPrint();
IncrementAndPrint();
IncrementAndPrint();
}
这是我的主文件,namearray.cpp
#include "stdafx.h"
#include <iostream>
#include "auto vs static.h"
using namespace std;
int main(); // changing this to "int main()" (get rid of ;) solves error 5.
{
print_static();
print_auto();
cin.get();
return 0;
}
我正在尝试打印 (cout) 2 2 2 2 3 4
错误:
我觉得我的错误只是在头文件中使用了 void 。如何更改头文件以使我的代码正常工作?
参考:来自learncpp的代码