-5

expected member name or ';' after declaration specifiers

This is the error I am getting and I am not sure where it wants me to put the semi-colon as I think I have placed them all correctly. This is the section of the code where the error occurs.

class Read : public Data {
    
public: {   
      
      float result_1 = sensor_stream1.get_data(&state);
      float result_2 = sensor_stream2.get_data(&state);
      float result_3 = sensor_stream3.get_data(&state);
      
      std::cout << result_1 << '\t';
      
    };
  };

On the bracket after the second public is where it says the error is. The data class that this class is inheriting from doesn't show any errors whilst writing or when I have run the code.

Any help would be much appreciated.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Bateman
  • 13
  • 2
  • Create a [mcve] – eerorika Apr 09 '21 at 14:15
  • 2
    Is there supposed to be a function signature somewhere in there? You just have `public:` and then a block of code. – Nathan Pierson Apr 09 '21 at 14:15
  • 2
    You appear to have provided a function body without a function. – Adrian Mole Apr 09 '21 at 14:15
  • 3
    What is this `public: { `? Why `std::cout << result_1 << '\t';` in (seemingly) class scope? It must be in function scope. You cannot learn C++ by guessing. Consider learning C++ from [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Apr 09 '21 at 14:16
  • 3
    The error is a red herring. Compiler is too confused with syntax, so it proposes a semicolon, when in fact you are missing function syntax before a body. – Yksisarvinen Apr 09 '21 at 14:17
  • The declaration specifier is `public:`. It is normally followed by a member declaration, but the syntax also allows a (meaningless) semicolon in many places, including there (`class A { public:;;;;};` is a valid nonsensical class definition). Being confused by your `{`, the compiler has no idea what you're trying to say and gives up at `{`. – molbdnilo Apr 09 '21 at 14:47

1 Answers1

1

You can't put arbitrary code in a class definition. Only instance variable and function declarations belong in there. If you intend that the code be run during the constructor, make that explicit.

class Read : public Data {
public:
  Read() { 
  
    float result_1 = sensor_stream1.get_data(&state);
    float result_2 = sensor_stream2.get_data(&state);
    float result_3 = sensor_stream3.get_data(&state);
  
    std::cout << result_1 << '\t';
  
  }
};
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116