Reputation: 951
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!
Upvotes: 2
Views: 438
Reputation: 124642
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;
Upvotes: 12
Reputation: 1
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[])
Upvotes: 0
Reputation: 6623
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.
Upvotes: 3
Reputation: 39284
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++.
Upvotes: 2
Reputation: 8895
A tutorial on arrays in C may be enlightening, especially as regards the passing of array parameters.
Upvotes: 0
Reputation: 5454
Brackets are associated with the variable name, not the type. The first line should be
int determineProfit (int profit[], int decay[]) {
Upvotes: 0
Reputation: 1
Try int determineProfit (int* profit, int* decay)
because for formal arguments, arrays and pointers are almost alike.
Upvotes: 1