• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 00:50
CET 06:50
KST 14:50
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets3$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)15Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns7[BSL21] Non-Korean Championship - Starts Jan 103SC2 All-Star Invitational: Jan 17-1823
StarCraft 2
General
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets SC2 All-Star Invitational: Jan 17-18 When will we find out if there are more tournament SC2 Spotted on the EWC 2026 list? Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns
Tourneys
$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7) $25,000 Streamerzone StarCraft Pro Series announced WardiTV Winter Cup WardiTV Mondays SC2 AI Tournament 2026
Strategy
Simple Questions Simple Answers
Custom Maps
Map Editor closed ?
External Content
Mutation # 508 Violent Night Mutation # 507 Well Trained Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes
Brood War
General
[ASL21] Potential Map Candidates Potential ASL qualifier breakthroughs? A cwal.gg Extension - Easily keep track of anyone BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion
Tourneys
[Megathread] Daily Proleagues [BSL21] Grand Finals - Sunday 21:00 CET [BSL21] Non-Korean Championship - Starts Jan 10 SLON Grand Finals – Season 2
Strategy
Game Theory for Starcraft Simple Questions, Simple Answers Current Meta [G] How to get started on ladder as a new Z player
Other Games
General Games
Beyond All Reason Nintendo Switch Thread Awesome Games Done Quick 2026! Mechabellum Stormgate/Frost Giant Megathread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread European Politico-economics QA Mega-thread Trading/Investing Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List TL+ Announced
Blogs
My 2025 Magic: The Gathering…
DARKING
Physical Exercise (HIIT) Bef…
TrAiDoS
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2075 users

The Big Programming Thread - Page 195

Forum Index > General Forum
Post a Reply
Prev 1 193 194 195 196 197 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Nausea
Profile Joined October 2010
Sweden807 Posts
Last Edited: 2012-11-15 14:03:14
November 15 2012 13:59 GMT
#3881
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.
Set it ablaze!
FFGenerations
Profile Blog Joined April 2011
7088 Posts
November 15 2012 14:15 GMT
#3882
heya

im sick and tired of it

there is just 1 major thing

i have


float subtotal = totalWallsPaintedCost+totalWallpaperCost+totalWoodworkCost;
float subtotalVat = subtotal*vat;
float finalTotal=subtotal+subtotalVat;

later

cout<<"SUB TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotal<<endl;
cout<<"VAT @ 20% "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotalVat<<endl;
cout<<"FINAL TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<finalTotal<<endl<<endl;


it returns
SUB TOTAL £-1.#R
VAT @ 20% £-1.#R
FINAL TOTAL £-1.#R

could someone please give me a hint what this damn £-1.#R is

it was working for 5 seconds on one compile but then i started control-y ing to undo/redo and eventually realised contol-y doesnt redo in my compiler but instead DELETES LINES

full code c++

+ Show Spoiler +
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
float glossCost = 4.00;
float emulsionCost = 3.50;
float doorPaintRequirement = 1;
float windowPaintRequirement = 0.5;
float vat = 0.2;
int start = 0;
int goAgain;
float roomLength;
float roomHeight;
float roomWidth;
string customerName;
char wallsPainted;
int wallpaperRollCount;
float wallpaperRollCost;
int doorNumber;
int windowNumber;



cout<<"Welcome to Blake's Estimator."<<endl;
cout<<"1 - Begin estimate."<<endl;
cout<<"2 - Display instructions."<<endl;
cin>>start;

if (start == 2)
{
cout<<endl<<"This program will calculate the estimated cost of decorating a room based on "<<endl<<"the input you provide. Units of length are in centimetres. Quantity of "<<endl<<"wallpaper rolls must be a whole number."<<endl;
cout<<endl<<"Enter '1' to continue."<<endl;
cin>>start;
while (start !=1)
{
cout<<"Enter '1' to continue."<<endl;
cin>>start;
}
}

if (start == 1)
{
do
{
cout<<"Enter customer name."<<endl;
cin>>customerName;

char roomLengthInput[256];
cout<<"Enter room length."<<endl;
cin>>roomLengthInput;
roomLength = atoi (roomLengthInput);

while (roomLength < 45)
{
cout<<"Please enter a numerical room length in cm greater than 45."<<endl;
cin>>roomLengthInput;
roomLength = atoi (roomLengthInput);
}

char roomWidthInput[256];
cout<<"Enter room width."<<endl;
cin>>roomWidthInput;
roomWidth = atoi (roomWidthInput);

while (roomWidth < 45)
{
cout<<"Please enter a numerical room width in cm greater than 45."<<endl;
cin>>roomWidthInput;
roomWidth = atoi (roomWidthInput);
}

char roomHeightInput[256];
cout<<"Enter room height."<<endl;
cin>>roomHeightInput;
roomHeight = atof (roomHeightInput);

while (roomHeight < 45)
{
cout<<"Please enter a numerical room height in cm greater than 45."<<endl;
cin>>roomHeightInput;
roomHeight = atof (roomHeightInput);
}



cout<<"Are walls painted (p) or wallpapered (w)?"<<endl;
cin>>wallsPainted;

while (wallsPainted !='p' && wallsPainted !='w')
{
cout<<"Enter 'p' if walls are painted or 'w' if walls are wallpapered."<<endl;
cin>>wallsPainted;
}

if (wallsPainted == 'w')
{
char wallpaperRollCountInput[256];
cout<<"Enter required number of wallpaper rolls."<<endl;
cin>>wallpaperRollCountInput;
wallpaperRollCount = atoi (wallpaperRollCountInput);

while (wallpaperRollCount < 1)
{
cout<<"Please enter required number of wallpaper rolls as a whole number."<<endl;
cin>>wallpaperRollCountInput;
wallpaperRollCount = atoi (wallpaperRollCountInput);
}

char wallpaperRollCostInput[256];
cout<<"Enter cost of each roll."<<endl;
cin>>wallpaperRollCostInput;
wallpaperRollCost = atof (wallpaperRollCostInput);

while (wallpaperRollCost < 1)
{
cout<<"Please enter cost of wallpaper rolls as a whole number."<<endl;
cin>>wallpaperRollCostInput;
wallpaperRollCost = atof (wallpaperRollCostInput);
}
}

char doorNumberInput[256];
cout<<"Enter number of doors."<<endl;
cin>>doorNumberInput;
doorNumber = atof (doorNumberInput);

while (doorNumber < 1)
{
cout<<"Please number of doors as a whole number."<<endl;
cin>>doorNumberInput;
doorNumber = atof (doorNumberInput);
}

char windowNumberInput[256];
cout<<"Enter number of windows."<<endl;
cin>>windowNumberInput;
windowNumber = atof (windowNumberInput);

while (windowNumber < 1)
{
cout<<"Please number of windows as a whole number."<<endl;
cin>>windowNumberInput;
windowNumber = atof (windowNumberInput);
}

float ceilingLitres = (roomLength/100)*(roomWidth/100)/8;
float ceilingCosts = ceilingLitres*emulsionCost;
float wallsLitres = ((roomLength/100*roomHeight/100)+(roomWidth/100*roomHeight/100)*2)-(doorNumber*2)-(windowNumber*0.5);
float totalWallsPaintedCost = wallsLitres*emulsionCost;
float totalWallpaperCost = wallpaperRollCount*wallpaperRollCost;
float woodworkLitres = (doorNumber*doorPaintRequirement)+(windowNumber*windowPaintRequirement);
float totalWoodworkCost = woodworkLitres*glossCost;
float subtotal = totalWallsPaintedCost+totalWallpaperCost+totalWoodworkCost;
float subtotalVat = subtotal*vat;
float finalTotal=subtotal+subtotalVat;

cout << string(50, '\n');
cout<<endl<<endl<<"Joe Stevens Painting & Decorating Tel: 01522 522522"<<endl;
cout<<endl<<endl<<"Estimate costs for "<<customerName<<"."<<endl<<endl;
cout<<"MATERIALS"<<endl;
cout<<" Ceiling: "<<setiosflags(ios::fixed)<<setprecision(2)<<ceilingLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<emulsionCost<<" per litre "<<(char)156<<ceilingCosts<<endl;

switch (wallsPainted)
{
case 'p':
cout<<" Walls: "<<setiosflags(ios::fixed)<<setprecision(2)<<wallsLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<emulsionCost<<" per litre "<<(char)156<<totalWallsPaintedCost<<endl;
break;
case 'w':
cout<<" Walls: "<<setiosflags(ios::fixed)<<setprecision(2)<<wallpaperRollCount<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<wallpaperRollCost<<" per roll "<<(char)156<<totalWallpaperCost<<endl;
break;
}

cout<<" Woodwork: "<<setiosflags(ios::fixed)<<setprecision(2)<<woodworkLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<glossCost<<" per litre "<<(char)156<<totalWoodworkCost<<endl<<endl;

cout<<"SUB TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotal<<endl;
cout<<"VAT @ 20% "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotalVat<<endl;
cout<<"FINAL TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<finalTotal<<endl<<endl;

char goAgainInput[256];
cout << string(6, '\n');
cout<<"What would you like to do now?"<<endl;
cout<<"1 - Begin a new estimate."<<endl;
cout<<"2 - Exit the program."<<endl;
cin>>goAgainInput;
goAgain = atoi (goAgainInput);

while (goAgain != 1 && goAgain != 2)
{
cout<<"Please enter 1 to begin again or 2 to quit."<<endl;
cin>>goAgainInput;
goAgain = atof (goAgainInput);
}

} while (goAgain == 1);

system("PAUSE");
return EXIT_SUCCESS;
}
}


