C++ Help
Nov 4 2008, 9:58 am
By: Souma  

Nov 4 2008, 9:58 am Souma Post #1



Hello! I need your help D:! I've been following my book on learning C++ and well I decided to make something simple but I'm stuck D:.

I have 3 files, 2 ".cpp" and 1 header file ".h". Here it is:

main.cpp
Quote
#include <iostream>
#include "functions.cpp"

using namespace std;

int main()
{
cout << "Hi";

char response;
cin >> response;

return 0;
}

functions.cpp
Quote
#include "monster.h"

Monster::SetHP(int HP)
{
itsHP = HP;
}
Monster::SetSP(int SP)
{
itsSP = SP;
}

Monster::GetHP()
{
return itsHP;
}
Monster::GetSP()
{
return itsSP;
}

monster.h
Quote
#include <iostream>

class Monster
{
private:
unsigned int itsHP;
unsigned int itsSP;

public:
void SetHP(int HP = 50);
void SetSP(int SP = 50);
unsigned int GetHP() const;
unsigned int GetSP() const;
};

When I try to build this, I get a huge load of errors. It'll be way too long to list here, and I'm fairly sure I probably have some tiny error somewhere...hopefully D:. In main I just put "Hi" because I wanted to see if it would build at least before I moved on to making what I wanted. So if anyone sees anything wrong and can help me out, I would love juu 4eva ^^ !

Thanks :D !



None.

Nov 4 2008, 3:06 pm Oyen Post #2



Well, I'm a bit new to visual c++ as well, but if I recall correctly, you can't use include statements for .cpp files. Try changing the #include "functions.cpp" to #include "monster.h"

Also:
Quote
Monster::SetHP(int HP)
{
itsHP = HP;
}
Monster::SetSP(int SP)
{
itsSP = SP;
}

Monster::GetHP()
{
return itsHP;
}
Monster::GetSP()
{
return itsSP;
}
You forgot to put the return type for these member functions. The first two functions should probably accept an unsigned int, so that the function won't accept a negative int, which will screw up the itsHP/itsSP variables from gaining a value. You also forgot the const for the last two. It should look like this:

void Monster::SetHP(unsigned int HP)
{
itsHP = HP;
}
void Monster::SetSP(unsigned int SP)
{
itsSP = SP;
}

unsigned int Monster::GetHP() const
{
return itsHP;
}
unsigned int Monster::GetSP() const
{
return itsSP;
}

Post has been edited 1 time(s), last time on Nov 4 2008, 3:14 pm by Oyen.



None.

Nov 4 2008, 8:58 pm Souma Post #3



Thank You for the reply! I never knew you couldn't include ".cpp". I changed functions.cpp to functions.h and did what you told me to. The huge load of errors went down to 2 :) .

Quote
...\functions.h(4) : error C2511: 'void Monster::SetHP(unsigned int)' : overloaded member function not found in 'Monster'
...\functions.h(8) : error C2511: 'void Monster::SetSP(unsigned int)' : overloaded member function not found in 'Monster'

I have no idea what this means o.o. If you could or someone else can help me again D:. <3!



None.

Nov 4 2008, 9:19 pm ClansAreForGays Post #4



Either you're send an int to a method that doesn't have an int parameter, or your only calling one that needs one.




Nov 4 2008, 9:50 pm Oyen Post #5



Quote from Souma
Thank You for the reply! I never knew you couldn't include ".cpp". I changed functions.cpp to functions.h and did what you told me to. The huge load of errors went down to 2 :) .

Quote
...\functions.h(4) : error C2511: 'void Monster::SetHP(unsigned int)' : overloaded member function not found in 'Monster'
...\functions.h(8) : error C2511: 'void Monster::SetSP(unsigned int)' : overloaded member function not found in 'Monster'

I have no idea what this means o.o. If you could or someone else can help me again D:. <3!

Oh, I forgot to mention that you need to change the declarations of those member functions in the header file to each have an unsigned int as a parameter instead of int.
In otherwords, change this:
Quote
public:
void SetHP(int HP = 50);
void SetSP(int SP = 50);

unsigned int GetHP() const;
unsigned int GetSP() const;
to this:
Quote
public:
void SetHP(unsigned int HP = 50);
void SetSP(unsigned int SP = 50);

