-4

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;
}
  • 4
    which error do you get? – AMA Oct 16 '17 at 15:58
  • @yeasir arafat shahed The parameters in the constructor name::name(int x, int y) { a=0; b=0; } are not used. So the declaration of the constructor does not make sense. – Vlad from Moscow Oct 16 '17 at 16:00
  • @yeasir arafat shahed It is better to name the method getdata as setdata or even set_data – Vlad from Moscow Oct 16 '17 at 16:01
  • That `name` constructor looks bad for two reasons: 1) it should use the initialization list, not the constructor body (and yes, it matters for performance in many cases). 2) it doesn't actually use the arguments passed in. – Jesper Juhl Oct 16 '17 at 16:16
  • `name::getdata` looks mis-named. Shouldn't it be named `name::setdata` since that's what it does; it *sets* the member variables. – Jesper Juhl Oct 16 '17 at 16:19
  • 2
    Look at `showdata`’s prototype in the header. Then look at `showdata`’s implementation. Keep looking until you spot the difference. – molbdnilo Oct 16 '17 at 16:19
  • 1
    my 2c: `name.h` misses header guard; `using namespace` in header is usually [bad](https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers) – AMA Oct 16 '17 at 16:22
  • `using namespace std;` - please don't. You are completely obliterating the whole point of having namespases in the first place. *Especially don't do that in a header* where you will impose your bad choice on all users. Using `using namespace` in a header is a *huge* code smell. – Jesper Juhl Oct 16 '17 at 16:23
  • @AMA s Folder\Study\CSE107\Practice\Practice 16.10\2.o:2.cpp|| undefined reference to `name::name(int, int)'| s Folder\Study\CSE107\Practice\Practice 16.10\2.o:2.cpp|| undefined reference to `name::getdata(int, int)'| s Folder\Study\CSE107\Practice\Practice 16.10\2.o:2.cpp|| undefined reference to `name::showdata()'| ||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===| – yeasir arafat shahed Oct 16 '17 at 16:25
  • 1
    Please put the error-info inside the question, so that everyone can see it. – AMA Oct 16 '17 at 16:26
  • May I recommend reading [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jesper Juhl Oct 16 '17 at 16:27

1 Answers1

2

There are many problems with your code. It looks like the best advice in this situation would be to read a good C++ book.

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()
  • header misses an include guard
  • using namespace in a header is a code smell 99 times out of 100
  • header does not need to include <iostream>, it would suffice to include it in implementation file, where it's actually used.

By looking at undefined references you are getting, I would also guess that name.cpp is not build.

I fixed some of the mentioned points to just make it build: Live Demo

AMA
  • 4,114
  • 18
  • 32
  • I have tried following your instruction edited some mistakes [check this out](https://imgur.com/Oi7oh63) but the error is still there [error](https://imgur.com/er4BcrA) – yeasir arafat shahed Oct 17 '17 at 07:40
  • @yeasirarafatshahed I guess that `name.cpp` is not added to the build. Probably it's a problem in your project configuration and not in the code. Please check your configuration and make sure that `name.cpp` is added. – AMA Oct 17 '17 at 07:59