0

I have a file like this:

#include <list>
#include "term.h"

class Term {

};

class Number : public Term {
private:
    double m_number;

    Term* m_exponent = nullptr;

public:
    Number(double number) {
        m_number = number;
    }

    Number(double number, Term* exponent) {
        m_number = number;
        m_exponent = exponent;
    }

    double getNumber() {
        return m_number;
    }

    void setExponent(Term* exponent) {
        m_exponent = exponent;
    }

    Term* getExponent() {
        return m_exponent;
    }
};

and a header file term.h like this:

#pragma once

class Term;

class Number;

Now in my main function, if I try to declare a Variable of type Number (Number a(10.0)), I get the following error: Incomplete Type is not allowed.

#include "term.h"

int main() {
   Number a(10.0);
}

I don't understand where this is coming from. Any help would be appreciated. Thanks in advance! :)

Reskareth
  • 47
  • 4

1 Answers1

0

Okay, a quick tutorial.

C++ separates the concept of the definition of a class and the implementation. This makes it different from languages like Java. Now, you can, if you want, inline portions of your class in the definition, but you don't have to.

Your .h file should have things like:

class Foo { ... the full definition };

And then in your .cpp, you implement anything you didn't do in the .h:

void Foo::someMethod() { ... }

What is going on is that your main.cpp has absolutely no idea what a Number class is. It knows it's a class, but that's it. And it needs to know.

Basically, everything in your first snippet needs to appear in the .h file so that other .cpp files can see what it is.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36