• Advertisement

Archived

This topic is now archived and is closed to further replies.

Problems with borland command line compiler

This topic is 5365 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone tell me why this compiler generates an error whenever I use a global variable within a function? I always get "undefined symbol errors" because the compiler apparently thinks the variables havent been declared when it goes into the function. If I have to use all local variables I''m going to shoot myself...

Share this post


Link to post
Share on other sites
Advertisement
#include <iostream>
void func();
int main()
{
int x=0;
int y=0;
func();
return 0;
}

void func()
{
cout << x << y;
}

Gives me 2 undefined symbol errors after compile...one for x and one for y. It thinks they havent been defined even though they are global variables. Apparently it wants you to define shit locally, which I dont want to do.

P.S. No problem in VC++ with this...very strange.

Share this post


Link to post
Share on other sites
You have to define it before main for it to be global, otherwise you have to pass it in to your function.
to make them global ...

#include <iostream.h>
void func();
int x=0;
int y=0;
int main()
{

func();
return 0;
}

void func()
{
cout << x << y;
}

or to pass them into your function ...

#include <iostream.h>
void func(int y, int x);

int main()
{
int x=0;
int y=0;
func(y, x);
return 0;
}

void func(int y, int x)
{
cout << x << y;
}


An ASCII tetris clone... | AsciiRis


[edited by - Tiffany Smith on August 15, 2003 8:20:34 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by KevinG
Gives me 2 undefined symbol errors after compile...one for x and one for y. It thinks they havent been defined even though they are global variables. Apparently it wants you to define shit locally, which I dont want to do.


Thing is, those are local declarations. ^_^
They're inside main(), so they're local to main().
quote:

P.S. No problem in VC++ with this...very strange.


That is strange. That code has 2 distinct errors in it, so there's no way it should compile on any compiler... maybe you copied it wrong?

[edited by - twix on August 15, 2003 8:41:15 PM]

Share this post


Link to post
Share on other sites

  • Advertisement