10 Replies - 1061 Views - Last Post: 08 March 2012 - 04:24 PM Rate Topic: -----

#1 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

How to create classes properly?

Posted 07 March 2012 - 05:08 PM

Basically in code::blocks, I have created a new class named Date. It automatically created the header file and the cpp file. I typed in some members in the header file, and the definitions in the cpp file.

Here are the contents:

.h file:


#ifndef DATE_H
#define DATE_H

class Date
{
    public:
        Date();
        bool good;
        void getData();
        void checkFormat(int &x);
        void validate();
        void display();
    private:
     int month, day, year;
};

#endif // DATE_H




.cpp file


#include "Date.h"
#include <iostream>

using namespace std;

Date::Date()
{
    //ctor
}
void Date::getData()
{
    cout << "Please enter month. day and year: "
    cin >> month >> day, year;
}
void Date::checkFormat(int &x)
{

}
void Date::validate()
{

}
void Date::display()
{
    cout << "The year is: " << year << endl;
    cout << "The month is: " << month << endl;
    cout << "The day is: " << day << endl;
}




I get the following error:

Compiling: src\Date.cpp
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:1:18: Date.h: No such file or directory
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:6: error: `Date' has not been declared
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:7: error: ISO C++ forbids declaration of `Date' with no type
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:10: error: `Date' is not a class or namespace
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp: In function `void getData()':
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:13: error: expected `;' before "cin"
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp: At global scope:
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:15: error: `Date' is not a class or namespace
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:19: error: `Date' is not a class or namespace
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:23: error: `Date' is not a class or namespace
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp: In function `void display()':
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:25: error: `year' undeclared (first use this function)
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:26: error: `month' undeclared (first use this function)
C:\Users\Renz Bagaporo\Desktop\Classes Problem\Chap12\src\Date.cpp:27: error: `day' undeclared (first use this function)
Process terminated with status 1 (0 minutes, 1 seconds)
12 errors, 0 warnings


What is wring with my program?

Is This A Good Question/Topic? 0
  • +

Replies To: How to create classes properly?

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: How to create classes properly?

Posted 07 March 2012 - 05:35 PM

Are you positive that they are saved in the same folder?

Check that.

Jack
Was This Post Helpful? 0
  • +
  • -

#3 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Re: How to create classes properly?

Posted 07 March 2012 - 07:27 PM

Well, no. Is that the problem? Code::blocks created it automatically like that, so I expected it work.

In terms of the code itself, are there problems?
Was This Post Helpful? 0
  • +
  • -

#4 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: How to create classes properly?

Posted 07 March 2012 - 07:47 PM

Well as of right now I see 2 errors. You need a semicolon on line 12, and you have a random "," on line 13. Compile it after that.

Also. Post your main function also.

Jack
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: How to create classes properly?

Posted 07 March 2012 - 07:50 PM

Quote

Well, no. Is that the problem? Code::blocks created it automatically like that, so I expected it work.


Data.h must be in the same directory as Data.cpp for the way it is being included
Was This Post Helpful? 0
  • +
  • -

#6 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Re: How to create classes properly?

Posted 07 March 2012 - 09:45 PM

Does code::blocks really behave this way? I mean, if they were supposed to be at the same directory, why did the IDE put them into separate places? I just boggles me.
Was This Post Helpful? 0
  • +
  • -

#7 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: How to create classes properly?

Posted 07 March 2012 - 09:49 PM

Have you fixed the 2 errors I pointed out?

Also, I have run this program in CodeBlock IDE and get no errors. Make sure that they are in the same directory. Just find the files and double check.

Jack

This post has been edited by jdavi134: 07 March 2012 - 09:51 PM

Was This Post Helpful? 0
  • +
  • -

#8 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Re: How to create classes properly?

Posted 08 March 2012 - 12:21 AM

View Postjdavi134, on 07 March 2012 - 09:49 PM, said:

Have you fixed the 2 errors I pointed out?

Also, I have run this program in CodeBlock IDE and get no errors. Make sure that they are in the same directory. Just find the files and double check.

Jack


Yes, I have done your suggestions of moving them to the same directory and those fixing those two errors you point them out. But is putting the .cpp file and .h file the default beahvior of code::blocks, or have I just failed to tick a certain setting?
Was This Post Helpful? 0
  • +
  • -

#9 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: How to create classes properly?

Posted 08 March 2012 - 12:33 AM

Hmmm. This is odd. What are the names of the files. Are they:

Date.h
and
Date.cpp

??

Jack
Was This Post Helpful? 0
  • +
  • -

#10 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 05-February 12

Re: How to create classes properly?

Posted 08 March 2012 - 04:14 PM

View Postjdavi134, on 08 March 2012 - 12:33 AM, said:

Hmmm. This is odd. What are the names of the files. Are they:

Date.h
and
Date.cpp

??

Jack


Yes. Except that date.h is inside an include folder and date.cpp is inside a source folder.
Was This Post Helpful? 0
  • +
  • -

#11 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: How to create classes properly?

Posted 08 March 2012 - 04:24 PM

View Postznerihc, on 08 March 2012 - 11:14 PM, said:

View Postjdavi134, on 08 March 2012 - 12:33 AM, said:

Hmmm. This is odd. What are the names of the files. Are they:

Date.h
and
Date.cpp

??

Jack


Yes. Except that date.h is inside an include folder and date.cpp is inside a source folder.



They MUST be in the same folder. You have to move the header file and source file into the same folder. Because I get no errors when running this myself.

Jack

EDIT: If I am understanding you correctly you are saying that they are both in the same folder, but are in 2 different folders within said folder?

This post has been edited by jdavi134: 08 March 2012 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1