愚蠢的语法错误 c++

发布于 2024-12-15 14:54:19 字数 680 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我的痛♀有谁懂 2024-12-22 14:54:19

您正在声明您的数组,就好像这是 C# 一样。应该是

int profit[]

,否则

int *profit

你接下来就会碰到这个。您需要用分号来终止您的课程。

class Foo { 

};  <----

您遇到的下一个问题是逻辑问题,而不是语法问题。这并不像您想象的那样:

int N = sizeof(profit)/sizeof(decay); 

您使用的是 sizeof 两个指针,而不是数组的大小。您实际上有:

int N = 4/4  /* assumes sizeof int == 4 */

您还需要将您的大小传递给函数(或者,更好的是;停止使用数组并使用向量。)

当您采用“数组”时作为函数的参数,它实际上衰减为指向数组类型的指针(数组本身不能传递给函数)。因此,接下来

void Foo( int array[] ) {
    size_t arrSize = sizeof(array);
    // arrSize == 4 for a 32-bit system, i.e., sizeof(int*)

    int a[100];
    size_t actualSizeInBytes = sizeof(a);
    // actualSizeInBytes == 400, i.e., 4 * 100 as an int occupies 4 bytes
}

,这一行会导致您的第一次迭代始终被跳过。不确定这是否是故意的:

if (i == j) continue; 

You are declaring your arrays as if this were c#. It should be

int profit[]

Or

int *profit

You'll hit this one next. You need to terminate your class with a semi-colon.

class Foo { 

};  <----

The next problem you have is logical, not syntactic. This does not do what you think it does:

int N = sizeof(profit)/sizeof(decay); 

You are taking the sizeof two pointers, not the size of the arrays. You actually have:

int N = 4/4  /* assumes sizeof int == 4 */

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:

void Foo( int array[] ) {
    size_t arrSize = sizeof(array);
    // arrSize == 4 for a 32-bit system, i.e., sizeof(int*)

    int a[100];
    size_t actualSizeInBytes = sizeof(a);
    // actualSizeInBytes == 400, i.e., 4 * 100 as an int occupies 4 bytes
}

Next, this line causes your first iteration to always be skipped. Not sure if that is intentional:

if (i == j) continue; 
浪荡不羁 2024-12-22 14:54:19

您不需要像 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; 
  } 
};

编辑:还要记住 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.

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; 
  } 
};

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). Your N value will always equal 1.

请叫√我孤独 2024-12-22 14:54:19

这行是错误的:

 int determineProfit (int [] profit, int [] decay) { 

将其更改为:

int determineProfit (int profit[], int decay[]) { 

or

int determineProfit (int* profit, int* decay) { 

并添加结束 ;

如果您这样做并添加 main,当然:

int main() {}

那么您可以编译您的代码 - 我刚刚用 g++ 尝试过。

This line is wrong:

 int determineProfit (int [] profit, int [] decay) { 

Change it into:

int determineProfit (int profit[], int decay[]) { 

or

int determineProfit (int* profit, int* decay) { 

and add a closing ;

If you do that and add a main, of course:

int main() {}

then you can compile your code - I just tried it with g++.

書生途 2024-12-22 14:54:19

尝试 int certainProfit (int*profit, int*decay) 因为对于形式参数,数组和指针几乎是相似的。

Try int determineProfit (int* profit, int* decay) because for formal arguments, arrays and pointers are almost alike.

卖梦商人 2024-12-22 14:54:19

括号与变量名称相关联,而不是与类型相关联。第一行应该是

int determineProfit (int profit[], int decay[]) { 

Brackets are associated with the variable name, not the type. The first line should be

int determineProfit (int profit[], int decay[]) { 
鯉魚旗 2024-12-22 14:54:19

关于C 中的数组的教程可能会很有启发,特别是在数组参数的传递方面。

A tutorial on arrays in C may be enlightening, especially as regards the passing of array parameters.

茶底世界 2024-12-22 14:54:19
int determineProfit (int[] profit int [] decay 

这是你的错误 - 上面的陈述是错误的;应该是这样的

int determineProfit (int profit[], int decay[]) 
int determineProfit (int[] profit int [] decay 

here is your error - the above statement is wrong; it should be like this

int determineProfit (int profit[], int decay[]) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文