Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
test.h file
#ifndef TEST_H
#define TEST_H

public:
bool yesornot(int a);
int returnnum(int b,int c,int z);

#endif



test.cpp file
#include "stdafx.h"
#include "test.h"

bool yesornot(int a){
    if (a==1)
        return true;
    else
        return false;
}



when compile it giv me error C2059: syntax error : 'public'
how should i use public/private to control my function in c++?
Posted

The keywords public and private apply to class members. Your function is global; it is not a member of any class.
 
Share this answer
 
Comments
tuolesi 12-Jul-10 8:54am    
thx...
If you want to change the visibility of plain functions, you cannot use public, protected and private; these keywords are specific to class members.

To achieve your goal keep in mind the rules below:


  • a public class method could be called from everyone, not only from inside another method of the class.
    In a similar way, if you want your function to be accessible from other parts of your code, declare it inside an header file (i.e. *.h) and implement it inside a C/C++ code file (i.e. *.c or *.cpp).
    This way, all modules that include your header file will be aware of your function and will be able to call it.
  • a private class method could be called only from inside another method of the class.
    In a similar way, if you want your function to be hidden to modules other than the one where it is implemented, do not declare it on an header file. Instead declare it directly on the module where it is implemented, and declare it as static.
    This way, the function will be defined only in the scope of the module where it is implemented.


Here below is an example:


  1. inside example.h
    C++
    // The function above is available for all modules that include example.h
    int __cdecl MyGlobalFunction(int a, int b);

  2. inside example.cpp
    C++
    #include "example.h"
    
    // The function below is available only from example.cpp
    static int MyPrivateFunction(int a, int b)
    {
       if (a > b) return a - b;
       else return b - a;
    }
    
    // Below is the implementation of MyGlobalFunction, which is defined in example.h
    int __cdecl MyGlobalFunction(int a, int b)
    {
       return MyPrivateFunction(abs(a), abs(b));
    }

 
Share this answer
 
Comments
tuolesi 12-Jul-10 8:51am    
thx for ur guide....
Have a look at the following links of how to use those modifiers:
Public versus Private members in C++[^]
MSDN:private (C++)[^]
MSDN: public (C++)[^]
 
Share this answer
 
Comments
tuolesi 12-Jul-10 8:54am    
thx...
btw i wonder why most c++ tutorial do not add a note state that" keywords public and private apply to class members only....."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900