愚蠢的语法错误 c++
我对 C++ 完全陌生。
我为这个错误而苦恼了一个多小时。想必有经验的人都能一眼看出来。
以下代码给出了错误:
class TimeTravellingCellar {
private:
public:
int determineProfit (int [] profit, int [] decay) {
int N = sizeof(profit)/sizeof(decay);
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) continue;
if (profit [i] - decay [j] > max)
max = profit [i] - decay [j];
}
}
return max;
}
}
Visual Studio Express 在 defineProfit
的参数中的 profit
下放置了一条红线,并表示:
在标识符利润之前需要一个 ')'< /代码>。
我将不胜感激一些帮助。 谢谢!
I'm completely new to C++.
Bashing my head against this error for over an hour. Probably someone with experience can see right through it.
The following code gives an error:
class TimeTravellingCellar {
private:
public:
int determineProfit (int [] profit, int [] decay) {
int N = sizeof(profit)/sizeof(decay);
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) continue;
if (profit [i] - decay [j] > max)
max = profit [i] - decay [j];
}
}
return max;
}
}
Visual Studio Express puts a red line under profit
in the parameters of determineProfit
and says:
expected a ')' before identifier profit
.
I will appreciate some help.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在声明您的数组,就好像这是 C# 一样。应该是
,否则
你接下来就会碰到这个。您需要用分号来终止您的课程。
您遇到的下一个问题是逻辑问题,而不是语法问题。这并不像您想象的那样:
您使用的是
sizeof
两个指针,而不是数组的大小。您实际上有:您还需要将您的大小传递给函数(或者,更好的是;停止使用数组并使用
向量
。)当您采用“数组”时作为函数的参数,它实际上衰减为指向数组类型的指针(数组本身不能传递给函数)。因此,接下来
,这一行会导致您的第一次迭代始终被跳过。不确定这是否是故意的:
You are declaring your arrays as if this were c#. It should be
Or
You'll hit this one next. You need to terminate your class with a semi-colon.
The next problem you have is logical, not syntactic. This does not do what you think it does:
You are taking the
sizeof
two pointers, not the size of the arrays. You actually have:You need to pass in the size of your to the function as well (or, better yet; stop using arrays and use a
vector<T>
.)When you take an "array" as an argument to your function it actually decays to a pointer to the array type (an array proper cannot be passed to a function). So it follows that:
Next, this line causes your first iteration to always be skipped. Not sure if that is intentional:
您不需要像 C++ 中那样声明数组,
[]
需要位于名称后面。另请注意,类声明后需要有一个分号。
编辑:还要记住 sizeof(pointer) 将返回指针类型的字节数,而不是数组中的元素数。因此,如果您有一个
int
数组,则sizeof(array) == sizeof(int)
。您的N
值将始终等于 1。You don't declare arrays like that in C++, the
[]
needs to go after the name.Also note you need to have a semicolon after the class declaration.
Edit: also remember that sizeof(pointer) will return the number of bytes of the pointer type, not the number of elements in the array. So if you have an
int
array,sizeof(array) == sizeof(int)
. YourN
value will always equal 1.这行是错误的:
将其更改为:
or
并添加结束
;
如果您这样做并添加 main,当然:
那么您可以编译您的代码 - 我刚刚用 g++ 尝试过。
This line is wrong:
Change it into:
or
and add a closing
;
If you do that and add a main, of course:
then you can compile your code - I just tried it with g++.
尝试 int certainProfit (int*profit, int*decay) 因为对于形式参数,数组和指针几乎是相似的。
Try
int determineProfit (int* profit, int* decay)
because for formal arguments, arrays and pointers are almost alike.括号与变量名称相关联,而不是与类型相关联。第一行应该是
Brackets are associated with the variable name, not the type. The first line should be
关于C 中的数组的教程可能会很有启发,特别是在数组参数的传递方面。
A tutorial on arrays in C may be enlightening, especially as regards the passing of array parameters.
这是你的错误 - 上面的陈述是错误的;应该是这样的
here is your error - the above statement is wrong; it should be like this