0

data.h

namespace Data{
    class Enviroment{
        private:
            struct EnviromentData {
                float temprature;
                float pressure;
                float light;
            };            

        public:
            float ReturnTemprature();
            float ReturnPressure();
            float ReturnLight();

            void SetTemprature(float);
            void SetPressure(float);
            void SetLight(float);

            void SetAll(float, float, float);
    };
};

data.cpp

#include "data.h"

struct EnviromentData envData;

float ReturnTemprature(){
    return envData.temprature;
}
    
float ReturnPressure(){
    return envData.pressure;
}

float ReturnLight(){
    return envData.light;
}

void Set(float temprature, float pressure, float light){
    envData.temprature = temprature;
    envData.pressure = pressure;
    envData.light = light;
}

On the line struct EnvirometnData envData; I have the

error Variable has incomplete type 'struct EnviromentData'

I'm not sure what I have done wrong, my implementation is based of the top answer to this stackover question

Oliver Guy
  • 71
  • 8
  • 2
    `EnvironmentData` is both `private` and exists in the scope `Data::Enviroment`. Your line `struct EnvirometnData envData;` accounts for neither of those things. Should the type be private? Should the type be named `Data::Enviroment::EnviromentData`? Unrelated to the question, should `Enviroment` have its spelling fixed to `Environment`? – Drew Dormann Apr 11 '22 at 15:11
  • `struct EnviromentData envData;` -- The second thing is that the keyword `struct` is not required here. Looks like you're looking at `C` language related code and/or material. – PaulMcKenzie Apr 11 '22 at 15:18
  • @DrewDormann Thanks for pointing out the spelling error. Maybe I'm fundamentally misunderstanding the use of a header file. But I though it was used to define the .cpp file. So I want a class Environment within the Data namespace, and it has a struct that is not accessible outside an instance of that class. And there are several public methods. Then I define what those methods actually do within the .cpp file. – Oliver Guy Apr 11 '22 at 15:19
  • 1
    A header file does very little. All it does is `#include <>` literally takes the text inside that header file, and replaces the `#include` with that text. It is not some fancy "import" that Java or other languages have. – PaulMcKenzie Apr 11 '22 at 15:19
  • [As illustrated here](https://godbolt.org/z/Eo7PTP7bY) this code and this error have nothing to do with header files. Placing some of the code in a header file changes nothing. – Drew Dormann Apr 11 '22 at 15:21
  • Side-note: Misspelling "Environment" as "Enviroment" is going to bite someone (you, or someone else) eventually. Same goes for "Temperature" vs. "Temprature". – ShadowRanger Apr 11 '22 at 15:22
  • @OliverGuy -- To add, a header file is just a convenient way to have declarations in one file, so that you don't have to copy/paste the same definitions (or worse, type them in over and over again) when you have two or more C++ modules. It is nothing more than that. If you want to dare to do it, you can literally take any header file you're including now, and copy/paste the text in that file to wherever you have `#include` in one of your c++ modules, then remove the `#include` directive. You will see that your code compiles with no errors, just as if you had `#include` there. – PaulMcKenzie Apr 11 '22 at 15:28
  • @PaulMcKenzie Okay I understand. If I had my code in data.cpp, with the struct defined as what it is in the header. How should the header file that defines that class look? – Oliver Guy Apr 11 '22 at 15:33
  • @OliverGuy -- Are you aware that the name is `Data::Enviroment::EnvironmentData`? That's the purpose of namespaces, and that is to disambiguate the name. The `Data::` is part of the name. A `Data::Enviroment::EnvironmentData` is not a `Enviroment::EnvironmentData`, just as it wouldn't be a `Paul::Enviroment::EnvironmentData`. Maybe that's the concept you're missing, and that concept is knowing what the actual "global" name of the types are when given a namespace. – PaulMcKenzie Apr 11 '22 at 15:36
  • You also seem to be unsure about what `private` means. You have declared the struct private and are now asking why a new instance can not be declared in the global scope as if it were `public`. – Drew Dormann Apr 11 '22 at 15:43

1 Answers1

0

You should supply the namespace(Data::Enviroment::EnviromentData) and make EnviromentData public.