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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With