0

I've got a namespace. The class is in a namespace.

namespace SALES
{
    class Sales
    {
    ...

I've created functions.

void SALES::Sales::set()
{
   ...

Now I'm creating an object SALES::Sales interactive ();

but I can't get to methods. Point my error out, please.

interactive.set(); // doesn't work

SALES::interactive.set(); // doesn't work

interactive.SALES::set(); // doesn't work

Mykola Tetiuk
  • 153
  • 1
  • 13
  • (1) Method 'set' could not be resolved. (2)Request for member 'set' in 'interactive', which is of non-class type 'SALES::Sales()' – Mykola Tetiuk Feb 22 '21 at 12:24
  • 3
    `SALES::Sales interactive ();` is parsed as function declaration. Try `SALES::Sales interactive {};` or simply `SALES::Sales interactive;` It's called [Most vexing parsing](https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse). – Lukas-T Feb 22 '21 at 12:24
  • 1
    Now it's obvious `SALES::Sales interactive;` Nothing to do with namespaces at all. Please in the future be a bit more descriptive than 'doesn't work'. – john Feb 22 '21 at 12:25
  • 2
    @13.tamil Completely irrelevant comment, and generally bad advice too. – john Feb 22 '21 at 12:26
  • Thanks to all of the comments. It seems, I have found the culprit: my default constructor was not full (all defaults for variables) and the function didn't create an object with `SALES::Sales interactive;` This is why I tried the () version – Mykola Tetiuk Feb 22 '21 at 12:29
  • 1
    IT WORKED! Thanks to everyone!!! Now it seems as a wrong question. Should I delete it? – Mykola Tetiuk Feb 22 '21 at 12:35
  • 1
    @MykolaTetiuk If it worked for you, there is a chance that it might help someone else. So consider not deleting it. – D-RAJ Feb 22 '21 at 12:39

1 Answers1

1

The way to access non-static members of a function is by using the member of object operator. For example,

class SomeObject {
public:
    SomeObject() {}

    void SayHello() { ... }
    
    int age = 10;
};

// When calling members.
SomeObject object;
object.SayHello();
int age = object.age;

And when instantiating, use SALES::Sales interactive; or SALES::Sales interactive{}; instead. If not when you try to do it like this: SALES::Sales interactive ();, C++ will try to resolve it as a function. Now in your case, the first; interactive.set(); needs to work.

If it still doesn't work, it might have to do with the accessibility of members. Classes by default make their members private; only the friend classes and itself can access those objects. So make sure that the members are public.

D-RAJ
  • 3,263
  • 2
  • 6
  • 24