0

I am learning c++, in particular, I am learning about inheritance. I wrote the following code where I wanted to print the contents of protected_stuff, a variable defined within the protected access specifier in the MainClass.

Here is my code:

  • inheritance.cpp
#include<iostream>
#include"MainClass.h"
#include"DerivedClass.h"


int main(){

    DerivedClass a;
    a.func();
    return 0;
}
  • DerivedClass.h
#ifndef DERIVEDCLASS

#define DERIVEDCLASS

class DerivedClass: public MainClass{
private:
    int val;
protected:
    int val2;
public:
    int val3;

    void func(void){
        std::cout<<protected_stuff;
    }

};

#endif
  • MainClass.h
#ifndef MAINCLASS
#define MAINCLASS


class MainClass{

private:
    int value;
    char charecter;

    value= 10;
    charecter='a';

protected:
    int protected_stuff;
    protected_stuff = 2;


public:
    int public_stuff;
    public_stuff = 3;

};

#endif

When I try to run g++ -I . inheritance.cpp I get the following errors:

In file included from inheritance.cpp:2:
MainClass.h:11:2: error: 'value' does not name a type
   11 |  value= 10;
      |  ^~~~~
MainClass.h:12:2: error: 'charecter' does not name a type
   12 |  charecter='a';
      |  ^~~~~~~~~
MainClass.h:16:2: error: 'protected_stuff' does not name a type
   16 |  protected_stuff = 2;
      |  ^~~~~~~~~~~~~~~
MainClass.h:21:2: error: 'public_stuff' does not name a type
   21 |  public_stuff = 3;
      |  ^~~~~~~~~~~~

Later I modified MainClass.h and the code works fine.

#ifndef MAINCLASS
#define MAINCLASS


class MainClass{

private:
    int value= 10;
    char charecter='a';

    // value= 10;
    // charecter='a';

protected:
    int protected_stuff= 2;
    // protected_stuff = 2;


public:
    int public_stuff = 3;
    // public_stuff = 3;

};

#endif

My question is what was I doing wrong? I tried looking at various other questions on SO but couldn't find something similar:

  • 2
    You can't have code (for example assignments like `value= 10;`) outside of functions. Same as https://stackoverflow.com/questions/33335341/error-c-does-not-name-a-type and https://stackoverflow.com/questions/16938810/y-does-not-name-a-type-error-in-c – Lukas-T Aug 01 '20 at 06:17
  • 1
    I should have properly read **"Y does not name a type" error in C++**... Please close this question as a duplicate. – Sabito stands with Ukraine Aug 01 '20 at 06:23
  • 2
    The one you linked "Correct place to initialize class variables?" is fundamentally the same problem: both you and that OP are trying to treat an *instance* variable as if it belonged to the class. Note that in some *other* languages (e.g. Python), analogous code *would* create something that belongs to the class. – Karl Knechtel Aug 01 '20 at 06:27

1 Answers1

2

Your errors have nothing to do with inheritance. The same code would be an error without any inheritance.

In C++ statements like value = 10; must be placed inside functions or constructors. So this is OK (statement inside a constructor)

class MainClass{
    int value= 10;
    MainClass()
    {
        value = 10;
    }
};

so is this (statement inside a function)

class MainClass{
    int value= 10;
    void function()
    {
        value = 10;
    }
};

On the other hand int value = 10; is a declaration so it can go outside of a function.

This is very basic C++ syntax and semantics. You need to understand the difference between a statement and a declaration. It suggests to me that you need to spend some more time learning the basics of C++ before you tackle inheritance.

john
  • 85,011
  • 4
  • 57
  • 81