thankyou
Cool BW Music Vid - youtube.com/watch?v=W54nlqJ-Nx8 ~~~~~ ᕤ OYSTERS ᕤ CLAMS ᕤ AND ᕤ CUCKOLDS ᕤ ~~~~~~ ༼ ᕤ◕◡◕ ༽ᕤ PUNCH HIM ༼ ᕤ◕◡◕ ༽ᕤ
Isualin
Profile Joined March 2011
Germany1903 Posts
November 15 2012 14:31 GMT
#3883
i have a question about "goto" statement. im pretty sure my teacher told me never use them. i know how they work and see some people use it. is there a reason not to use them except readability?
| INnoVation | The literal god TY | ByuNjwa | LRSL when? |
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
November 15 2012 14:34 GMT
#3884
On November 15 2012 23:31 Isualin wrote:
i have a question about "goto" statement. im pretty sure my teacher told me never use them. i know how they work and see some people use it. is there a reason not to use them except readability?

The only real reason is that there's never a reason to use goto statements if you use other constructs properly, and NOT using goto makes it far easier to find bugs and read the code. So yes, it's more or less just about readability, but it's a pretty big factor, goto statements are usually a crutch which is not worth getting used to.
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
November 15 2012 14:39 GMT
#3885
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
November 15 2012 14:44 GMT
#3886
On November 15 2012 23:31 Isualin wrote:
i have a question about "goto" statement. im pretty sure my teacher told me never use them. i know how they work and see some people use it. is there a reason not to use them except readability?


+ Show Spoiler +
The reason why you shouldn't use goto ever is because if you type it and i see it, i will personally hunt you down and strangle you until you bleed out of your eyes like all the poor programmers that lost their souls debugging code using goto.


It makes code impossible to read and understand and there is just a single case where goto has a use but i won't tell you which case that is because you will never get across that use case, so you should never use it. Ever.
Nausea
Profile Joined October 2010
Sweden807 Posts
November 15 2012 14:47 GMT
#3887
On November 15 2012 23:39 netherh wrote:
Show nested quote +
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.


The X var is only defined in C_Entity, neither C_Player or C_PlayerShade defines it, they just use it through C_Entity.
Set it ablaze!
kaykaykay
Profile Joined July 2012
Singapore637 Posts
November 15 2012 14:55 GMT
#3888
Is there anybody decent with Object oriented programing eclipse java here? :D
Starve the ego, feed the soul.
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
November 15 2012 15:03 GMT
#3889
On November 15 2012 23:15 FFGenerations wrote:
heya

im sick and tired of it

there is just 1 major thing

i have


float subtotal = totalWallsPaintedCost+totalWallpaperCost+totalWoodworkCost;
float subtotalVat = subtotal*vat;
float finalTotal=subtotal+subtotalVat;

later

cout<<"SUB TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotal<<endl;
cout<<"VAT @ 20% "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotalVat<<endl;
cout<<"FINAL TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<finalTotal<<endl<<endl;


it returns
SUB TOTAL £-1.#R
VAT @ 20% £-1.#R
FINAL TOTAL £-1.#R

could someone please give me a hint what this damn £-1.#R is

it was working for 5 seconds on one compile but then i started control-y ing to undo/redo and eventually realised contol-y doesnt redo in my compiler but instead DELETES LINES

full code c++

