C++一点疑问

来源:百度知道 编辑:UC知道 时间:2024/06/17 03:03:46
#include<iostream.h>

int n;
void fn();
void main()
{
n=20;
cout<<n<<endl;
fn();
}
#include<iostream.h>
static int n;
void fn()
{
n++;
cout<<n<<endl;

}

编译结果:
--------------------Configuration: xiaoping7 - Win32 Debug--------------------
Compiling...
xiaoping7.cpp
E:\C++\MSDev98\MyProjects\xiaoping7\xiaoping7.cpp(12) : error C2370: 'n' : redefinition; different storage class
E:\C++\MSDev98\MyProjects\xiaoping7\xiaoping7.cpp(3) : see declaration of 'n'
Error executing cl.exe.

xiaoping7.obj - 1 error(s), 0 warning(s)
这个是按照钱能的C++程序设计教程来写入的,但是我不知道哪个方面出了错误。我用的是 VC++6.0

#include<iostream.h>

static int n;
void fn();
void main()
{
n=20;
cout<<n<<endl;
fn();
}

void fn()
{
n++;
cout<<n<<endl;

}

n定义了两次(int n, static int n)

n重复定义啊

这是两个程序,你写在一起了,出现了两个对n的定义,拆开即可

目的是什么?
起初没有声明 using namespace std;
也没有在 cout 前加上 std::
怎么能用输出输入呢?

你起初定义的n是全域变数;可是下面 又用了static 定义了1次。我不知道你的目的 所以不知道为什么用了static 这里貌似没什么用。我给你改了一下程序 结果应该是 20 21

#include<iostream>
using namespace std;
int n;
void fn();
void main()
{
n=20;
cout<<n<<endl;
fn();
}

void fn()
{
n++;
cout<<n<<endl;
}