C++ 从“int”到“int*”的无效转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2096381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid conversion from 'int' to 'int*'
提问by ggg
I was looking for solving a LCS problem (Longest common subsequence) and I tried to make my own code in c++ by referring to the explanation and the pascal code given at wikipedia.
我正在寻找解决 LCS 问题(最长公共子序列),并尝试通过参考维基百科给出的解释和 pascal 代码,用 C++ 编写自己的代码。
My final result was this:
我的最终结果是这样的:
#include <iostream>
#include <algorithm>
using namespace std;
int LCS(int a[100], int b[100], int m, int n);
int main()
{
int n, m, i, k, x[100], y[100];
cout << "n i m: " << endl;
cin >> n >> m;
cout << "n array: " << endl;
for(i=1;i<=n;i++)
cin >> x[i];
cout << "m array: " << endl;
for(i=1;i<=m;i++)
cin >> y[i];
k = LCS(x[100], y[100], m, n);
cout << k << endl;
return 0;
}
int LCS(int a[100], int b[100], int m, int n)
{
int c[m][n], i, j;
for(i=0;i<=m;i++)
c[i][0] = 0;
for(i=0;i<=n;i++)
c[0][i] = 0;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i] == b[j])
{
c[i][j] = c[i-1][j-1]+1;
}
else
c[i][j] = max(c[i][j-1], c[i-1][j]);
}
}
return c[m][n];
}
I tried to compile it via g++ and i got an error:
我尝试通过 g++ 编译它,但出现错误:
3.cpp: In function 'int main()':
3.cpp:19: error: invalid conversion from 'int' to 'int*'
3.cpp:19: error: initializing argument 1 of 'int LCS(int*, int*, int, int)'
3.cpp:19: error: invalid conversion from 'int' to 'int*'
3.cpp:19: error: initializing argument 2 of 'int LCS(int*, int*, int, int)'
I'm not really into c/c++ programming, and i want to know where is my mistake, why it happens and how to fix it. Thank you.
我不是很喜欢 c/c++ 编程,我想知道我的错误在哪里,为什么会发生以及如何修复它。谢谢你。
回答by Daniel A. White
Just pass the array name.
只需传递数组名称。
This
这个
LCS(x, y, m, n);
not this
不是这个
LCS(x[100], y[100], m, n);
回答by Kyle Lutz
Change k = LCS(x[100], y[100], m, n);
to k = LCS(x, y, m, n);
.
更改 k = LCS(x[100], y[100], m, n);
为 k = LCS(x, y, m, n);
。
回答by Yogesh Arora
The problem is in line
问题出在
k = LCS(x[100], y[100], m, n);
change it to
将其更改为
k = LCS(x, y, m, n);
回答by Andy Ross
Your code to call the function is mixed up:
您调用该函数的代码混淆了:
k = LCS(x[100], y[100], m, n);
This passes the 101st entry of x in the first argument, and the 101st entry of y in the second. Presumably the function wants you to pass the address of the array, so it should be LCS(x, y, m, n)
.
这将传递第一个参数中 x 的第 101 个条目,以及第二个参数中 y 的第 101 个条目。据推测,该函数希望您传递数组的地址,因此它应该是LCS(x, y, m, n)
.
回答by chakradhar pogiri
Change k = LCS(x[100], y[100], m, n);
to k = LCS(x, y, m, n);
.
更改 k = LCS(x[100], y[100], m, n);
为 k = LCS(x, y, m, n);
。
回答by Nikolai Fetissov
int LCS(int a[100], int b[100], int m, int n)
{
int c[m][n], i, j;
...
You cannot declare variable-length arrays on the stack (or anywhere) in C/C++ like that. You ether need to allocate them on the heap or use constant-length arrays.
您不能像那样在 C/C++ 中的堆栈(或任何地方)上声明可变长度数组。您需要在堆上分配它们或使用恒定长度的数组。
回答by Graeme Perrow
At least part of the problem is this line:
至少部分问题是这一行:
int c[m][n], i, j;
You can't use a variable to declare the size of an array. You will need to do something like:
您不能使用变量来声明数组的大小。您将需要执行以下操作:
int **c = new int[m][n];
Then at the end,
然后最后,
int ret = c[m][n];
delete [][] c;
return ret;
回答by Eli Bendersky
LCS accepts an array of 100 ints. You pass it a single int. This is invalid in C++. This is just one of many problems in this code, by the way. Just as an example, this:
LCS 接受 100 个整数的数组。你传递给它一个整数。这在 C++ 中无效。顺便说一下,这只是这段代码中的许多问题之一。举个例子,这个:
int c[m][n]
Is also invalid in C++.
在 C++ 中也是无效的。
If you're not familiar with a language, it's a good practice to start with small code snippets and build-up iteratively, step by step. It's a better way to gain understanding than to translate a large chunk of code from another language and just cross your fingers for it to compile and work.
如果您不熟悉一种语言,最好从小的代码片段开始,然后逐步迭代地构建。这是获得理解的更好方法,而不是从另一种语言翻译大量代码,然后交叉手指使其编译和工作。
回答by McAden
k = LCS(x[100], y[100], m, n);
should be
应该
k = LCS(x, y, m, n);
You should be passing in an array of int, but instead you're passing in the int that is at index 100.
您应该传入一个 int 数组,但您传入的是索引 100 处的 int。
There's a few other errors I'm seeing though.
不过,我还看到了一些其他错误。
for(i=1...
arrays are of index 0 in C/C++. for(i = 0...
数组在 C/C++ 中的索引为 0。对于(我= 0 ...
i<=n should be i
i<=n 应该是 i
And you're not doing any error checking for the bounds of your array for the user inputting n and m. If the user supplies 150 - you crash.
并且您没有对输入 n 和 m 的用户的数组边界进行任何错误检查。如果用户提供 150 - 你会崩溃。