需要帮助!!编译C ++程序 - 错误

时间:2011-03-10 04:46:26

标签: c++

这是一个我认为是C的例行程序。我将它(合法地)复制出来并试图让它在Visual Studio 2008中编译并运行。我想将它作为C ++程序保留。在IBM大型机汇编程序中有很多编程经验但在C ++中没有。非常感谢您的帮助。我想只是一些简单的改动,但我已经阅读了教程,并在这几个小时内打败了 - 无处可去。得到很多(4)错误C2440'=':在reedsolomon函数之后的语句中无法将'void *'转换为'int *'错误:非常感谢!计划如下:

#include <iostream>
using namespace std;
int wd[50] = {131,153,175,231,5,184,89,239,149,29,181,153,175,191,153,175,191,159,231,3,127,44,12,164,59,209,104,254,150,45};
int nd = 30, nc=20, i, j, k, *log, *alog, *c, gf=256, pp=301;  

/* The following is routine which calculates the error correction codewords
for a given data codeword string of length "nd", stored as an integer array wd[].
The function ReedSolomon()first generates log and antilog tables for the Galois
Field of size "gf" (in the case of ECC 200, 28) with prime modulus "pp"
(in the case of ECC 200, 301), then uses them in the function prod(), first to
calculate coefficients of the generator polynomial of order "nc" and then to
calculate "nc" additional check codewords which are appended to the data in wd[].*/

/* "prod(x,y,log,alog,gf)" returns the product "x" times "y" */

int prod(int x, int y, int *log, int *alog, int gf)
    {if (!x || !y) 
        return 0;
    else
        return alog[(log[x] + log[y]) % (gf-1)];
    }
/* "ReedSolomon(wd,nd,nc,gf.pp)" takes "nd" data codeword values in wd[] */
/* and adds on "nc" check codewords, all within GF(gf) where "gf" is a */
/* power of 2 and "pp" is the value of its prime modulus polynomial */

void ReedSolomon(int *wd, int nd, int nc, int gf, int pp) 
{int i, j, k, *log,*alog,*c;

/* allocate, then generate the log & antilog arrays: */
   log = malloc(sizeof(int) * gf);
   alog = malloc(sizeof(int) * gf);
   log[0] = 1-gf; alog[0] = 1;
   for (i = 1; i < gf; i++)
       {alog[i] = alog[i-1] * 2;
       if (alog[i] >= gf) alog[i] ^= pp;
          log[alog[i]] = i;
       }
/* allocate, then generate the generator polynomial coefficients: */
   c = malloc(sizeof(int) * (nc+1));
   for (i=1; i<=nc; i++) c[i] = 0; c[0] = 1;
   for (i=1; i<=nc; i++)
       {c[i] = c[i-1];
        for (j=i-1; j>=1; j--)
        {c[j] = c[j-1] ^ prod(c[j],alog[i],log,alog,gf);
        }
        c[0] = prod(c[0],alog[i],log,alog,gf);
       }
/* clear, then generate "nc" checkwords in the array wd[] : */
   for (i=nd; i<=(nd+nc); i++) wd[i] = 0;
   for (i=0; i<nd; i++)
       {k = wd[nd] ^ wd[i] ;
        for (j=0; j<nc; j++)
            {wd[nd+j] = wd[nd+j+1] ^ prod(k,c[nc-j-1],log, alog,gf);
            }
       }
    free(c);
    free(alog);
    free(log);
    return ();
}
int main ()
   {reedsolomon (50,30,20,256,301);
    for (i = 1; i < 51; i++)
    {cout<< i; "="; wd[i];}
    cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
    cin.get();
    return 1;
   }

4 个答案:

答案 0 :(得分:3)

在C ++中,无法将void指针隐式地转换为不同的指针。

所以而不是

int *pInt;
pInt = malloc(sizeof(int) * 5);

你需要说

int *pInt;
pInt = (int *) malloc(sizeof(int) * 5);

或者最好

int *pInt = new int[5];

(使用匹配的delete[]代替free),或者最好使用vector,如果它是动态的。

答案 1 :(得分:1)

在程序开头输入:#include <cstdlib>。如果您不包含此库,则malloc将不起作用。在C ++中,void * to int *不是自动转换,在行中:31 32和40你需要转换为int *例如:log =(int *)malloc(sizeof(int)* gf); 在主要功能,第63行,你将函数称为reedsolomon,它应该是你声明它的ReedSolomon。

另外,在“void ReedSolomon(int * wd,int nd,int nc,int gf,int pp)”中,当你在main中调用函数时,你会说ReedSolomon(50,30,20,256,301);所以你将一个int值设置为指向int的指针,这是一个类型碰撞。我不确定你想用wd做什么。

下次,请发布编译器的错误,以便人们不必自己编译代码来检查并查看错误。

同样是一项可以节省大量时间的好方法是对编译器给你的错误进行谷歌搜索(很可能有人已经犯了同样的错误),还读了一本C ++书来熟悉用语言。

干杯!

答案 2 :(得分:0)

C ++要求您将malloc的返回值强制转换为您指定给它的任何类型的指针。所以例如log = malloc(sizeof(int) * gf);需要成为log = (int *) malloc(sizeof(int) * gf);

答案 3 :(得分:0)

在分配指向malloc返回的指针时,应该输入强制转换。

示例:

log = reinterpret_cast<int*>(malloc(sizeof(int) * gf));