niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
again a newbie... :( compiler problems
Post new topic   Reply to topic Page 1 of 2 [19 Posts] View previous topic :: View next topic
Goto page:  1, 2 Next
Author Message
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jul 20, 2006 8:22 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

* Thorsten Kaben:
Quote:
'alloca' is not a standard C++ function. However, you don't need it for
what you're doing above, a fixed size stack allocation; you could just
use a local variable for that. And unless 'write_mail' changes the
contents of the buffer you don't even need that local variable; you
could do by simply using a literal string constant, e.g.

static char const text[] =
"bla bla"
"bla bla"
"bla bla";

it does not change the contents. you mean like this(?):

static char const text[] ="(-h000)<<< Alliance Watcher Info! >>>\r\r"
"You has more alliances closed or offered\r"
"as allowed within this game! Because of\r"
"this all your alliances are canceled!\r\r"
"Bye, The Gambler's Alliance Watcher.";

This OK, assuming that \r\r instead of \n is intentional.


Quote:
write_mail(i, text);

If the 'write_mail' second argument is not declared 'const', it should be.



Quote:
what is with the assigned mem if the function ends? is it then free?

It's a named constant. The memory is allocated once and for all when
the program starts.


Quote:
and what i have to do if the contents would be changed?

Use a variable insted of a constant.


Quote:
what about this(?):

void run_battle(void)
{
char* tmp_str[12];
char* text=(char*) alloca(250);

for (int i=1; i<=11; i++)
{
tmp_str[i]=(char*) alloca(100);
bzero(tmp_str[i],100);
}

If alloca had been a standard function you could use, this would be a
recipe for disaster, assuming 'tmp_str' is used for something.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 7:45 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
'alloca' is not a standard C++ function. However, you don't need it for
what you're doing above, a fixed size stack allocation; you could just
use a local variable for that. And unless 'write_mail' changes the
contents of the buffer you don't even need that local variable; you
could do by simply using a literal string constant, e.g.

static char const text[] =
"bla bla"
"bla bla"
"bla bla";

it does not change the contents. you mean like this(?):

static char const text[] ="(-h000)<<< Alliance Watcher Info! >>>\r\r"
"You has more alliances closed or offered\r"
"as allowed within this game! Because of\r"
"this all your alliances are canceled!\r\r"
"Bye, The Gambler's Alliance Watcher.";
write_mail(i, text);

what is with the assigned mem if the function ends? is it then free?
and what i have to do if the contents would be changed?

what about this(?):

void run_battle(void)
{
char* tmp_str[12];
char* text=(char*) alloca(250);

for (int i=1; i<=11; i++)
{
tmp_str[i]=(char*) alloca(100);
bzero(tmp_str[i],100);
}

bzero(text,250);

bzero does nothing more/less as filling the assigned mem with zeros.

and here comes this error: c++ forbids assignment of arrays

for (int i=1; i<=999; i++)
{
switch (ship_dat[i].wRace)
{
case 1 :ship_dat[i].sCode="501"; break;
case 2 :ship_dat[i].sCode="502"; break;
case 3 :ship_dat[i].sCode="503"; break;
case 4 :ship_dat[i].sCode="504"; break;
case 5 :ship_dat[i].sCode="505"; break;
case 6 :ship_dat[i].sCode="506"; break;
case 7 :ship_dat[i].sCode="507"; break;
case 8 :ship_dat[i].sCode="508"; break;
case 9 :ship_dat[i].sCode="509"; break;
case 10:ship_dat[i].sCode="510"; break;
case 11:ship_dat[i].sCode="511"; break;
}

ship_dat[i].wWarp=0;
ship_dat[i].wXWay=0;
ship_dat[i].wYWay=0;
ship_dat[i].wXShip=2000;
ship_dat[i].wYShip=2000;
ship_dat[i].wMission=4;
ship_dat[i].wEnemy=0;
ship_dat[i].wTowed=0;
if (!ship_dat[i].wFuel) ship_dat[i].wFuel=10;
ship_dat[i].wInterceptShip=0;
ship_xy[i].pos.x=2000;
ship_xy[i].pos.y=2000;
}
}

how could i fix this? there must be many changes in the gcc since my
last prog... Sad
Back to top
Default User
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1159

PostPosted: Thu Jul 20, 2006 6:27 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Thorsten Kaben wrote:


Quote:
void *alloca(size_t _size);

but why does "text=(char*) alloca(250); bzero(text,250);" not work?


That's a non-standard function, so's bzero() for that matter. About the
best we can tell you is to make sure you have the headers that your
documention specifies included.




Brian
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jul 20, 2006 6:13 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

* Thorsten Kaben:
Quote:
if (to_much[i])
{
text=(char*) alloca(250); bzero(text,250);
strcat(text, "(-h000)<<< Alliance Watcher Info! >>>\r\r");
strcat(text, "You has more alliances closed or offered\r");
strcat(text, "as allowed within this game! Because of\r");
strcat(text, "this all your alliances are canceled!\r\r");
strcat(text, "Bye, The Gambler's Alliance Watcher.");
write_mail(i, text);
}
}
write_auxdata_and_grey_hst();
}

