-
Classes
I was going through the basics of C++ to make sure i got everything and i came accros this problem ive been trying for hours to fix this but the error just wont got away.
Code:
#include <iostream>
using namespace std;
class Animal
{
public:
void SetAge(int age) { Age = age; }
int GetAge() const { cout <<Age; }
private:
int Age;
};
int main()
{
Animal * Horse = new Animal;
Horse->SetAge(19);
cout <<"The horses age is "<<Horse->GetAge<<endl;
cin.get();
return 0;
}
Code:
Compiling...
void.cpp
C:\C++\RP\void.cpp(18) : warning C4761: integral size mismatch in argument; conversion supplied
C:\C++\RP\void.cpp(8) : error C4716: 'Animal::GetAge' : must return a value
Error executing cl.exe.
RP.exe - 1 error(s), 1 warning(s)
-
int GetAge() const { cout <<Age; }
cout <<"The horses age is "<<Horse->GetAge<<endl;
you have GetAge explicitly output the Age but not return the Age for your second cout. If you return Age w/o outputing it you will get the desired result. Hope that helps somewhat.
-
So GetAge has to return a value ? i tried doing it compiler with no error's or warning's but the horses age is always 1 no matter what. :(
Code:
#include <iostream>
using namespace std;
class Animal
{
public:
void SetAge(int age) { Age = age; }
int GetAge() const { cout <<Age; return 0; }
private:
int Age;
};
int main()
{
Animal * Horse = new Animal;
Horse->SetAge(19);
cout <<"The horses age is "<<Horse->GetAge<<endl;
cin.get();
return 0;
}
-
Now GetAge will always return 0, so 0 is what you will see as age. try to return Age.
-
Thanks i got it working im just about ready to start my first big project but i ran into another problem :/
Code:
#include <iostream>
using namespace std;
class Cell
{
public:
int GetPositionX() { return CellPosX; }
int GetPositionY() { return CellPosY; }
void SetPositionX(int PositionX) { CellPosX = PositionX; }
void SetPositionY(int PositionY) { CellPosY = PositionY; }
private:
int CellPosX;
int CellPosY;
};
int main()
{
Cell * CellOne = new Cell;
CellOne->SetPositionX(2);
CellOne->SetPositionY(5);
cout <<"The cell is at "<<CellOne->GetPositionX()<<","<<CellOne->GetPositionY<<endl;
cin.get();
}
why does the cell position return as 2,1?
i think this might have something to do with it but i cant fix the warning
Code:
C:\C++\RP\void.cpp(22) : warning C4761: integral size mismatch in argument; conversion supplied
-
you forgot the parenthesis:
Code:
cout <<"The cell is at "<<CellOne->GetPositionX()<<","<<CellOne->GetPositionY()<<endl;
-
....and if you use new to allocate memory be sure to delete it when you're done.
-
Can you help me to develop class architecture in c++, in my own GUI windows library?
-
What happens if i dont delete memory? does this mean i will have to restart my computer to use it again?
-
It depends completly on the operating system. Some will keep track of all memory allocated by your program and will free any allocated memory. You should never trust the OS to do this though. Those that don't would eventually require you to reboot as you'll eventually run out of memory.
-
Does Windows ME free the memory automatically? I've noticed that after having the computer on for several straight days it will eventaully require me to reboot because of insifficent computer resources, but my Windows XP machine never does this.
-
Code:
Game Player;
Game * PlayerTwo = new Game;
iwch method is better for accesing members of a class? and is there any advantage is using one over the other?
-
Neither of those methods access a class, they declare a new instance of that class. The first does it on the stack, the second allocates it dynamically on the heap. Which is better, depends on the situation. If one was hands-down better, it probably wouldn't exist. As for acessing a class, the same applies.
-
Ah ok thanks for clearing that up.