+ Show Spoiler +
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[]
{
float glossCost = 4.00;
float emulsionCost = 3.50;
float doorPaintRequirement = 1;
float windowPaintRequirement = 0.5;
float vat = 0.2;
int start = 0;
int goAgain;
float roomLength;
float roomHeight;
float roomWidth;
string customerName;
char wallsPainted;
int wallpaperRollCount;
float wallpaperRollCost;
int doorNumber;
int windowNumber;



cout<<"Welcome to Blake's Estimator."<<endl;
cout<<"1 - Begin estimate."<<endl;
cout<<"2 - Display instructions."<<endl;
cin>>start;

if (start == 2)
{
cout<<endl<<"This program will calculate the estimated cost of decorating a room based on "<<endl<<"the input you provide. Units of length are in centimetres. Quantity of "<<endl<<"wallpaper rolls must be a whole number."<<endl;
cout<<endl<<"Enter '1' to continue."<<endl;
cin>>start;
while (start !=1)
{
cout<<"Enter '1' to continue."<<endl;
cin>>start;
}
}

if (start == 1)
{
do
{
cout<<"Enter customer name."<<endl;
cin>>customerName;

char roomLengthInput[256];
cout<<"Enter room length."<<endl;
cin>>roomLengthInput;
roomLength = atoi (roomLengthInput);

while (roomLength < 45)
{
cout<<"Please enter a numerical room length in cm greater than 45."<<endl;
cin>>roomLengthInput;
roomLength = atoi (roomLengthInput);
}

char roomWidthInput[256];
cout<<"Enter room width."<<endl;
cin>>roomWidthInput;
roomWidth = atoi (roomWidthInput);

while (roomWidth < 45)
{
cout<<"Please enter a numerical room width in cm greater than 45."<<endl;
cin>>roomWidthInput;
roomWidth = atoi (roomWidthInput);
}

char roomHeightInput[256];
cout<<"Enter room height."<<endl;
cin>>roomHeightInput;
roomHeight = atof (roomHeightInput);

while (roomHeight < 45)
{
cout<<"Please enter a numerical room height in cm greater than 45."<<endl;
cin>>roomHeightInput;
roomHeight = atof (roomHeightInput);
}



cout<<"Are walls painted (p) or wallpapered (w)?"<<endl;
cin>>wallsPainted;

while (wallsPainted !='p' && wallsPainted !='w')
{
cout<<"Enter 'p' if walls are painted or 'w' if walls are wallpapered."<<endl;
cin>>wallsPainted;
}

if (wallsPainted == 'w')
{
char wallpaperRollCountInput[256];
cout<<"Enter required number of wallpaper rolls."<<endl;
cin>>wallpaperRollCountInput;
wallpaperRollCount = atoi (wallpaperRollCountInput);

while (wallpaperRollCount < 1)
{
cout<<"Please enter required number of wallpaper rolls as a whole number."<<endl;
cin>>wallpaperRollCountInput;
wallpaperRollCount = atoi (wallpaperRollCountInput);
}

char wallpaperRollCostInput[256];
cout<<"Enter cost of each roll."<<endl;
cin>>wallpaperRollCostInput;
wallpaperRollCost = atof (wallpaperRollCostInput);

while (wallpaperRollCost < 1)
{
cout<<"Please enter cost of wallpaper rolls as a whole number."<<endl;
cin>>wallpaperRollCostInput;
wallpaperRollCost = atof (wallpaperRollCostInput);
}
}

char doorNumberInput[256];
cout<<"Enter number of doors."<<endl;
cin>>doorNumberInput;
doorNumber = atof (doorNumberInput);

while (doorNumber < 1)
{
cout<<"Please number of doors as a whole number."<<endl;
cin>>doorNumberInput;
doorNumber = atof (doorNumberInput);
}

char windowNumberInput[256];
cout<<"Enter number of windows."<<endl;
cin>>windowNumberInput;
windowNumber = atof (windowNumberInput);

while (windowNumber < 1)
{
cout<<"Please number of windows as a whole number."<<endl;
cin>>windowNumberInput;
windowNumber = atof (windowNumberInput);
}

float ceilingLitres = (roomLength/100)*(roomWidth/100)/8;
float ceilingCosts = ceilingLitres*emulsionCost;
float wallsLitres = ((roomLength/100*roomHeight/100)+(roomWidth/100*roomHeight/100)*2)-(doorNumber*2)-(windowNumber*0.5);
float totalWallsPaintedCost = wallsLitres*emulsionCost;
float totalWallpaperCost = wallpaperRollCount*wallpaperRollCost;
float woodworkLitres = (doorNumber*doorPaintRequirement)+(windowNumber*windowPaintRequirement);
float totalWoodworkCost = woodworkLitres*glossCost;
float subtotal = totalWallsPaintedCost+totalWallpaperCost+totalWoodworkCost;
float subtotalVat = subtotal*vat;
float finalTotal=subtotal+subtotalVat;

cout << string(50, '\n');
cout<<endl<<endl<<"Joe Stevens Painting & Decorating Tel: 01522 522522"<<endl;
cout<<endl<<endl<<"Estimate costs for "<<customerName<<"."<<endl<<endl;
cout<<"MATERIALS"<<endl;
cout<<" Ceiling: "<<setiosflags(ios::fixed)<<setprecision(2)<<ceilingLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<emulsionCost<<" per litre "<<(char)156<<ceilingCosts<<endl;

switch (wallsPainted)
{
case 'p':
cout<<" Walls: "<<setiosflags(ios::fixed)<<setprecision(2)<<wallsLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<emulsionCost<<" per litre "<<(char)156<<totalWallsPaintedCost<<endl;
break;
case 'w':
cout<<" Walls: "<<setiosflags(ios::fixed)<<setprecision(2)<<wallpaperRollCount<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<wallpaperRollCost<<" per roll "<<(char)156<<totalWallpaperCost<<endl;
break;
}

cout<<" Woodwork: "<<setiosflags(ios::fixed)<<setprecision(2)<<woodworkLitres<<" @ "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<glossCost<<" per litre "<<(char)156<<totalWoodworkCost<<endl<<endl;

cout<<"SUB TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotal<<endl;
cout<<"VAT @ 20% "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<subtotalVat<<endl;
cout<<"FINAL TOTAL "<<setiosflags(ios::fixed)<<setprecision(2)<<(char)156<<finalTotal<<endl<<endl;

char goAgainInput[256];
cout << string(6, '\n');
cout<<"What would you like to do now?"<<endl;
cout<<"1 - Begin a new estimate."<<endl;
cout<<"2 - Exit the program."<<endl;
cin>>goAgainInput;
goAgain = atoi (goAgainInput);

while (goAgain != 1 && goAgain != 2)
{
cout<<"Please enter 1 to begin again or 2 to quit."<<endl;
cin>>goAgainInput;
goAgain = atof (goAgainInput);
}

} while (goAgain == 1);

system("PAUSE");
return EXIT_SUCCESS;
}
}


thankyou


I think your main problem is that you don't initialize all the values before adding them into your subtotals, so they're completely random (specifically the wallpaper stuff with a painted room).

-1.#R is probably a floating point error display specific to your system (there's also -1.#inf, -1.#ind, etc.).
Bigpet
Profile Joined July 2010
Germany533 Posts
November 15 2012 15:05 GMT
#3890
On November 15 2012 23:47 Nausea wrote:
Show nested quote +
On November 15 2012 23:39 netherh wrote:
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.


The X var is only defined in C_Entity, neither C_Player or C_PlayerShade defines it, they just use it through C_Entity.


Whatever the problem is, it doesn't appear to originate in the snippets you posted, have you tried setting breakpoints and checking the value of the member X at different points (mainly before and after passing the pointer to OnLoad)?
I'm NOT the caster with a similar nick
Nausea
Profile Joined October 2010
Sweden807 Posts
November 15 2012 15:15 GMT
#3891
On November 16 2012 00:05 Bigpet wrote:
Show nested quote +
On November 15 2012 23:47 Nausea wrote:
On November 15 2012 23:39 netherh wrote:
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.


The X var is only defined in C_Entity, neither C_Player or C_PlayerShade defines it, they just use it through C_Entity.


Whatever the problem is, it doesn't appear to originate in the snippets you posted, have you tried setting breakpoints and checking the value of the member X at different points (mainly before and after passing the pointer to OnLoad)?


