Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; I was going through the basics of C++ to make sure i got everything and i came accros this problem ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    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)

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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.

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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;
    }

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Now GetAge will always return 0, so 0 is what you will see as age. try to return Age.

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you forgot the parenthesis:

    Code:
    cout <<"The cell is at "<<CellOne->GetPositionX()<<","<<CellOne->GetPositionY()<<endl;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    ....and if you use new to allocate memory be sure to delete it when you're done.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    1
    Can you help me to develop class architecture in c++, in my own GUI windows library?

  9. #9
    Banned
    Join Date
    Oct 2004
    Posts
    250
    What happens if i dont delete memory? does this mean i will have to restart my computer to use it again?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  11. #11
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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.
    Last edited by MadCow257; 01-01-2005 at 10:09 PM.

  12. #12
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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?

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  14. #14
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Ah ok thanks for clearing that up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21