Compile error, can someone have a look?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Laputa
New poster
Posts: 3
Joined: Sun Jan 29, 2006 9:17 pm

Compile error, can someone have a look?

Post by Laputa » Sun Jan 29, 2006 9:25 pm

I am moving from Java to C++ and this code gives error, please tell me where is the error:

Code: Select all

#include<iostream>
using namespace std;
int rank[1000000];

void main()
{
	long a, b, c;

	while (cin>>a>>b)
	{
		//Calculate & Track for max
		int max = 0;
		for (int i = a; i <= b; i++)
		{
			if (rank[i] == 0)
				rank[i] = findrank(i);
			if (max < rank[i])
				max = rank[i];
		}
		//Print out
		cout << max;
	}
}

int findrank(int& x)
{
	int rank, next;
	if (x == 1)
	{
		rank = 0;
	}
	else
	{
		if (x % 2 == 0)
			next = x/2;
		else
			next = x*3+1;

		if (next > 1000000 || rank[next] == 0)
			rank = 1 + findrank[next];
		else
			rank = 1 + rank[next];
	}

	if (x < 1000001)
		rank[x] = rank;

	return rank;
}
Last edited by Laputa on Tue Jan 31, 2006 2:48 am, edited 1 time in total.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Mon Jan 30, 2006 4:26 am

Code: Select all

rank = 1 + rank[next];
It can't be compiled even in your local machine, right?

neno_uci
Experienced poster
Posts: 104
Joined: Sat Jan 17, 2004 12:26 pm
Location: Cuba

Post by neno_uci » Mon Jan 30, 2006 4:31 am

Here are your errors according to my compiler(g++):

Code: Select all

laputa.cc:5: error: `main' must return `int'
laputa.cc: In function `int main(...)':
laputa.cc:16: error: `findrank' undeclared (first use this function)
laputa.cc:16: error: (Each undeclared identifier is reported only once for each function it appears in.)
laputa.cc: In function `int findrank(int&)':
laputa.cc:26: error: `int findrank(int&)' used prior to declaration
laputa.cc:39: error: invalid types `int[int]' for array subscript
laputa.cc:40: error: pointer to a function used in arithmetic
laputa.cc:40: error: pointer to a function used in arithmetic
laputa.cc:40: error: invalid conversion from `int (*)(int&)' to `int'
laputa.cc:42: error: invalid types `int[int]' for array subscript
laputa.cc:46: error: invalid types `int[int]' for array subscript

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Mon Jan 30, 2006 6:46 am

Your rank array is declared in main function and you are trying to access it in your findrank function. Just like in Java, you cannot access another method's local variables. Maybe you would like to describe what you intended to do with this code?

Laputa
New poster
Posts: 3
Joined: Sun Jan 29, 2006 9:17 pm

Post by Laputa » Mon Jan 30, 2006 9:27 pm

It's my very first try to the 3n+1 problem. I see that I should move the rank arrays into the beginning of the class. Is there anything else I need to fix?

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Tue Jan 31, 2006 5:00 am

Did you read neno_uci's post?

Fix those errors as well as the one I pointed out.

Laputa
New poster
Posts: 3
Joined: Sun Jan 29, 2006 9:17 pm

Post by Laputa » Thu Feb 02, 2006 7:04 am

Thank you for all trying. I bring the code up to my tutor and he helped me fixed it. By the way, I know that the code is wrong already. I just didn't know how to fix it. The compiler told me I had these errors already.

Post Reply

Return to “C++”