Well I will have to code it again since I decided to remove it yesterday after hours of trial and error and memory errors making my computer wanna kill itself, so I ended up restarting my computer and reverting all that stuff.
Set it ablaze!
Nausea
Profile Joined October 2010
Sweden807 Posts
November 15 2012 17:56 GMT
#3892
On November 16 2012 00:15 Nausea wrote:
Show nested quote +
On November 16 2012 00:05 Bigpet wrote:
On November 15 2012 23:47 Nausea wrote:
On November 15 2012 23:39 netherh wrote:
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.


The X var is only defined in C_Entity, neither C_Player or C_PlayerShade defines it, they just use it through C_Entity.


Whatever the problem is, it doesn't appear to originate in the snippets you posted, have you tried setting breakpoints and checking the value of the member X at different points (mainly before and after passing the pointer to OnLoad)?


Well I will have to code it again since I decided to remove it yesterday after hours of trial and error and memory errors making my computer wanna kill itself, so I ended up restarting my computer and reverting all that stuff.


If anyone want to take a look and see if something looks glaringly wrong, please do so.

+ Show Spoiler +
class C_Entity {

public:
static std::vector<C_Entity*> EntityList;

C_Animation Anim_Control;

protected:


SDL_Surface* Surf_Entity;

public:
float X;
float Y;
(...)


#include "C_Entity.h"

std::vector<C_Entity*> C_Entity::EntityList;

C_Entity::C_Entity()
{

Surf_Entity = NULL;

X = 0;
Y = 0;

Width = 0;
Height = 0;

MoveLeft = false;
MoveRight = false;

Direction = ENTITY_DIRECTION_RIGHT;

Type = ENTITY_TYPE_GENERIC;

Dead = false;
Flags = ENTITY_FLAG_NONE;

SpeedX = 0;
SpeedY = 0;

AccelY = 0;

MaxSpeedX = 13;
MaxSpeedY = 10;

Col_X = 0;
Col_Y = 0;

Col_Width = 0;
Col_Height = 0;
(...)


class C_Player : public C_Entity {

public:

unsigned int LastSlide;
bool Sliding;
bool SlidingRight;
bool SlidingLeft;
bool JumpedOnSlide;

bool CanJump;
bool Jumping;
bool FirstJump;

bool Shooting;
bool ChargeShot;

(...)


C_Player::C_Player()
{

MaxSpeedY = 10.5;
X = 0;
Y = 0;

LastSlide = 0;
Sliding = false;
SlidingRight = false;
SlidingLeft = false;
JumpedOnSlide = false;

CanJump = false;
Jumping = false;
FirstJump = true;

Shooting = false;
ChargeShot = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;

Dead = false;

}


class C_PlayerShade : public C_Entity {

private:
C_Entity* target;

float SpeedDelay;

public:
C_PlayerShade();
C_PlayerShade(C_Entity* parent, float delay);
(...)


C_PlayerShade::C_PlayerShade()
{
SpeedX = 0;
SpeedY = 0;
SpeedDelay = 0;

AccelY = 0;

X = 0;
Y = 0;

target = NULL;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;
}

C_PlayerShade::C_PlayerShade(C_Entity* parent, float delay)
{

target = parent;

AccelY = target->AccelY;

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;
SpeedDelay = delay;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_GENERIC;
Flags = ENTITY_FLAG_GRAVITY;

OnLoad();

}


bool C_PlayerShade::OnLoad()
{


C_Frameset CharJumpRight( "JUMPINGRIGHT" );
CharJumpRight.AddFrame(0, 188, 25, 48);
CharJumpRight.AddFrame(25, 187, 23, 51);
CharJumpRight.AddFrame(48, 189, 23, 55);
CharJumpRight.AddFrame(71, 187, 23, 57);
CharJumpRight.AddFrame(94, 190, 27, 53);
CharJumpRight.AddFrame(121, 191, 34, 50);

C_Frameset CharJumpLeft( "JUMPINGLEFT" );
CharJumpLeft.AddFrame(0, 245, 25, 48);
CharJumpLeft.AddFrame(25, 244, 23, 51);
CharJumpLeft.AddFrame(48, 246, 23, 55);
CharJumpLeft.AddFrame(71, 244, 23, 57);
CharJumpLeft.AddFrame(94, 247, 27, 53);
CharJumpLeft.AddFrame(121, 248, 34, 50);

C_Frameset CharFallRight( "FALLINGRIGHT" );
CharFallRight.AddFrame(155, 190, 32, 52);
CharFallRight.AddFrame(187, 189, 29, 54);

C_Frameset CharFallLeft( "FALLINGLEFT" );
CharFallLeft.AddFrame(155, 247, 32, 52);
CharFallLeft.AddFrame(187, 246, 29, 52);

C_Frameset CharSlideRight( "SLIDINGRIGHT" );
CharSlideRight.AddFrame(0, 301, 34, 44);
CharSlideRight.AddFrame(34, 301, 46, 44, 10);
CharSlideRight.AddFrame(80, 301, 53, 44, 20);
CharSlideRight.AddFrame(133, 301, 57, 44, 20);
CharSlideRight.AddFrame(190, 301, 38, 44, 10);
CharSlideRight.AddFrame(228, 301, 30, 44, 10);
CharSlideRight.AddFrame(258, 301, 41, 44, 10);
CharSlideRight.AddFrame(299, 301, 43, 44, 10);

C_Frameset CharSlideLeft( "SLIDINGLEFT" );
CharSlideLeft.AddFrame(0, 345, 34, 44);
CharSlideLeft.AddFrame(34, 345, 46, 44);
CharSlideLeft.AddFrame(80, 345, 53, 44);
CharSlideLeft.AddFrame(133, 345, 57, 44);
CharSlideLeft.AddFrame(190, 345, 38, 44);
CharSlideLeft.AddFrame(228, 345, 30, 44);
CharSlideLeft.AddFrame(258, 345, 41, 44);
CharSlideLeft.AddFrame(299, 345, 43, 44);

C_Frameset Transparent("TRANSPARENT");
Transparent.AddFrame(500,0,0,0);

Anim_Control.AddFrameset(CharJumpRight);
Anim_Control.AddFrameset(CharJumpLeft);
Anim_Control.AddFrameset(CharFallRight);
Anim_Control.AddFrameset(CharFallLeft);
Anim_Control.AddFrameset(CharSlideRight);
Anim_Control.AddFrameset(CharSlideLeft);
Anim_Control.AddFrameset(Transparent);

Anim_Control.SetAnimation("TRANSPARENT",150);

if(C_Entity::OnLoad("./gfx/megaman/shade.png") == false)
{
fprintf(stderr, "Failed to load shade.png: %s\n", SDL_GetError());
return false;
}

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;

AccelY = target->AccelY;

return true;


}

Set it ablaze!
dapierow
Profile Blog Joined April 2010
Serbia1316 Posts
November 15 2012 18:24 GMT
#3893
Hey guys I need some help with a html+php problem for my assignment, ive spent over 30 hours writing code but im stuck on this last part of it.

on one page that only appears when the user has completed a successful log in. I am giving him an option to either increase or decrease the number of stocks he has for 11 different companies. The number of stocks he has is in a text file. I've simplified the text file to only contain the numbers of stocks and a ":" to separate the numbers from each other. Now my question is how would I create of function to go through the text file find the stock and either add or subtract the number of stocks he wants to buy or sell. But it must write to the file when the user is logging out!

can anyone help me out? anything is appreciated!
Eat.Sleep.Starcraft 2
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
Last Edited: 2012-11-15 18:28:05
November 15 2012 18:27 GMT
#3894
On November 16 2012 03:24 dapierow wrote:
Hey guys I need some help with a html+php problem for my assignment, ive spent over 30 hours writing code but im stuck on this last part of it.

on one page that only appears when the user has completed a successful log in. I am giving him an option to either increase or decrease the number of stocks he has for 11 different companies. The number of stocks he has is in a text file. I've simplified the text file to only contain the numbers of stocks and a ":" to separate the numbers from each other. Now my question is how would I create of function to go through the text file find the stock and either add or subtract the number of stocks he wants to buy or sell. But it must write to the file when the user is logging out!

can anyone help me out? anything is appreciated!


$_SESSION['stocks'] = explode(":", file_get_contents("user_xyz.stocks")); // load
file_put_contents("user_xyz.stocks", implode(":", $_SESSION['stocks'])); // save

$_SESSION['stocks'][$stock_idx]++; // increase
$_SESSION['stocks'][$stock_idx]--; // decrease

dapierow
Profile Blog Joined April 2010
Serbia1316 Posts
November 15 2012 18:35 GMT
#3895
On November 16 2012 03:27 Morfildur wrote:
Show nested quote +
On November 16 2012 03:24 dapierow wrote:
Hey guys I need some help with a html+php problem for my assignment, ive spent over 30 hours writing code but im stuck on this last part of it.

on one page that only appears when the user has completed a successful log in. I am giving him an option to either increase or decrease the number of stocks he has for 11 different companies. The number of stocks he has is in a text file. I've simplified the text file to only contain the numbers of stocks and a ":" to separate the numbers from each other. Now my question is how would I create of function to go through the text file find the stock and either add or subtract the number of stocks he wants to buy or sell. But it must write to the file when the user is logging out!

can anyone help me out? anything is appreciated!


$_SESSION['stocks'] = explode(":", file_get_contents("user_xyz.stocks")); // load
file_put_contents("user_xyz.stocks", implode(":", $_SESSION['stocks'])); // save

$_SESSION['stocks'][$stock_idx]++; // increase
$_SESSION['stocks'][$stock_idx]--; // decrease


Yeah the Session stuff is really giving me struggles but thank you for the help I appriciate it!
Eat.Sleep.Starcraft 2
Bigpet
Profile Joined July 2010
Germany533 Posts
November 15 2012 19:16 GMT
#3896
On November 16 2012 02:56 Nausea wrote:
Show nested quote +
On November 16 2012 00:15 Nausea wrote:
On November 16 2012 00:05 Bigpet wrote:
On November 15 2012 23:47 Nausea wrote:
On November 15 2012 23:39 netherh wrote:
On November 15 2012 22:59 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:45 Bigpet wrote:
Show nested quote +
On November 15 2012 22:22 Nausea wrote:
+ Show Spoiler +
On November 15 2012 22:07 Schokomuesli wrote:
Just to get it right... What you are doing is:

{
(...)
C_Player player;
Shade.OnLoad(&player, 0.5f);
(...)
}

and inside Shade.OnLoad() you copy a value in player to a local variable in Shade, right? Furthermore: is the pointer you passed into OnLoad still valid when using it? In particular: do you use if after leaving the scope in which you created player (that is: the closing curly brace just below OnLoad ^)? Because after this brace, player does not exist anymore if you create it on the stack as you did in the example above...

If you do _excatly_ this:

{
C_Player player;
Shade.OnLoad(&player, 0.5f);
}

^and never again use anything inside Shade which refers to the pointer you passed you are fine and all of this should work.

If you do this:

{
{
C_Player player;
C_Player* player2 = new C_Player();
Shade.OnLoad(&player, 0.5f);
Shade.OnLoad(player2, 0.5f);
}
Shade.asdf1(); // asdf1 uses the pointer to player you passed /w onLoad before - this breaks
Shade.asdf2(); // asdf2 uses the pointer to player2 you passed /w onLoad before - this works because due to heap construction, the object player2 points to still exists...
}

This will not work... player does not exist anymore but you still have the adress where it used to reside before leaving the scope you created it in.

BTW, this is just guesswork without seeing the methods and the surrounding code...


Thanks, but C_Player player is from C_Application.h and then implemented inside applications OnInit funktion. This should lead player to "die" at the end of the program, like every entity in my entitylist. I have tried making player dynamic, this did not work either. This is why I am confused atm.


Then what exactly is strange in your program then? Explaining it in words is kind of hard to understand. You should post some code example. Btw. if your problem is that the pointer is slightly offset then that may be due to multiple inheritance if you use that. Don't let us puzzle, post a self contained example.


#include <iostream>

class Base{public: long padding;};
class Base2{};
class Derived : public Base, public Base2{};

int main(){
Derived d;
Derived *ptr = &d;
Base2 *bas = static_cast<Base2 *>(ptr);
if((void *)ptr != (void *)bas){
std::cout << "the wonders of multiple inheritance" << std::endl;
}
return 0;
}


Ok, here goes then. Adding some example code to show you what is happening.

+ Show Spoiler +
C_Application.h


class C_Application : public C_Event {

private:
C_Player player;

(...)

};



C_Application.cpp


bool C_Application::OnInit()
{

player.OnLoad("player.png", 0, 0);

C_PlayerShade* shadeptr = new C_Shade;

shadeptr->OnLoad(&player, 0.5);

(...)

}


C_Player inherits C_Entity, C_PlayerShade inherits C_Entity.

C_PlayerShade.h


class C_PlayerShade : public C_Entity {

private:
C_Entity* parentptr;

public:
bool OnLoad(C_Entity* parent, float delay);

(...)

}



C_PlayerShade.cpp


bool C_PlayerShade::OnLoad(C_Entity* parent, float delay)
{

parentptr = parent;

this->X = parentptr->X; // Does not work. Gives wrong value.

}



Hope that helps, If you want to know more let me know.



I think we might need to see C_Player and C_Entity and where / how you set the value of X to be certain what's going on.

It's possible that you're defining X in both the player and entity class, and only set the value in the player class. Then when you try to fetch it from your entity pointer, you get the value of X from the entity class, and not the player class. But that's just a guess without seeing the other things.


The X var is only defined in C_Entity, neither C_Player or C_PlayerShade defines it, they just use it through C_Entity.


Whatever the problem is, it doesn't appear to originate in the snippets you posted, have you tried setting breakpoints and checking the value of the member X at different points (mainly before and after passing the pointer to OnLoad)?


Well I will have to code it again since I decided to remove it yesterday after hours of trial and error and memory errors making my computer wanna kill itself, so I ended up restarting my computer and reverting all that stuff.


If anyone want to take a look and see if something looks glaringly wrong, please do so.

+ Show Spoiler +
class C_Entity {

public:
static std::vector<C_Entity*> EntityList;

C_Animation Anim_Control;

protected:


SDL_Surface* Surf_Entity;

public:
float X;
float Y;
(...)


#include "C_Entity.h"

std::vector<C_Entity*> C_Entity::EntityList;

C_Entity::C_Entity()
{

Surf_Entity = NULL;

X = 0;
Y = 0;

Width = 0;
Height = 0;

MoveLeft = false;
MoveRight = false;

Direction = ENTITY_DIRECTION_RIGHT;

Type = ENTITY_TYPE_GENERIC;

Dead = false;
Flags = ENTITY_FLAG_NONE;

SpeedX = 0;
SpeedY = 0;

AccelY = 0;

MaxSpeedX = 13;
MaxSpeedY = 10;

Col_X = 0;
Col_Y = 0;

Col_Width = 0;
Col_Height = 0;
(...)


class C_Player : public C_Entity {

public:

unsigned int LastSlide;
bool Sliding;
bool SlidingRight;
bool SlidingLeft;
bool JumpedOnSlide;

bool CanJump;
bool Jumping;
bool FirstJump;

bool Shooting;
bool ChargeShot;

(...)


C_Player::C_Player()
{

MaxSpeedY = 10.5;
X = 0;
Y = 0;

LastSlide = 0;
Sliding = false;
SlidingRight = false;
SlidingLeft = false;
JumpedOnSlide = false;

CanJump = false;
Jumping = false;
FirstJump = true;

Shooting = false;
ChargeShot = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;

Dead = false;

}


class C_PlayerShade : public C_Entity {

private:
C_Entity* target;

float SpeedDelay;

public:
C_PlayerShade();
C_PlayerShade(C_Entity* parent, float delay);
(...)


C_PlayerShade::C_PlayerShade()
{
SpeedX = 0;
SpeedY = 0;
SpeedDelay = 0;

AccelY = 0;

X = 0;
Y = 0;

target = NULL;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;
}

C_PlayerShade::C_PlayerShade(C_Entity* parent, float delay)
{

target = parent;

AccelY = target->AccelY;

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;
SpeedDelay = delay;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_GENERIC;
Flags = ENTITY_FLAG_GRAVITY;

OnLoad();

}


bool C_PlayerShade::OnLoad()
{


C_Frameset CharJumpRight( "JUMPINGRIGHT" );
CharJumpRight.AddFrame(0, 188, 25, 48);
CharJumpRight.AddFrame(25, 187, 23, 51);
CharJumpRight.AddFrame(48, 189, 23, 55);
CharJumpRight.AddFrame(71, 187, 23, 57);
CharJumpRight.AddFrame(94, 190, 27, 53);
CharJumpRight.AddFrame(121, 191, 34, 50);

C_Frameset CharJumpLeft( "JUMPINGLEFT" );
CharJumpLeft.AddFrame(0, 245, 25, 48);
CharJumpLeft.AddFrame(25, 244, 23, 51);
CharJumpLeft.AddFrame(48, 246, 23, 55);
CharJumpLeft.AddFrame(71, 244, 23, 57);
CharJumpLeft.AddFrame(94, 247, 27, 53);
CharJumpLeft.AddFrame(121, 248, 34, 50);

C_Frameset CharFallRight( "FALLINGRIGHT" );
CharFallRight.AddFrame(155, 190, 32, 52);
CharFallRight.AddFrame(187, 189, 29, 54);

C_Frameset CharFallLeft( "FALLINGLEFT" );
CharFallLeft.AddFrame(155, 247, 32, 52);
CharFallLeft.AddFrame(187, 246, 29, 52);

C_Frameset CharSlideRight( "SLIDINGRIGHT" );
CharSlideRight.AddFrame(0, 301, 34, 44);
CharSlideRight.AddFrame(34, 301, 46, 44, 10);
CharSlideRight.AddFrame(80, 301, 53, 44, 20);
CharSlideRight.AddFrame(133, 301, 57, 44, 20);
CharSlideRight.AddFrame(190, 301, 38, 44, 10);
CharSlideRight.AddFrame(228, 301, 30, 44, 10);
CharSlideRight.AddFrame(258, 301, 41, 44, 10);
CharSlideRight.AddFrame(299, 301, 43, 44, 10);

C_Frameset CharSlideLeft( "SLIDINGLEFT" );
CharSlideLeft.AddFrame(0, 345, 34, 44);
CharSlideLeft.AddFrame(34, 345, 46, 44);
CharSlideLeft.AddFrame(80, 345, 53, 44);
CharSlideLeft.AddFrame(133, 345, 57, 44);
CharSlideLeft.AddFrame(190, 345, 38, 44);
CharSlideLeft.AddFrame(228, 345, 30, 44);
CharSlideLeft.AddFrame(258, 345, 41, 44);
CharSlideLeft.AddFrame(299, 345, 43, 44);

C_Frameset Transparent("TRANSPARENT");
Transparent.AddFrame(500,0,0,0);

Anim_Control.AddFrameset(CharJumpRight);
Anim_Control.AddFrameset(CharJumpLeft);
Anim_Control.AddFrameset(CharFallRight);
Anim_Control.AddFrameset(CharFallLeft);
Anim_Control.AddFrameset(CharSlideRight);
Anim_Control.AddFrameset(CharSlideLeft);
Anim_Control.AddFrameset(Transparent);

Anim_Control.SetAnimation("TRANSPARENT",150);

if(C_Entity::OnLoad("./gfx/megaman/shade.png") == false)
{
fprintf(stderr, "Failed to load shade.png: %s\n", SDL_GetError());
return false;
}

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;

AccelY = target->AccelY;

return true;


}



Nothing glaringly obious, you should either make a self-contained example that contains the problem or like I said, set breakpoints, read the values of X at different points, make sure it's still valid by setting X to some distinct error value in the destructor.
I'm NOT the caster with a similar nick
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
November 15 2012 19:23 GMT
#3897
On November 16 2012 02:56 Nausea wrote:
If anyone want to take a look and see if something looks glaringly wrong, please do so.

+ Show Spoiler +
class C_Entity {

public:
static std::vector<C_Entity*> EntityList;

C_Animation Anim_Control;

protected:


SDL_Surface* Surf_Entity;

public:
float X;
float Y;
(...)


#include "C_Entity.h"

std::vector<C_Entity*> C_Entity::EntityList;

C_Entity::C_Entity()
{

Surf_Entity = NULL;

X = 0;
Y = 0;

Width = 0;
Height = 0;

MoveLeft = false;
MoveRight = false;

Direction = ENTITY_DIRECTION_RIGHT;

Type = ENTITY_TYPE_GENERIC;

Dead = false;
Flags = ENTITY_FLAG_NONE;

SpeedX = 0;
SpeedY = 0;

AccelY = 0;

MaxSpeedX = 13;
MaxSpeedY = 10;

Col_X = 0;
Col_Y = 0;

Col_Width = 0;
Col_Height = 0;
(...)


class C_Player : public C_Entity {

public:

unsigned int LastSlide;
bool Sliding;
bool SlidingRight;
bool SlidingLeft;
bool JumpedOnSlide;

bool CanJump;
bool Jumping;
bool FirstJump;

bool Shooting;
bool ChargeShot;

(...)


C_Player::C_Player()
{

MaxSpeedY = 10.5;
X = 0;
Y = 0;

LastSlide = 0;
Sliding = false;
SlidingRight = false;
SlidingLeft = false;
JumpedOnSlide = false;

CanJump = false;
Jumping = false;
FirstJump = true;

Shooting = false;
ChargeShot = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;

Dead = false;

}


class C_PlayerShade : public C_Entity {

private:
C_Entity* target;

float SpeedDelay;

public:
C_PlayerShade();
C_PlayerShade(C_Entity* parent, float delay);
(...)


C_PlayerShade::C_PlayerShade()
{
SpeedX = 0;
SpeedY = 0;
SpeedDelay = 0;

AccelY = 0;

X = 0;
Y = 0;

target = NULL;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;
}

C_PlayerShade::C_PlayerShade(C_Entity* parent, float delay)
{

target = parent;

AccelY = target->AccelY;

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;
SpeedDelay = delay;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_GENERIC;
Flags = ENTITY_FLAG_GRAVITY;

OnLoad();

}


bool C_PlayerShade::OnLoad()
{
C_Frameset CharJumpRight( "JUMPINGRIGHT" );
CharJumpRight.AddFrame(0, 188, 25, 48);
CharJumpRight.AddFrame(25, 187, 23, 51);
CharJumpRight.AddFrame(48, 189, 23, 55);
CharJumpRight.AddFrame(71, 187, 23, 57);
CharJumpRight.AddFrame(94, 190, 27, 53);
CharJumpRight.AddFrame(121, 191, 34, 50);

C_Frameset CharJumpLeft( "JUMPINGLEFT" );
CharJumpLeft.AddFrame(0, 245, 25, 48);
CharJumpLeft.AddFrame(25, 244, 23, 51);
CharJumpLeft.AddFrame(48, 246, 23, 55);
CharJumpLeft.AddFrame(71, 244, 23, 57);
CharJumpLeft.AddFrame(94, 247, 27, 53);
CharJumpLeft.AddFrame(121, 248, 34, 50);

C_Frameset CharFallRight( "FALLINGRIGHT" );
CharFallRight.AddFrame(155, 190, 32, 52);
CharFallRight.AddFrame(187, 189, 29, 54);

C_Frameset CharFallLeft( "FALLINGLEFT" );
CharFallLeft.AddFrame(155, 247, 32, 52);
CharFallLeft.AddFrame(187, 246, 29, 52);

C_Frameset CharSlideRight( "SLIDINGRIGHT" );
CharSlideRight.AddFrame(0, 301, 34, 44);
CharSlideRight.AddFrame(34, 301, 46, 44, 10);
CharSlideRight.AddFrame(80, 301, 53, 44, 20);
CharSlideRight.AddFrame(133, 301, 57, 44, 20);
CharSlideRight.AddFrame(190, 301, 38, 44, 10);
CharSlideRight.AddFrame(228, 301, 30, 44, 10);
CharSlideRight.AddFrame(258, 301, 41, 44, 10);
CharSlideRight.AddFrame(299, 301, 43, 44, 10);

C_Frameset CharSlideLeft( "SLIDINGLEFT" );
CharSlideLeft.AddFrame(0, 345, 34, 44);
CharSlideLeft.AddFrame(34, 345, 46, 44);
CharSlideLeft.AddFrame(80, 345, 53, 44);
CharSlideLeft.AddFrame(133, 345, 57, 44);
CharSlideLeft.AddFrame(190, 345, 38, 44);
CharSlideLeft.AddFrame(228, 345, 30, 44);
CharSlideLeft.AddFrame(258, 345, 41, 44);
CharSlideLeft.AddFrame(299, 345, 43, 44);

C_Frameset Transparent("TRANSPARENT");
Transparent.AddFrame(500,0,0,0);

Anim_Control.AddFrameset(CharJumpRight);
Anim_Control.AddFrameset(CharJumpLeft);
Anim_Control.AddFrameset(CharFallRight);
Anim_Control.AddFrameset(CharFallLeft);
Anim_Control.AddFrameset(CharSlideRight);
Anim_Control.AddFrameset(CharSlideLeft);
Anim_Control.AddFrameset(Transparent);

Anim_Control.SetAnimation("TRANSPARENT",150);

if(C_Entity::OnLoad("./gfx/megaman/shade.png") == false)
{
fprintf(stderr, "Failed to load shade.png: %s\n", SDL_GetError());
return false;
}

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;

AccelY = target->AccelY;

return true;


}



This all looks ok, as long as you're setting up your pointers properly and so forth. (i.e. you're not calling the default constructor of PlayerShade, then calling OnLoad without setting a target.

What errors / problems are you currently getting? Is it still the value of x that's wrong? Really the best way to solve that issue would be using breakpoints / stepping through and looking at the values, as bigpet said earlier.
Nausea
Profile Joined October 2010
Sweden807 Posts
Last Edited: 2012-11-15 19:43:56
November 15 2012 19:41 GMT
#3898
On November 16 2012 04:23 netherh wrote:
Show nested quote +
On November 16 2012 02:56 Nausea wrote:
If anyone want to take a look and see if something looks glaringly wrong, please do so.

+ Show Spoiler +
class C_Entity {

public:
static std::vector<C_Entity*> EntityList;

C_Animation Anim_Control;

protected:


SDL_Surface* Surf_Entity;

public:
float X;
float Y;
(...)


#include "C_Entity.h"

std::vector<C_Entity*> C_Entity::EntityList;

C_Entity::C_Entity()
{

Surf_Entity = NULL;

X = 0;
Y = 0;

Width = 0;
Height = 0;

MoveLeft = false;
MoveRight = false;

Direction = ENTITY_DIRECTION_RIGHT;

Type = ENTITY_TYPE_GENERIC;

Dead = false;
Flags = ENTITY_FLAG_NONE;

SpeedX = 0;
SpeedY = 0;

AccelY = 0;

MaxSpeedX = 13;
MaxSpeedY = 10;

Col_X = 0;
Col_Y = 0;

Col_Width = 0;
Col_Height = 0;
(...)


class C_Player : public C_Entity {

public:

unsigned int LastSlide;
bool Sliding;
bool SlidingRight;
bool SlidingLeft;
bool JumpedOnSlide;

bool CanJump;
bool Jumping;
bool FirstJump;

bool Shooting;
bool ChargeShot;

(...)


C_Player::C_Player()
{

MaxSpeedY = 10.5;
X = 0;
Y = 0;

LastSlide = 0;
Sliding = false;
SlidingRight = false;
SlidingLeft = false;
JumpedOnSlide = false;

CanJump = false;
Jumping = false;
FirstJump = true;

Shooting = false;
ChargeShot = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;

Dead = false;

}


class C_PlayerShade : public C_Entity {

private:
C_Entity* target;

float SpeedDelay;

public:
C_PlayerShade();
C_PlayerShade(C_Entity* parent, float delay);
(...)


C_PlayerShade::C_PlayerShade()
{
SpeedX = 0;
SpeedY = 0;
SpeedDelay = 0;

AccelY = 0;

X = 0;
Y = 0;

target = NULL;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_PLAYER;
Flags = ENTITY_FLAG_GRAVITY;
}

C_PlayerShade::C_PlayerShade(C_Entity* parent, float delay)
{

target = parent;

AccelY = target->AccelY;

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;
SpeedDelay = delay;

Dead = false;

Direction = ENTITY_DIRECTION_RIGHT;
Type = ENTITY_TYPE_GENERIC;
Flags = ENTITY_FLAG_GRAVITY;

OnLoad();

}


bool C_PlayerShade::OnLoad()
{
C_Frameset CharJumpRight( "JUMPINGRIGHT" );
CharJumpRight.AddFrame(0, 188, 25, 48);
CharJumpRight.AddFrame(25, 187, 23, 51);
CharJumpRight.AddFrame(48, 189, 23, 55);
CharJumpRight.AddFrame(71, 187, 23, 57);
CharJumpRight.AddFrame(94, 190, 27, 53);
CharJumpRight.AddFrame(121, 191, 34, 50);

C_Frameset CharJumpLeft( "JUMPINGLEFT" );
CharJumpLeft.AddFrame(0, 245, 25, 48);
CharJumpLeft.AddFrame(25, 244, 23, 51);
CharJumpLeft.AddFrame(48, 246, 23, 55);
CharJumpLeft.AddFrame(71, 244, 23, 57);
CharJumpLeft.AddFrame(94, 247, 27, 53);
CharJumpLeft.AddFrame(121, 248, 34, 50);

C_Frameset CharFallRight( "FALLINGRIGHT" );
CharFallRight.AddFrame(155, 190, 32, 52);
CharFallRight.AddFrame(187, 189, 29, 54);

C_Frameset CharFallLeft( "FALLINGLEFT" );
CharFallLeft.AddFrame(155, 247, 32, 52);
CharFallLeft.AddFrame(187, 246, 29, 52);

C_Frameset CharSlideRight( "SLIDINGRIGHT" );
CharSlideRight.AddFrame(0, 301, 34, 44);
CharSlideRight.AddFrame(34, 301, 46, 44, 10);
CharSlideRight.AddFrame(80, 301, 53, 44, 20);
CharSlideRight.AddFrame(133, 301, 57, 44, 20);
CharSlideRight.AddFrame(190, 301, 38, 44, 10);
CharSlideRight.AddFrame(228, 301, 30, 44, 10);
CharSlideRight.AddFrame(258, 301, 41, 44, 10);
CharSlideRight.AddFrame(299, 301, 43, 44, 10);

C_Frameset CharSlideLeft( "SLIDINGLEFT" );
CharSlideLeft.AddFrame(0, 345, 34, 44);
CharSlideLeft.AddFrame(34, 345, 46, 44);
CharSlideLeft.AddFrame(80, 345, 53, 44);
CharSlideLeft.AddFrame(133, 345, 57, 44);
CharSlideLeft.AddFrame(190, 345, 38, 44);
CharSlideLeft.AddFrame(228, 345, 30, 44);
CharSlideLeft.AddFrame(258, 345, 41, 44);
CharSlideLeft.AddFrame(299, 345, 43, 44);

C_Frameset Transparent("TRANSPARENT");
Transparent.AddFrame(500,0,0,0);

Anim_Control.AddFrameset(CharJumpRight);
Anim_Control.AddFrameset(CharJumpLeft);
Anim_Control.AddFrameset(CharFallRight);
Anim_Control.AddFrameset(CharFallLeft);
Anim_Control.AddFrameset(CharSlideRight);
Anim_Control.AddFrameset(CharSlideLeft);
Anim_Control.AddFrameset(Transparent);

Anim_Control.SetAnimation("TRANSPARENT",150);

if(C_Entity::OnLoad("./gfx/megaman/shade.png") == false)
{
fprintf(stderr, "Failed to load shade.png: %s\n", SDL_GetError());
return false;
}

X = target->X;
Y = target->Y;

SpeedX = target->SpeedX;
SpeedY = target->SpeedY;

AccelY = target->AccelY;

return true;


}



This all looks ok, as long as you're setting up your pointers properly and so forth. (i.e. you're not calling the default constructor of PlayerShade, then calling OnLoad without setting a target.

What errors / problems are you currently getting? Is it still the value of x that's wrong? Really the best way to solve that issue would be using breakpoints / stepping through and looking at the values, as bigpet said earlier.


The C_PlayerShade object never gets the C_Player objects X/Y and other values, and I get some sort of memory error that causes my computer to slow down. However the C_PlayerShade object actually reacts to my character moving so I don´t understand what is wrong, that should mean that the pointer is good, right? Must be something else related to this causing the problems. Without using this part of the program everything else runs smooth.
Set it ablaze!
Nausea
Profile Joined October 2010
Sweden807 Posts
November 15 2012 20:34 GMT
#3899
Solved my problem, the pointer seems to work, my program crashing at the end was due to my cleanup function going through all enteties trying to delete them, and player when it was not a dynamic allocated object. That was stupid.

Thanks all for your help/attention
Set it ablaze!
dapierow
Profile Blog Joined April 2010
Serbia1316 Posts
Last Edited: 2012-11-15 22:54:26
November 15 2012 21:34 GMT
#3900
Nvm got it
Eat.Sleep.Starcraft 2
Prev 1 193 194 195 196 197 1032 Next
Please log in or register to reply.
Live Events Refresh
PiGosaur Cup
01:00
#64
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
WinterStarcraft452
Livibee 162
StarCraft: Brood War
Britney 25888
Stork 358
BeSt 159
EffOrt 60
Mind 30
ZergMaN 16
Icarus 13
Noble 12
Bale 8
Dota 2
monkeys_forever881
LuMiX0
League of Legends
JimRising 738
C9.Mang0584
Super Smash Bros
hungrybox444
Mew2King109
Other Games
summit1g6853
XaKoH 219
RuFF_SC2141
minikerr38
Organizations
Other Games
gamesdonequick2588
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• Berry_CruncH37
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• LaughNgamezSOOP
• Kozan
StarCraft: Brood War
• RayReign 25
• Diggity5
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
League of Legends
• Rush1466
• Lourlo1402
• Stunt344
Other Games
• Scarra2692
• Shiphtur193
Upcoming Events
WardiTV Invitational
6h 10m
The PondCast
1d 4h
OSC
1d 6h
OSC
2 days
All Star Teams
2 days
INnoVation vs soO
sOs vs Scarlett
uThermal 2v2 Circuit
3 days
All Star Teams
3 days
MMA vs DongRaeGu
Rogue vs Oliveira
Sparkling Tuna Cup
4 days
OSC
4 days
Replay Cast
5 days
[ Show More ]
Wardi Open
5 days
Liquipedia Results

Completed

Proleague 2026-01-13
Big Gabe Cup #3
NA Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL 21 Non-Korean Championship
CSL 2025 WINTER (S19)
OSC Championship Season 13
Underdog Cup #3
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025

Upcoming

Escore Tournament S1: W4
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
Thunderfire SC2 All-star 2025
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.