注册 登录
编程论坛 C++教室

这个抱错是怎么回事?

飞跃的乌龟 发布于 2013-04-14 19:43, 641 次点击
程序代码:
#include<iostream>
using namespace std;
int main()
{
    const int N=4;
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=30;j++)
            cout<<' ';
        for(int j=1;j<=8-2*i;j++)
            cout<<' ';
        for(int j=1;j<=2*i-1;j++)
            cout<<'*';
        cout<<endl;
    }
    for(int i=1;i<=N-1;i++)
    {
        for(int j=1;j<=30;j++)
            cout<<' ';
        for(int j=1;j<=7-2*i;j++)
            cout<<'*';
        cout<<endl;
    }
    return 0;
}

目的是输出一个图形
          *
        ***
      *****
    *******
    *****
    ***
    *
我是初学的,这段是书上抄的,抱错如下
--------------------Configuration: fwef - Win32 Debug--------------------
Compiling...
fwef.cpp
E:\VC++\fwef.cpp(10) : error C2374: 'j' : redefinition; multiple initialization
        E:\VC++\fwef.cpp(8) : see declaration of 'j'
E:\VC++\fwef.cpp(12) : error C2374: 'j' : redefinition; multiple initialization
        E:\VC++\fwef.cpp(8) : see declaration of 'j'
E:\VC++\fwef.cpp(16) : error C2374: 'i' : redefinition; multiple initialization
        E:\VC++\fwef.cpp(6) : see declaration of 'i'
E:\VC++\fwef.cpp(20) : error C2374: 'j' : redefinition; multiple initialization
        E:\VC++\fwef.cpp(18) : see declaration of 'j'
执行 cl.exe 时出错.
7 回复
#2
jiapengyun2013-04-14 19:53
我刚开始学这个,我觉得内循环的for中都有,int  j;  这句,也就是定义了多个变量都叫 j  ,而又没有重载,所以不合法。
#3
飞跃的乌龟2013-04-14 19:57
回复 2楼 jiapengyun
重载是什么
#4
rjsp2013-04-15 08:56
你的C++代码没有语法错误
错误的是你的编译器,太老旧了,不符合C++标准。
#5
xiaohui012013-04-15 09:31
换个编译器试试 啊
#6
ithaibo2013-04-15 12:28
VS2008运行通过
#7
rjsp2013-04-15 16:34
下班,写一个走人^_^
程序代码:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const int N = 4;

    for( int i=-N+1; i<N; ++i )
        cout << setfill(' ') << setw(i<0?-2*i:0) << "" << setfill('*') << setw(2*N-abs(i)*2) << "\n";

    return 0;
}

#8
jiapengyun2013-04-15 18:45
回复 3楼 飞跃的乌龟
就是同一个符号,不同的意思,比如  1+1  和  1.0+1.0   这两个式子中的加号,同一个符号,但前者是整型相加,后者是浮点相加,并不完全一样。系统根据加号前后的数据会调用不同的加号。      
我也是新手,具体到这段程序是不是这个问题我不确定。
1