David
David

Reputation: 159

Beginner at C++. Get errors in the code

I am completely new to c++, but I tried to run a code from a book I am reading about the subject. It is a very basic book for beginners, but it contains som codes which I have tried to write into c++ to learn how it works. I tried the following code, but I get two errors:

In function 'int main()':

[Error] expected ';' before 'emp'

#include <iostream>
#include <ctime>

class Employee
{
    private:
        int m_id;
    public:
        Employee(){}
        Employee(int id)
        {
            m_id=id;
        }
        int id;
        std::string firstname;
        std::string lastname;
        int birthyear;

        void ClockTime()
        {
            //clocktime code goes here
        }

        ~Employee()
        {
             m_id=0;
        }
};

int main()
{
    Employee emp(2);
    std::cout<<"The employee time clocked is"
    emp.ClockTime()<<std::endl;     
    return 0;   
}

Is there much I have to change in order for this code to work?

Upvotes: 0

Views: 138

Answers (1)

You broke a line while printing. It should be:

std::cout<<"The employee time clocked is " <<
emp.ClockTime()<<std::endl;    

Remember that white-space characters are ignored by the C++ compiler. So in your version, you had something like this:

std::cout<<"The employee time clocked is"emp.ClockTime()<<std::endl;

Which should make it clear to you why the error happens.


As gurka points out, you have numerous other problems with your code. Not the least of which is that you can't print a void (can't do much with it as a matter of fact). So ClockTime should return something.

Upvotes: 5

Related Questions