hello , i am a newbie in c++ and desperately wanna learn it. icoded this but i m gettin some error

# include<iostream>
using namespace std;
class cat {
 public:
 unsigned short int age;
public:
 void meow ()
{
 cout<<"MY AGE IS "<<age<<endl;
}
# include"cat.cpp"
 int main()
{
cat tom;
tom.age=12;
tom.meow();
return 0;
}

when i compile with

cc -o 1 1.cpp

i get following erors:

1.cpp:9: error: expected `}' at end of input
1.cpp:9: error: expected unqualified-id at end of input

Bothe class and client program are in the same folder.Please help me.Thanks in advance.

regards
CD-4+

Opening curly braces need to be matched with closing curly braces. The braces keep the stuff inside from falling out and choking the compiler. :D

// class definition
class MyClass
{
public:
    // public stuff
private:
    // private stuff
}; // close the definition
// method/function definition
void MyFunction()
{
    // stuff to do
} // close the definition

Please can you be a more specific specially void myfunction?? Im really noob.

regards
CD-4+

That example was just to show that the opening curly brace had a corresponding closing curly brace. :) Your error is coming from the class definition not having a closing curly brace and semicolon:

#include <iostream>

using namespace std;

class cat
{
public:
    unsigned short int age;
public:
    void meow()
    {
        cout << "MY AGE IS " << age << endl;
    }
}; // add this line

well i again rewrote the whole code..

# include<iostream>
using namespace std;
class cat {
public:
 int age;
 int weight;
public:
 void meow ();
{
 cout << "I m " <<age << "years old & of " <<weight <<"Kg weight."<<endl;
}
};
# include"cat.cpp"

int main()
{
 cat tom;
 tom.age=5;
 tom.weight=10;
 tom.meow();
 return 0;
}

when i compile on my linux i get

In file included from 1.cpp:1:
cat.cpp:9: error: expected unqualified-id before ‘{’ token

please help me.. i m sorry if there is some minor error..

regards
CD-4+

Hello CD-4+,

While I am still pretty new to C++, I believe that the compiler error that you are receiving is due to the fact that you have a semi-colon in your class definition of the meow() function:

# include<iostream>
using namespace std;
class cat {
public:
 int age;
 int weight;
public:
 void meow ();
{
 cout << "I m " <<age << "years old & of " <<weight <<"Kg weight."<<endl;
}
};

Instead you want to have the following:

# include<iostream>
using namespace std;
class cat {
public:
 int age;
 int weight;
public:
 void meow () //removed the semi-colon from here//
{
 cout << "I m " <<age << "years old & of " <<weight <<"Kg weight."<<endl;
}
};

When you are defining the function, you do not want to have the semi-colon after it; only when you are doing a prototype declaration or calling it. I hope that helps solve the problem.

-D

hi .. thanks for pointing out the mistake.. now its working fine as well as i got the concept clear.. thanks dgr231

regards
CD-4+

No problem. If everything is working well, then please mark this thread as solved.

Thanks.

-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.