unsigned int GetHP() const;
unsigned int GetSP() const;




None.

Nov 4 2008, 11:43 pm Souma Post #6



Quote from ClansAreForGays
Either you're send an int to a method that doesn't have an int parameter, or your only calling one that needs one.
I have no idea what you just said D:!

Quote from Oyen
Oh, I forgot to mention that you need to change the declarations of those member functions in the header file to each have an unsigned int as a parameter instead of int.
In otherwords, change this:
Quote
public:
void SetHP(int HP = 50);
void SetSP(int SP = 50);

unsigned int GetHP() const;
unsigned int GetSP() const;
to this:
Quote
public:
void SetHP(unsigned int HP = 50);
void SetSP(unsigned int SP = 50);

unsigned int GetHP() const;
unsigned int GetSP() const;
I had already done that before posting D:, same error o.o! Here I'll post the new updated files:

functions.h
Quote
#include "monster.h"

void Monster::SetHP(unsigned int HP)
{
itsHP = HP;
}
void Monster::SetSP(unsigned int SP)
{
itsSP = SP;
}

unsigned int Monster::GetHP() const
{
return itsHP;
}
unsigned int Monster::GetSP() const
{
return itsSP;
}

monster.h
Quote
class Monster
{
private:
unsigned int itsHP;
unsigned int itsSP;

public:
void SetHP(unsigned int HP = 50) const;
void SetSP(unsigned int SP = 50) const;
unsigned int GetHP() const;
unsigned int GetSP() const;
};

Thanks Again!



None.

Nov 5 2008, 12:46 am Oyen Post #7



Quote
public:
void SetHP(unsigned int HP = 50) const;
void SetSP(unsigned int SP = 50) const;

unsigned int GetHP() const;
unsigned int GetSP() const;
};
Take the const off the Set functions. It prevents the private variables from being changed.

If you are still having problems after doing that, I will copy your code into my own copy of visual c++ and try to get it to work.

Did you change the functions.cpp file to a .h file? :wtfage:



None.

Nov 5 2008, 1:01 am Souma Post #8



Oooo! Thanks! I didn't notice I put const after that.

It works now! ^^ ^^ ^^



None.

Nov 5 2008, 1:05 am Oyen Post #9



Good for you! :D
Glad I could help.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[03:29 am]
DarkenedFantasies -- Probably just didn't care. For example, at some point before release, they've updated the graphics of some of the Protoss buildings (Forge, CyberCore, Citadel, Observatory, Arbiter Tribunal), but instead of properly re-rendering them with edited 3D models, they did crappy copy-paste jobs on the rendered graphics.
[2026-6-22. : 8:35 pm]
Ultraviolet -- :wob:
[2026-6-21. : 11:38 pm]
Symmetry -- :wob:
[2026-6-21. : 4:56 am]
Ultraviolet -- I suppose we'll likely never know, but my guess would be that they already saw it operating successfully and there was no monetary incentive to finish the original work. And the dev cycle in old school Blizzard was so hectic, it's possible it just got forgotten about after the original game got released. Plus there's an element of existing MPQ files that were packaged with the original discs becoming outdated if they updated it. And it's not like they remade the original MPQs, they just made new ones for BW specifically
[2026-6-21. : 4:26 am]
Oh_Man -- so that makes me think maybe the theory they are unfinished is not true and its a deliberate design decision, coz why not finish them wen ur making brood war?
[2026-6-21. : 4:25 am]
Oh_Man -- the thing is thos buildings are from classic. that means they went ahead and made brood war without ever finishing the 'unfinished' buildings
[2026-6-20. : 6:15 pm]
Ultraviolet -- Yeah he's talked about a lot of that stuff in his casts before. It seems plausible. Especially knowing how Blizzard of yesteryear operated.
[2026-6-20. : 3:47 pm]
NudeRaider -- to clarify: couldn't recall the behavior for every single Protoss building but I was aware the disparity exists.
[2026-6-20. : 3:43 pm]
NudeRaider -- Contained nothing new for me. Didn't know all building's behavior, but very much all unit's. Also Terran balance whine - also nothing new :lol:
[2026-6-19. : 9:57 am]
Oh_Man -- makes me wonder if SEN knows anything about the topic
Please log in to shout.


Members Online: RIVE, JamaL