【问题标题】:C++ Source code errorsC++ 源代码错误
【发布时间】:2016-05-30 16:49:18
【问题描述】:

有人可以告诉我如何修复错误吗?如果我将数组更改为 c++11 的向量会更好吗?我会得到什么好处?还有人可以告诉我是否有任何方法可以改进我的代码?

#include <iostream>
#include <fstream>
//#define long long 100000000;
//#define int 1000;

using namespace std;
void ts1(int *zone);
void ts2();
void ts3();
int p,n,m;
struct lazer{
    int x;
    int y;
    int direction;

};
//int z[4]={1,1,1,1}
int main()
{
   // int p,n;
   // long z;
    std::ifstream file;
    file.open("input.txt");
    file >> p;
    file >>n>>m;
    lazer Bem[m];
   // long * sir = new long[2*n];
   // long sir[2*n] ;
    int *zone[n][n];
    for (int i=0;i< n;i++)
        for (int j=0;j< n;j++)
            file>>zone[i][j];
        //file >> sir[i];

    for (int k=0;k<m;k++){
            file >> Bem[k].x;
            file >> Bem[k].y;
            file >> Bem[k].direction;
    }
    file.close();
    for(int z1=0;z1<m;z1++){
        switch(Bem[z1].direction){
            case 1:
                for(int zt=n-Bem[z1].x;zt<n;z1++)
                {

                    zone[zt][Bem[z1].y]=zone[zt][Bem[z1].y] -1;
                }
        }

    }


    if (p==1)ts1(zone);
    else ts2();
   // else ts3();
//    delete [] sir;
    return 0;
}
void ts1(int *zone){
    int gropi=0;
    static int* mutari[8][2]={{-1,0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};//{{-1,0},{0,1},{1,0},{0,-1}}
    for (int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int z=0;z<8;z++){
                int ni=i+mutari[z][0];
                int nj=j+mutari[z][1];
                if(zone[i][j] <= zone[ni][nj])gropi++;
            }

        }

    }

}
void ts2(){
    cout << "d";
}
void ts3(){
    cout << "d";
}

错误:

void ts1(int **zone){
    int gropi=0;
    static int mutari[8][2]={{-1,0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};//{{-1,0},{0,1},{1,0},{0,-1}}
    for (int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int z=0;z<8;z++){
                int ni=i+mutari[z][0];
                int nj=j+mutari[z][1];
                if(zone[i][j] <= zone[ni][nj])gropi++;
            }

        }

    }
    cout <<gropi;
}

在这部分cout &lt;&lt;gropi;会让exe停止。为什么?

【问题讨论】:

  • zone[i][j] 是一个指针(int*),mutari[z][0] 也是如此。你的意思是声明int zone[n][n]int mutari[8][2]
  • @molbdnilo 是的,谢谢老兄,(p==1)ts1(zone) 的错误呢;如何将整个指针作为参数传递?
  • 数组不是指针(指针也不是数组)。您不能将多维“VLA”数组(无论如何都不是标准的)作为参数传递,您需要一个指针数组。我建议你使用std::vector 而不是数组。
  • @molbdnilo 谢谢,但是数组不是指针怎么办? int*ds=int ds[]
  • 数组可以隐式转换为指向其第一个元素的指针。这并不意味着数组是指针,就像 int 可以转换为 float 并不意味着 int 是浮点数一样。

标签: c++ arrays file c++11 vector


【解决方案1】:

我还没有查看您的完整代码,但我查看了main() 方法。 我发现了几个问题:

lazer Bem[m];

你不能像这样声明数组,因为它在编译期间需要精确的大小。您需要使用指针来动态分配内存。

我对您的main() 方法做了一些修改;

//int z[4]={1,1,1,1}
int main()
{
   // int p,n;
   // long z;
    std::ifstream file("input.txt");

    file >> p;
    file >>n>>m;

    lazer* Bem = new lazer[m];
    int **zone = new int*[n];

    for (int i=0;i< n;i++)
    {
        zone[i] = new int[n];
        for (int j=0;j< n;j++)
        {
            file>>zone[i][j];
        }
    }
        //file >> sir[i];

    for (int k=0;k<m;k++){
            file >> Bem[k].x;
            file >> Bem[k].y;
            file >> Bem[k].direction;
    }
    file.close();
    for(int z1=0;z1<m;z1++){
        switch(Bem[z1].direction){
            case 1:
                for(int zt=n-Bem[z1].x;zt<n;z1++)
                {

                    zone[zt][Bem[z1].y]=zone[zt][Bem[z1].y] -1;
                }
        }

    }


    if (p==1)ts1(zone);
    else ts2();
   // else ts3();
//    delete [] sir;
    return 0;
}

您可以类似地修改其他方法。

希望对你有帮助。

【讨论】:

  • 非常感谢老兄:)
  • 但是如果我写 cout
猜你喜欢
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
相关资源
最近更新 更多