![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 1
Rep Power: 0
![]() |
local function definitions are illegal, small prog. help appreciated
I am working on this small program and everything looks fine, but I get a "local function definitions are illegal" for all of my methods. All of the errors occur in this file I have pasted here.
#include "GSS.h"; int main(){ int GeometricSeriesSum( int x,unsigned int n) { int sum = 0; for (unsigned int i = 0; i <= n; ++i){ int prod = 1; for (unsigned int j = 0; j < i; ++j) prod *= x; sum += prod; } return sum; } int CalcGSByHorner(int x, unsigned int n){ int sum = 0; for (unsigned int i = 0; i <= n; ++i){ sum = 1 + x*sum; } return sum; } int CalcCNRecursively(int x, int n){ if(n==0) return 1; else if (n%2==0) return CalcXNRecursively(x*x, n/2); else return x*CalcXNRecursively(x*x, n/2); } int CalcGSUsingClosedForm(int x, unsigned int n){ if (x==1) return n+1; else return (CalcXNRecursively(x, n + 1) - 1) / (x - 1); } return 0; } These are the exact errors that I get: GSS.cpp(5) : error C2601: 'GeometricSeriesSum' : local function definitions are illegal GSS.cpp(17) : error C2601: 'CalcGSByHorner' : local function definitions are illegal GSS.cpp(25) : error C2601: 'CalcCNRecursively' : local function definitions are illegal GSS.cpp(34) : error C2601: 'CalcGSUsingClosedForm' : local function definitions are illegal Last edited by bmw3027; Sep 15th, 2005 at 9:23 AM. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
firstly, welcome to the forums
![]() secondly, use code tags next time You have put all your functions in the main function. you have to put them outside your main function. When a program starts, the main function is called, which calls the functions again and does the stuff it has to do. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 116
Rep Power: 0
![]() |
The general format of a single file would look similar to:
/* function declarations */
void foo ();
int bar (int);
/* definition of main */
int main (int argc, char ** argv)
{
/* ... */
return 0;
}
/* function definitions */
void foo ()
{
/* ... */
}
int bar (int val)
{
/* ... */
return something;
}You can define all your function before main (without prior declaration) or not declare a helper function until after main (so long as it is declared before it is used). The above method is just a common way.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i'm just gonna pull a response out of my ass because i'm not going to read your code (cuz sans tags and all...) but are you including a header file that you've written yourself without giving US the code to look at? if not cool, i sure as hell don't know the whole STL and maybe there is a GSS.h, if not, please post that code as well.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|