Hi, my question is very simple. Like the title says; How to declare an object?
I have this declaration in my program;

Class_name Object_name;

and it is giving me an error, saying "Expected a ';' before "Object_name"

And this is driving me nuts because putting a ';' before the object name doesn't look like an object declaration.
Please help.
Thank you all.

Sounds like it doesn't know what kind of object Class_name is. Where did you tell it that Class_name is a type of a class?

Edited 1 Year Ago by Moschops

I have a header file named class_clock.h, and I declared a class named "clock" there. And in the main file I have a directive

include "class_clock.h"

It seems that the class "clock" is actually recognized. It is as though the format of declaring an object is incorrect. I have cygwin installed by the way and I am using Windows 8. If that helps.

If that helps.

Not really. What would help is paring down your code to the absolute minimum without eliminating the error and posting it here so we can see what's going down. Typically that error occurs when you have a syntax error somewhere above the line that's reporting it.

Certainly. This is the main.cpp file

#include<cstdlib>
#include <iostream>
#include "class_clock.h"
using namespace std;

int main(){
    int hr, min, sec;
    clock myclock;  //object declaration

    cout<<"Program with a class Clock"<<endl;
    cout<<"Values before assignment"<<endl;
    myclock.display();
    cout<<endl;
    cout<<"now enter values for hour, Minute and Second respectively"<<endl;

    cin>>hr>>min>>sec;
    myclock.set_time(hr, min, sec);
    myclock.display();

    return 0;
}

Coolio. Actually, clock is a horrid name because it's also the name of a function in the C library. Your implementation could very easily expose that even if you don't include ctime, so I'd strongly recommend using either a different name or dropping the class in a namespace.

That's most likely your issue. The compiler sees clock as a function rather than a type, so the declaration is malformed.

Edited 1 Year Ago by deceptikon

Oh my goodness! I do not know how to thank you. That worked!
I changed "clock" to "clocks" to minimize changes to just one letter.
By the way, in the future, I am to use a namespace instead, how do I do it?

Yay, my crystal ball still works. :)

By the way, in the future, I am to use a namespace instead, how do I do it?

It's super easy. You create a namespace like so:

namespace MyNamespace
{
    class clock
    {
        ...
    };
}

And use it like so:

MyNamespace::clock myclock;

There are a few extras like being able to nest namespaces and multiple namespaces with the same name get merged as if they were all one, but it's mostly intuitive how things work.

This question has already been answered. Start a new discussion instead.