but now: error: 'alloca' was not declared in this scope. i've looked
into the help and it says:

void *alloca(size_t _size);

but why does "text=(char*) alloca(250); bzero(text,250);" not work?

'alloca' is not a standard C++ function. However, you don't need it for
what you're doing above, a fixed size stack allocation; you could just
use a local variable for that. And unless 'write_mail' changes the
contents of the buffer you don't even need that local variable; you
could do by simply using a literal string constant, e.g.

static char const text[] =
"bla bla"
"bla bla"
"bla bla";

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 5:50 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

okay... i've fixed this problem, many thanks again to all! but now i've
the next. it looks that the function alloca is no more declared as in
the past. this code works in the past:

void check_allies(void)
{
int num[12];
bool to_much[12];

char* text;

for (int i=1; i<=11; i++)
{
to_much[i]=false;

if (gen_dat.wPlaying[i])
for (int j=1; j<=11; j++)
{
num[i]=0;

if (ally_offer(i, j)) num[i]++;
if (num[i]>gambler_cfg.alliance_num)
{
printf("Gambler: Alliance watcher ... race #%u has to much
allies!\n", i);
log("Alliance watcher: Race #% has to much allies!", i);
printf("Gambler: Alliance watcher ... deleting all alliances of
race #%u.\n", i);
log("Alliance watcher: Deleting all alliances of race #%.", i);
delete_alliances(i); to_much[i]=true; break;
}
}

if (to_much[i])
{
text=(char*) alloca(250); bzero(text,250);
strcat(text, "(-h000)<<< Alliance Watcher Info! >>>\r\r");
strcat(text, "You has more alliances closed or offered\r");
strcat(text, "as allowed within this game! Because of\r");
strcat(text, "this all your alliances are canceled!\r\r");
strcat(text, "Bye, The Gambler's Alliance Watcher.");
write_mail(i, text);
}
}
write_auxdata_and_grey_hst();
}

but now: error: 'alloca' was not declared in this scope. i've looked
into the help and it says:

void *alloca(size_t _size);

but why does "text=(char*) alloca(250); bzero(text,250);" not work?
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 4:42 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
And while you are there, change your struct declarations to C++ as Alf
suggested.

i'll do so! thank you!
Back to top
Ian
*nix forums Guru


Joined: 16 Aug 2005
Posts: 1615

PostPosted: Thu Jul 20, 2006 4:35 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Thorsten Kaben wrote:
Quote:
Is this supposed to be C or C++? If the latter , why all the typedefs?


c++

Sure looks like C to me.

Either way, you can't typedef a struct XX and then use 'struct XX' for a
variable, just use XX.


i'm sorry... must say that i don't understand what you want to say... i
think i had to define a struct before i can use it!? but excause my
knowledge... it's six years ago and i try to remember me at the
moment...

it would really help if someone could show me a way to fix the compile
errors with a example out of my code...

Just remove the 'struct' from the lines like


extern struct gcfg gambler_cfg;

And while you are there, change your struct declarations to C++ as Alf
suggested.

--
Ian Collins.
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 4:32 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
You haven't shown the definition for 'shipdata', the array element type,
so it's impossible to say whether it has a member wWarp.

okay... that could help me further! Smile many thanks for the first! i'll
try to fix the problems like you show me and report the result later! Wink
Back to top
Alf P. Steinbach
*nix forums Guru


Joined: 09 Mar 2005
Posts: 1855

PostPosted: Thu Jul 20, 2006 4:29 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

* Thorsten Kaben:
Quote:
the gambler.h

#ifndef GAMBLER_H
#define GAMBLER_H

#include "vgapdefs.h"

Error, no such header.


Quote:
// gambler.cfg
typedef struct
{
[snip]
} gcfg;

In C++, use the form

struct Name {};

instead of

typedef struct {} Name;

By the way, the above is C code, not C++ code, at least in spirit. In
C++, use constructors and destructors to initialize and clean up.


[snip]
Quote:
extern struct gcfg gambler_cfg;

Remove the word 'struct'.


[snip]

Quote:
the accel.cpp

#include <math.h
#include "gambler.h"

[snip]

void run_acc(int snum)
{
speed = ship_dat[snum].wWarp;

You haven't shown the definition for 'shipdata', the array element type,
so it's impossible to say whether it has a member wWarp.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 4:28 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
Is this supposed to be C or C++? If the latter , why all the typedefs?

c++

Quote:
Either way, you can't typedef a struct XX and then use 'struct XX' for a
variable, just use XX.

i'm sorry... must say that i don't understand what you want to say... i
think i had to define a struct before i can use it!? but excause my
knowledge... it's six years ago and i try to remember me at the
moment...

it would really help if someone could show me a way to fix the compile
errors with a example out of my code...
Back to top
Ian
*nix forums Guru


Joined: 16 Aug 2005
Posts: 1615

PostPosted: Thu Jul 20, 2006 4:18 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Thorsten Kaben wrote:
Quote:
Read the FAQ item, it says something about example code...

the accel.cpp

Is this supposed to be C or C++? If the latter , why all the typedefs?

Either way, you can't typedef a struct XX and then use 'struct XX' for a
variable, just use XX.

<snip>

--
Ian Collins.
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 3:38 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
You're mistaken. gcc six years ago was vastly different that it is
today. The current version conforms much more closely to the C++
standard than the older version. As a result, code that compiled on
your six-year old compiler may nonetheless be code that violates the
C++ standard and cannot be compiled on the newer compiler unless you
first fix the code. Good luck.

okay... that may be... but i know i had some compiler switches set in
the past. but i can't remember me... :(

perhaps can anybody help me to solve the problem!? many thanks!
Back to top
Default User
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1159

PostPosted: Thu Jul 20, 2006 3:34 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Thorsten Kaben wrote:

Quote:
Alf P. Steinbach wrote:

See the FAQ item "How do I post a question about code that doesn't
work correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

okay, i'm sorry about this! but i thought it was not really important
whitch version etc, because of i thought it could only be a switch.


What Alf means is, Show Us The Code!




Brian
Back to top
Thomas Tutone
*nix forums Guru Wannabe


Joined: 13 Jun 2005
Posts: 190

PostPosted: Thu Jul 20, 2006 3:29 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Thorsten Kaben wrote:

Quote:
Alf P. Steinbach wrote:

See the FAQ item "How do I post a question about code that doesn't work
correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

okay, i'm sorry about this! but i thought it was not really important
whitch version etc, because of i thought it could only be a switch.

What Alf is saying is that if you don't post the code, there is little
we can do to help you. (And make sure you keep the code _short_ and
_complete_.)

Quote:
rhide 1.5 / gcc 4.10 / make 3.791 / gdb 6.11 under winxp pro. i hope
it's enough. because there couldn't be a failure in the code - it was
compiled under the same env six years ago without problem. only the
versions of the gcc etc are different.

You're mistaken. gcc six years ago was vastly different that it is
today. The current version conforms much more closely to the C++
standard than the older version. As a result, code that compiled on
your six-year old compiler may nonetheless be code that violates the
C++ standard and cannot be compiled on the newer compiler unless you
first fix the code.

Good luck.

Best regards,

Tom
Back to top
Thorsten Kaben
*nix forums beginner


Joined: 20 Jul 2006
Posts: 9

PostPosted: Thu Jul 20, 2006 3:23 am    Post subject: Re: again a newbie... :( compiler problems Reply with quote

Quote:
Read the FAQ item, it says something about example code...

the accel.cpp

#include <math.h>
#include "gambler.h"

const double pi=3.1415926535;

struct t_pos {long x; long y;};
struct t_real_pos {double x; double y;};

t_real_pos disp_f;
t_pos ship, disp;

long start_x=0, start_y=0, way_x=0, way_y=0, delta_x=0, delta_y=0;
double heading=0, full_dist=0, travel_dist=0;
int speed=0;

long sign(double r)
{
if (r<0) return -1;
else
if (r>0) return 1;
return 0;
}

double rad_to_bogen(double Rad)
{
return ((Rad*360)/(2*pi));
}

void run_acc(int snum)
{
speed = ship_dat[snum].wWarp;
start_x = ship_dat[snum].wXShip;
start_y = ship_dat[snum].wYShip;
ship.x = ship_dat[snum].wXShip;
ship.y = ship_dat[snum].wYShip;
way_x = ship_dat[snum].wXWay;
way_y = ship_dat[snum].wYWay;
delta_x = way_x;
delta_y = way_y;
full_dist = sqrt((delta_x*delta_x)+(delta_y*delta_y));
travel_dist = (speed*speed*gambler_cfg.factor)-(speed*speed);

if (delta_x==0) heading=270-rad_to_bogen(atan(delta_x/delta_y));
if (delta_y==0) heading=270-rad_to_bogen(atan(delta_y/delta_x));

while (heading>360) heading-=360;

if (travel_dist<full_dist)
{
disp_f.x=travel_dist*cos(heading)*sign(travel_dist*cos(heading));
disp_f.y=travel_dist*sin(heading)*sign(travel_dist*sin(heading));

disp.x=long (disp_f.x)+sign(disp_f.x);
disp.y=long (disp_f.y)+sign(disp_f.y);

ship_dat[snum].wXShip=ship_xy[snum].pos.x=start_x+disp.x;
ship_dat[snum].wYShip=ship_xy[snum].pos.y=start_y+disp.y;

ship_dat[snum].wXWay=way_x+disp.x;
ship_dat[snum].wYWay=way_y+disp.y;
}
else
{
if ((ship_dat[snum].wXWay!=0) || (ship_dat[snum].wYWay!=0))
{

ship_dat[snum].wXShip=ship_xy[snum].pos.x=start_x+ship_dat[snum].wXWay;

ship_dat[snum].wYShip=ship_xy[snum].pos.y=start_y+ship_dat[snum].wYWay;
ship_dat[snum].wXWay=0;
ship_dat[snum].wYWay=0;
}
}
}

the gambler.h

#ifndef GAMBLER_H
#define GAMBLER_H

#include "vgapdefs.h"

// gambler.cfg
typedef struct
{
bool log;
bool host999;
bool phost;
bool defcon;
bool give_planet;
bool give_planet_clans;
bool market;
bool plumber;
bool starbank;
bool superbase;
bool trader;
bool wormholes;
bool wormholes_eat_ships;
bool alliance;
char alliance_num;
float factor;
int beam_sweep_mines;
int beam_sweep_webs;
int fighter_sweep_mines;
int fighter_sweep_webs;
bool only_colonies;
bool scoop_enemy;
unsigned int add_fighterbay_cost;
unsigned int add_tube_cost;
unsigned int add_weapon_cost;
unsigned int update_engine_cost;
unsigned int update_tube_cost;
unsigned int update_weapon_cost;
bool want_marketmail[12];
char first[51];
char last[51];
char markets;
bool centre_market;
unsigned int repair_damage;
unsigned int fill_crew;
unsigned int shit_call;
bool acc;
bool want_wormmail[12];
bool give_planet_allow_bird;
bool scoop_own;
bool trader_allow_ally_buy;
bool surrender;
} gcfg;

// gaccount.dat
typedef struct
{
unsigned long mc;
unsigned long su;
unsigned long ne;
unsigned long tr;
unsigned long du;
unsigned long mo;
unsigned long old_mc;
unsigned long old_su;
unsigned long old_ne;
unsigned long old_tr;
unsigned long old_du;
unsigned long old_mo;
} gaccount;

// gwhole.dat
typedef struct
{
char owner;
int x1;
int y1;
int damage1;
int x2;
int y2;
int damage2;
int number;
char visible1[12];
char visible2[12];
} gholedat;

// gtrader.dat
typedef struct
{
int owner;
int price;
} g_trader_dat;

// gdefcon.dat
typedef struct
{
char race[12];
} g_defcon_dat;

extern struct gcfg gambler_cfg;
extern struct gaccount account[12];
extern struct gholedat hole_dat[501];
extern struct auxdatahst aux_dat;
extern struct greytype grey_dat;
extern struct racenm race_name[12];
extern struct shipxy ship_xy[1001];
extern struct shipdata ship_dat[1001];
extern struct planetcoords planet_xy[501];
extern struct planetname planet_name[501];
extern struct planetdata planet_dat[501];
extern struct basedata base_dat[501];
extern struct minedata mine_dat[501];
extern struct rconfig ref_dat;
extern struct hullspec hull_dat[121];
extern struct beamspec beam_dat[11];
extern struct torpspec torp_dat[11];
extern struct engspec eng_dat[10];
extern struct ufodata ufo_dat[1001];
extern struct genhost gen_dat;
extern struct hconfig hconfig_dat;
extern struct g_trader_dat trader_dat[1001];
extern struct g_defcon_dat defcon_dat[12];

extern unsigned int trp; // Anzahl der Trader-Planet-FC's
extern unsigned int sur; // Anzahl der Surrender-Kommandos
extern unsigned int dc; // Anzahl der Defcon-FC's
extern UB2 ub2_dummy1, ub2_dummy2, ub2_dummy3, ub2_dummy4; // Dummys
extern bool overwrite; // Erstelle / berschreibe gaccount.dat!?

extern char* v;
extern char* homepath;
extern char* gamepath;
extern char* logfile;
extern int _argc;
extern char** _argv;

extern unsigned int turn;

void deinit_program(void);
void say_error(char* error);
double round(double wert);
int pos(char* str, char* sub_str);
char* add_slash(char* str);
char* prg_dir(void);
bool check_params(char* param);
bool bit_gesetzt(int zahl, int bit_nr);

#endif
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 2 [19 Posts] Goto page:  1, 2 Next
View previous topic :: View next topic
The time now is Thu Dec 04, 2008 1:57 am | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts Winbind problems for ADS authentication nlinley networking 1 Tue Sep 19, 2006 9:22 am
No new posts Anyone interested in a 4gl compiler ? Mike Aubury MySQL 1 Fri Jul 21, 2006 1:53 pm
No new posts problems using oddmuse with mod_perl2 inside apache2.2 pe... Fergus McMenemie Perl 0 Fri Jul 21, 2006 9:48 am
No new posts Problems with make-kpkg and skas patch Todd A. Jacobs Debian 0 Fri Jul 21, 2006 12:30 am

Mortgage Calculator | Loans | Mortgages | BabbFest | Cheap Loan
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2596s ][ Queries: 16 (0.0840s) ][ GZIP on - Debug on ]