user379888
user379888

Reputation:

Problem In Separating The Interface And The Implementation

Its the first time I am trying to separate the class in a separate header file but I am getting an error.Please help me out.Thanks CODE:

My main function:

#include <iostream>
#include <MyClass>
int MyClass::data;
int main()
{
    cout<<"data="<<MyClass::data;
    system("pause");
    return 0;
}

MyClass.h

#ifndef MyClass
#define <MyClass>
class MyClass
{
    static int data_;
    
};
#endif

Error: fatal error C1083: Cannot open include file: 'MyClass.h': No such file or directory

Upvotes: 1

Views: 212

Answers (3)

husseincoder
husseincoder

Reputation: 1

use #include "Myclass.h" instead of #include

Upvotes: 0

jdehaan
jdehaan

Reputation: 19938

First good idea to separate definition and implementation in C++. Your #include directive shall use " and not < > as your header is not a system header. Or your header is not lying inside the same directory than the cpp file.

That is another topic but OO is more than just using some classes. Encapsulating static variables inside a class doesn't make them less global... At least they have another namespace...

Upvotes: 2

6502
6502

Reputation: 114559

You should use

#include "MyClass.h"

angle brackets are for system headers.

Also it's data or data_?

Also it would be better something like

#if !defined(MYCLASS_H_INCLUDED)
#define MYCLASS_H_INCLUDED

...

#endif

#define-ing a name identical to the class name is going to be a source of problems

Upvotes: 5

Related Questions