0

Greetings I am getting this problem "Expression must have class type" -the code is something like this it's made to protect constant values-

Class.h

Class Myclass{
    Public: My class();
    Private: 
    const int value;
};

Class CPP

Myclass::Myclass()
   :value(5)
   {}

Main

#include "Myclass.h"
#include <iostram.h>
int main()
{
    Myclass Smth();
    int five=Smth.value; // error line  
                         // Smth is underlined       
    return(0);
}

thanks

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 4
    Please include a [mcve] and the error message in the question. The code as posted has lots of typos... – 463035818_is_not_an_ai Jan 20 '20 at 14:18
  • 1
    No. It is because `value` is private. Also, your code is full of syntax errors. You don't start keywords like `public` with a capital. You can't separate words like `My class`. – CoderCharmander Jan 20 '20 at 14:19
  • 1
    `Class` should be `class`, `Public` should be `public`, `My class();` should be `Myclass();` and maybe more – 463035818_is_not_an_ai Jan 20 '20 at 14:20
  • 2
    This is a most vexing parse. `Myclass Smth();` is a function declaration. And, of course, lots of keywords with incorrect capitalization, as everyone is pointing out. – Max Langhof Jan 20 '20 at 14:21
  • That's not a copy of the code, the code had no syntax errors, I had tried it with public and private members as well as with const int and just int type. – user12684739 Jan 20 '20 at 15:17
  • "That's not a copy of the code, the code had no syntax errors" why? We can only answer on the code you post. If your question is not about those syntax errors you should have removed them. Now it is too late, because the answers do answer your question as it stands (including the syntax errors) – 463035818_is_not_an_ai Jan 20 '20 at 15:25
  • Still that doesn't solve the problem – user12684739 Jan 20 '20 at 15:36
  • `Myclass Smth(); ` is considered as a function declaration with return value `Myclass` you need to initialize the object as `Myclass Smth;` – TruthSeeker Jan 22 '20 at 06:10

2 Answers2

2

You're spelling class, private, and public incorrectly and you can't have spaces in names:

Class Myclass {
    Public: My class();
    Private: 
    const int value;
};

should be:

class Myclass {
    public: Myclass();
    private: 
    const int value;
};

and you can't access value outside of the Myclass because it's private.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

You can't access a private member. Also, this line is better to be Myclass Smth;.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
  • That's not a copy of the code, the code had no syntax errors, I had tried it with public and private members as well as with const int and just int type – user12684739 Jan 20 '20 at 15:18