0

Firstly, I am giving the codes. Then I am explaining the problem I am facing.

main.cpp

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

using namespace std;
class mem;
int main()
{
    show();
    return 0;
}

acc.h

#ifndef ACC_H
#define ACC_H

#include "acc.cpp"
void show();

class mem{
    int a;
public:
    void showa();
    void seta(int A);
};
#endif

acc.cpp

#include <iostream>
using namespace std;

void mem::showa(){cout<<a<<endl;}
void mem::seta(int A){a = A;}

void show()
{
    mem m;
    m.seta(22);
    string ss;
    cin>>ss;
    cout<<"MY name is "<<ss<<" ";
    m.showa();
}

"mem" class I declared in "acc.h" file already and added that "acc.h" into acc.cpp file also. But when I am calling that class from a function. It can't response. Showing "a" and "mem" not declared. How can I perfectly link that class definition and member functions of that class so that calling member functions of that class from another function can't create any problem?

Rezwan
  • 23
  • 8

1 Answers1

1

If you remove the #include "acc.cpp" from the acc.h file it should compile without any errors. I tried and it compiles for me. I am using Visual Studio 2010 for the same.

Other than this, few more comments:

  1. You can use #pragma once in you header file instead of #ifndef/#define macros. The former is more cleaner.
  2. You dont need to forward declare class mem before main() as you are already including acc.h.
  3. the show() can be moved to where main() is defined making the acc.h/acc.cppfiles dedicated for the mem class.
  4. A header file should always be named after the class it is holding i.e. mem.h/mem.cpp in your case. This informs which file contains which class even without opening the file.
Sisir
  • 4,584
  • 4
  • 26
  • 37