Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 15th, 2005, 9:10 AM   #1
bmw3027
Newbie
 
Join Date: Sep 2005
Posts: 1
Rep Power: 0 bmw3027 is on a distinguished road
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.
bmw3027 is offline   Reply With Quote
Old Sep 15th, 2005, 9:35 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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.
Polyphemus_ is offline   Reply With Quote
Old Sep 15th, 2005, 10:24 AM   #3
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 121
Rep Power: 0 L7Sqr is an unknown quantity at this point
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;
}
Although it is not set in stone that way.
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]
L7Sqr is offline   Reply With Quote
Old Sep 16th, 2005, 6:46 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC