Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function is inaccessible

Tags:

c++

I have

// file BoardInitializer.h
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>

using namespace std;
class BoardInitializer
{
    static int *beginBoard;
    static int *testBoard;
    static void testBoardInitialize();
}



// file mh.cpp
#include "BoardInitializer.h"

int main(int argc, char* argv[])
{
    BoardInitializer.testBoardInitialize();
    return 0;
}

and I implemented BoardInitializer::testBoardInitialize in mh.cpp. But I get the error "Function is inaccessible". What's wrong?

like image 868
Masoud Avatar asked Nov 28 '22 07:11

Masoud


1 Answers

The default protection level for a class in C++ is private (with the others being public and protected). That means all your members and your member function are private and only accessible by other member functions of that class or friends (functions or classes) of that class.

The function main is neither and you end up with the error.

C++ provides a handy shortcut (or C legacy cruft, depending on your worldview) called struct, where the default protection level is public.

class my_class {
public:
  int my_int;      
};

or

struct my_struct {
  int my_int;
};

should show the difference.

like image 176
pmr Avatar answered Dec 05 '22 10:12

pmr