problem with scope operator

I am having difficulty sing objects with the scope operator (::). I don't have any issues writing for (.) or (->) but I am doing something really ridiculous with (::).

In main, I create the class:
1
2
3
4
5
6
7
class Test
{
	
       private:
       public:
	void print_it();
};

I left out the constructor and destruct or info and some of member variables to expedite.
1
2
3
4
5
6
7
void Test::print_it()
{
	std::cout << "Test member scope" << std::endl;
}

Test test;
test::print_it();

I instantiate the Test class and try to call the function without luck. What am I doing wrong here?


Severity	Code	Description	Project	File	Line	Suppression State
Error	C2510	'test': left of '::' must be a class/struct/union	ArduCam_test	c:\users\clang\desktop\working folder\opencv\xxxx_test\xxx_demo.cpp	58	
It's three different use cases.

. is used to access an object's member function.

-> is used to access a pointer-to-object's member function. It is equivalent to (*obj).member.

:: is used to access a static member of a class (and can also be used within the class member functions to disambiguate certain members, but don't worry about this yet.)

You want:
1
2
Test test;
test.print_it();


Here's a example to demonstrates the syntax (although doesn't do anything useful)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Example program
#include <iostream>
#include <string>

class Bunny {
  public:
    // non-static member variable
    int weight;
    
    // non-static member function
    void hop() { std::cout << "hippity hop!\n"; }
    
    // static class constant
    const static int Legs = 4;
    
    // static function
    static void bun() { std::cout << "bun!\n"; }
};

int main()
{
    Bunny bunny;
    
    bunny.weight = 32;

    Bunny* p_bunny = &bunny;
    p_bunny->weight = 42;

    bunny.hop();
    p_bunny->hop();
    (*p_bunny).hop();

    std::cout << Bunny::Legs << "\n";
    Bunny::bun();
}
Last edited on
I don't want to "critique" the answer but it was thorough, clear, concise and solved the problem. Thx!
Topic archived. No new replies allowed.