Thread: Hmm... can you see anything wrong?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    Hmm... can you see anything wrong?

    In this book im reading about C++ it has gave me a task to create a small program using a class CAT and constructor and a destructor in right.

    so it says meow
    frisky is a cat who is 5 years old
    meow
    now frisky is 7 years old

    but its giving me an error and i just cannot see what wrong with it.

    if anyone could just look at it for me and try to tell me whats wrong with it id greatly appreciate it.

    thanks in advance.

    Code:
    // Demonstrates declaration of constructors and
    // destructor for the cat class
    
    #include <iostream>
    
    using namespace std;
    
    class Cat
    {
    public:
    	Cat(int initialAge);
    	~Cat();
    	int GetAge();
    	int SetAge(int age);
    	void Meow();
    private:
    	int ItsAge;
    };
    
    // constructor of Cat,
    Cat::Cat(int initialAge)
    {
    	ItsAge = initialAge;
    }
    
    Cat::~Cat()
    {						// do nothing
    }
    
    // GetAge public accessor function
    // returns value of ItsAge member
    
    int Cat::GetAge()
    {
    	return ItsAge;
    }
    
    // Definition of SetAge, public
    // accessor function
    
    void Cat::SetAge(int age)
    {
    	// set member variable ItsAge to
    	// value passed in by parameter age
    	
    	ItsAge = age;
    }
    
    // Definition of Meow method
    // returns: void
    // parameters: none
    // action: prints "MEOW" on screen
    
    void Cat::Meow()
    {
    	cout <<"MEOW\n";
    }
    
    int main()
    {
    	Cat Frisky(5);
    	Frisky.Meow();
    	cout << "Frisky is a cat that is";
    	cout << Frisky.GetAge() << " years old.\n";
    	Frisky.Meow();
    	Frisky.SetAge(7);
    	cout << "Frisky is now";
    	cout << Frisky.GetAge() << " years old.\n";
    	system("PAUSE");
    	return 0;
    }
    the errors are
    Code:
    ------ Build started: Project: constructor and destructor, Configuration: Debug Win32 ------
    Compiling...
    constructor and destructor.cpp
    .\constructor and destructor.cpp(27) : error C2143: syntax error : missing ';' before '}'
    .\constructor and destructor.cpp(27) : error C2761: 'Cat::~Cat(void)' : member function redeclaration not allowed
    .\constructor and destructor.cpp(27) : error C2143: syntax error : missing ';' before '}'
    .\constructor and destructor.cpp(27) : error C2059: syntax error : '}'
    .\constructor and destructor.cpp(28) : error C2143: syntax error : missing ';' before '}'
    .\constructor and destructor.cpp(28) : error C2059: syntax error : '}'
    .\constructor and destructor.cpp(34) : error C2143: syntax error : missing ';' before '{'
    .\constructor and destructor.cpp(34) : error C2447: '{' : missing function header (old-style formal list?)
    .\constructor and destructor.cpp(42) : error C2556: 'void Cat::SetAge(int)' : overloaded function differs only by return type from 'int Cat::SetAge(int)'
            .\constructor and destructor.cpp(14) : see declaration of 'Cat::SetAge'
    .\constructor and destructor.cpp(42) : error C2371: 'Cat::SetAge' : redefinition; different basic types
            .\constructor and destructor.cpp(14) : see declaration of 'Cat::SetAge'
    .\constructor and destructor.cpp(66) : error C2264: 'Cat::SetAge' : error in function definition or declaration; function not called
    Build log was saved at "file://c:\Documents and Settings\Reece.YOUR-E0367A1424\My Documents\Visual Studio 2005\Projects\constructor and destructor\constructor and destructor\Debug\BuildLog.htm"
    constructor and destructor - 11 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Hugo.
    Last edited by Hugo716; 06-02-2006 at 03:58 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Cat::~Cat()
    } // do nothing
    }
    You have two closing braces.

    Actually, you dont even need to write a destructor for this class. The compiler generated destructor will suffice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    yeh i just changed the {{ to the right one.

    i know but it says you may as well since im learning.

    also, i removed the "VOID" Cat::SetAge(int age) and now there is only one error.

    Code:
    .\constructor and destructor.cpp(42) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    any ideas?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    also, i removed the "VOID" Cat::SetAge(int age) and now there is only one error.
    Leave the member function definition with the void return type, change the function declaration in the class to match this return type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    put that 'void' back there!

    A function declaration (your line 14) :
    Code:
    int SetAge(int age);
    and a function definition (your line 41):
    Code:
    void Cat::SetAge(int age)
    {
    	// set member variable ItsAge to
    	// value passed in by parameter age
    	
    	ItsAge = age;
    }
    must match both in,
    • the return type;
    • the function name;
    • the argument placement and types.


    As a example...

    int MyFunction(int x, double y, float z); is a declaration (also known as function prototype). The function is not being defined yet, just like what you did at the top of your class definition with SetAge. I thus declared a function called MyFunction that returns an integer and accepts an integer, a double integer and a floating-point as arguments and in this order.

    When later I define my function (just like you defined yours further down) I must respect my declaration. The only thing I may not pay attention to, is the argument names. In fact, my declaration above only needs the argument types. I could have written int MyFunction(int, double, float); instead.

    On your case, SetAge dos not return anything. It is meant to simply assign to one of the class data members. As such, it's your declaration that needs to be changed to return void. Leave the definition alone.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    ahh cheers done it.

    but why did this stop it from working?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The compiler error is somewhat misleading. What happened is that before it checked for your function matching, it realized you declared something without a type. In this case, when you removed your function return type.

    C supports default int. That is, any variable or function declared that that doesn't have a type specified is assumed to be int. C++ doesn't agree with this (I tend to agree with C++, hehe).

    Personally I don't even like the fact C++ defaults to signed... but I'm crazy like that

    Edited:
    I actually wrote C++ defaulted to unsigned. ugh.
    Last edited by Mario F.; 06-02-2006 at 04:44 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    C supports default int. That is, any variable or function declared that that doesn't have a type specified is assumed to be int. C++ doesn't agree with this (I tend to agree with C++, hehe).
    Neither does C99, the most recent version of C.

    Most C compilers will accept default-int style anyway, though.

    [edit]
    The reason K&R C supported default-to-int was to indicate that the function didn't return a meaningful value. This was before the keyword void was introduced. The void keyword was introduced because even if the default-to-int function didn't return a value, the compiler had to treat it as if it did, making it inefficient and prone to errors, becaue you might use the return value thinking it meant something.
    [/edit]
    Last edited by dwks; 06-02-2006 at 01:24 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM