简体   繁体   English

将接口与实现分开不起作用 C++

[英]Separating interface from implementation isn't working c++

Hey i was trying to use Separating interface from implementation but got error.嘿,我试图从实现中使用分离接口,但出现错误。 Not understanding what's wrong.不明白怎么回事。 Here's my program here's the error image这是我的程序这是错误图像

#include<iostream>
#include"name.h"
using namespace std;
int main()
{
    int x,y;
    cin>>x>>y;
    name n1(x,y);
    n1.getdata(x,y);
    n1.showdata();
}

now here's the created header file现在这是创建的头文件

#include<iostream>
using namespace std;
class name{
private:
    int a,b;
public:
    name(int x, int y);
    void getdata(int x, int y);
    int showdata();
};

& here's the next part of class &这是课程的下一部分

#include"name.h"
using namespace std;
name::name(int x, int y)
{
    a=0;
    b=0;
}
void name::getdata(int x,int y)
{
    a=x;
    b=y;
}
void name::showdata()
{
    cout<<a+b;
}

There are many problems with your code.你的代码有很多问题。 It looks like the best advice in this situation would be to read a good C++ book .看起来在这种情况下最好的建议是阅读一本好的 C++ 书

When this is out of the way, here's the short-list of problems in the severity-descending order:当这不碍事时,以下是按严重性降序排列的问题的简短列表:

  • name::showdata() declaration signature does not match difinitioin: int showdata() vs. void showdata() name::showdata()声明签名与定义不匹配: int showdata() vs. void showdata()
  • header misses an include guard标题错过了一个包含保护
  • using namespace in a header is a code smell 99 times out of 100在标题中using namespace是 100 次中的 99 次代码异味
  • header does not need to include <iostream> , it would suffice to include it in implementation file, where it's actually used. header 不需要包含<iostream> ,将它包含在实际使用的实现文件中就足够了。

By looking at undefined references you are getting, I would also guess that name.cpp is not build.通过查看您获得的未定义引用,我也猜想name.cpp不是构建的。

I fixed some of the mentioned points to just make it build: Live Demo我修复了一些提到的要点以使其构建: Live Demo

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

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