0

I have a very simple problem. I am trying to learn C++ and I'm having a little problem. Here's the code

system.h

#include <iostream>
#include "processor.h"
using namespace std;

class sys
{
    public:
        int id;
        sys()
        {
            id=0;
        }
};

processor.h

#include <iostream>
using namespace std;
class proc
{
    public:
    const sys* s1;
    s1=new sys();
};  

The error says

"error C2512: 'sys' : no appropriate default constructor available" 

There is a default constructor.

I am a beginner at C++ so please explain what I am doing wrong. Thank You.

1
  • 1
    You really don't want to have a global using namespace directives in header files. Commented Sep 22, 2013 at 12:31

2 Answers 2

2
  • You do not initialize class members inside the class body, You do so in the class constructor.
  • const members are special members and they must be initialized in the Member initialization list.
  • Avoid using dynamically allocated memory as much as possible and if you must use smart pointers instead of raw pointers.

proc() : s1(new sys())
{
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since class proc is using class sys the include order should be the opposite:

// sys.h
class sys {
   ...
};

// process.h
#include "sys.h"

class proc {
   ...
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.