简体   繁体   English

接口分离与实现中的问题

[英]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 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 错误:致命错误C1083:无法打开包含文件:'MyClass.h':没有此类文件或目录

You should use 你应该用

#include "MyClass.h"

angle brackets are for system headers. 尖括号用于系统接头连接器。

Also it's data or data_ ? 也是datadata_吗?

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 #define define-提供与类名称相同的名称将成为问题的根源

First good idea to separate definition and implementation in C++. 将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. 您的#include指令应使用"而不是< >因为您的标头不是系统标头。或者您的标头与cpp文件不在同一目录中。

That is another topic but OO is more than just using some classes. 那是另一个话题,但是OO不仅仅是使用某些类。 Encapsulating static variables inside a class doesn't make them less global... At least they have another namespace... 将静态变量封装在类中不会使它们的全局性降低...至少它们具有另一个名称空间...

使用#include“ Myclass.h”代替#include

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM