Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: Need help with c++ , urgent!  (Read 3253 times)

alfie275

  • Bay Watcher
    • View Profile
Need help with c++ , urgent!
« on: April 05, 2009, 05:41:14 am »

Why will this class not work?

class BodyPart
{
public:
       void Init(char initName, int initHealth, char initParent, bool initHasParent) {
        Name = initName;
        Parent = initParent;
        HasParent = initHasParent;
        Severed = false;
        }
       
        bool GetSevered() {
             return Severed;
             }
       
        int GetHealth() {
             return Health;
             }       
         void Update() {
              CheckHealth();
              if (HasParent == true) {
              CheckParent();
              }
              }
                 
           
           
private:
        char Name;
        char Parent;
        bool HasParent;
        bool Severed;
        int Health;
       
        void CheckHealth() {
             if (Health < 1) {
             Severed = true  ;         
             }
             }
        void CheckParent() {
             if (Parent.GetSevered() == true) {
             Severed = true;
             }
             }
       
       
};           

I need it to work for my rougelike game.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

IndonesiaWarMinister

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #1 on: April 05, 2009, 05:43:28 am »

Move this to Creative Project. Quick!
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #2 on: April 05, 2009, 06:43:47 am »

How do I move it?
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

IndonesiaWarMinister

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #3 on: April 05, 2009, 07:25:14 am »

There are... buttons in the lower part of the thread.

I think. Beside the lock topic button, there is the move topic button.
Logged

popo999

  • Escaped Lunatic
    • View Profile
Re: Need help with c++ , urgent!
« Reply #4 on: April 05, 2009, 10:07:40 am »

if (Parent.GetSevered() == true) {
             Severed = true;
             }

Maybe because Parent is char and doesn't have method GetSevered()?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Need help with c++ , urgent!
« Reply #5 on: April 05, 2009, 10:36:37 am »

Are you using a debuger? If so, can you tell us what kind of error you get?
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #6 on: April 05, 2009, 11:12:35 am »

Okay update:
class BodyPart
{
public:
       void Init(char initName, int initHealth, BodyPart initParent, bool initHasParent) {
        Name = initName;
        Parent = initParent;
        HasParent = initHasParent;
        Severed = false;
        }
       bool GetSevered() {
            return Severed;
            }
     
       
        int GetHealth() {
             return Health;
             }       
         void Update() {
              CheckHealth();
              if (HasParent == true) {
              CheckParent();
              }
              }
                 
           
           
private:
        char Name;
        BodyPart Parent;
        bool HasParent;
        int Health;
        bool Severed;
         
        void CheckHealth() {
             if (Health < 1) {
             Severed = 1  ;         
             }
             }
        void CheckParent() {
             if (Parent.GetSevered() == true) {
             Severed = true;
             }
             }
       
       
};

43 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp field `Parent' has incomplete type

 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp In member function `void BodyPart::Init(char, int, BodyPart, bool)':

20 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp `Parent' undeclared (first use this function)

 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp In member function `void BodyPart::CheckParent()':

54 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp `Parent' undeclared (first use this function)

 E:\Alfie\Ascii Man\AsciiMan alpha\Makefile.win [Build Error]  [main.o] Error 1
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

corvvs

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #7 on: April 05, 2009, 11:14:18 am »

if (Parent.GetSevered() == true) {
             Severed = true;
             }
Maybe because Parent is char and doesn't have method GetSevered()?

This.

Parent should be defined as:
BodyPart *Parent;

Also Name should be char *Name;
or since you're using c++, String Name;
« Last Edit: April 05, 2009, 11:16:57 am by corvvs »
Logged

Mcshay

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #8 on: April 05, 2009, 11:27:10 am »

You can't have an auto variable of BodyPart inside of the definition of BodyPart. The compiler does not yet know how large the data type is so it can't finish the definition of the type it's trying to define. You can work around this by using a BodyPart pointer which is guaranteed to have a fixed size in memory (32 bits in most machines).

This might make things a bit more complicated though, since you now either have to make sure the initParent argument points to an existing BodyPart either by you yourself making sure it exists or allocating it to the heap beforehand (and deleting it when you are done with it).

Try this:


class BodyPart
{
public:
       void Init(char initName, int initHealth, BodyPart* initParent, bool initHasParent) {
        Name = initName;
        Parent = initParent;
        HasParent = initHasParent;
        Severed = false;
        }
       bool GetSevered() {
            return Severed;
            }
     
       
        int GetHealth() {
             return Health;
             }       
         void Update() {
              CheckHealth();
              if (HasParent == true) {
              CheckParent();
              }
              }
                 
           
           
private:
        char Name;
        BodyPart* Parent;
        bool HasParent;
        int Health;
        bool Severed;
         
        void CheckHealth() {
             if (Health < 1) {
             Severed = 1  ;         
             }
             }
        void CheckParent() {
             if (Parent->GetSevered() == true) {
             Severed = true;
             }
             }
       
       
};
Logged

Mcshay

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #9 on: April 05, 2009, 11:29:40 am »

if (Parent.GetSevered() == true) {
             Severed = true;
             }
Maybe because Parent is char and doesn't have method GetSevered()?

This.

Parent should be defined as:
BodyPart *Parent;

Also Name should be char *Name;
or since you're using c++, String Name;

Minor issue: C++ strings are called 'string' not 'String'. You also need to remember to: #include <string>
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #10 on: April 05, 2009, 11:38:42 am »

strings arent working even when I include the string thing.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

popo999

  • Escaped Lunatic
    • View Profile
Re: Need help with c++ , urgent!
« Reply #11 on: April 05, 2009, 11:41:20 am »

strings arent working even when I include the string thing.

Are you using namespace std?
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #12 on: April 05, 2009, 12:53:58 pm »

Err I don't think so... ::)

Also, in my character class:
     void  Addpart(BodyPart* part,char initName, int initHealth, BodyPart* initParent, bool initHasParent) {
           int i;
           OldParts = new BodyPart[Numparts];
           for (i = 0;i < Numparts; i++) {
               OldParts = Parts;
               }
               delete [] Parts;
               Numparts = Numparts + 1;
               Parts = new BodyPart[Numparts];
               Parts[Numparts] = part;
               part.Init(initName, initHealth, initParent, initHasParent);     
               for (i = 0;i < Numpats - 1; i++) {
                   Parts = OldParts
                   }
               delete [] OldParts;
               } 

Throws the errors:

83 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp no match for 'operator=' in '*(((Character*)this)->Character::Parts + (+(((unsigned int)((Character*)this)->Character::Numparts) * 20u))) = part'

 note E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp:16 candidates are: BodyPart& BodyPart::operator=(const BodyPart&)


84 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp `Init' is not a type

84 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp request for member of non-aggregate type before '(' token

Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

popo999

  • Escaped Lunatic
    • View Profile
Re: Need help with c++ , urgent!
« Reply #13 on: April 05, 2009, 01:07:50 pm »

Strings must be used like this:

#include <string>
using namespace std;

string text = "text";

or like this:

#include <string>

std::string text = "text";


and btw I recommend using vectors instead of arrays, they are easier to use.
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #14 on: April 05, 2009, 02:08:08 pm »

Yeah but it's not an array of numbers, it's an array of body parts.
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275
Pages: [1] 2