Hack Forums
[C] Can't access functions inside class - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Can't access functions inside class (/showthread.php?tid=4926333)



Can't access functions inside class - Nicktron - Jul 28, 2015

So I just recently started learning c++, right now I am learning about classes.

My error: function "AccountManager::registerAccount" (declared at line 15) is inaccessible

My code:
Code
#include <iostream>
#include <string>

using namespace std;

int Main() {
    AccountManager AM;
    AM.registerAccount("Mik", "Pwd");
}

class AccountManager {
    string Username;
    string Password;

    void registerAccount(string User, string Pass) {
        Username = User;
        Password = Pass;
    }

    string loginAccount(string User, string Pass) {
        if (User == Username) {
            if (Pass == Password) {
                return "Success";
            }
            else {
                return "Error";
            }
        }
        else {
            return "Error";
        }
    }
};

Would really appreciate some help on this! :)

Edit: Do I need to prototype classes like you do with functions? If so, how do I do it? :p


* - 0xDEAD10CC - Jul 28, 2015

You have the class definition after main() instead of before it, and without any forward declarations to allow such an order. The other thing is that they are all marked implicitly as private... And it is NOT Main(), it's main(). Case matters.


* - Nicktron - Jul 28, 2015

(Jul 28, 2015 04:59 PM)0xDEAD10CC Wrote:  You have the class definition after main() instead of before it, and without any forward declarations to allow such an order. The other thing is that they are all marked implicitly as private... And it is NOT Main(), it's main(). Case matters.

Thanks, now I am able to get it working. What would I do if I would like to have my class under my main?


* - 0xDEAD10CC - Jul 28, 2015

Let this serve as an example:
Code
#include <iostream>

class myobj
{
public:
  void func();
};

int main()
{
  myobj o;
  o.func();
}

void myobj::func()
{
  std::cout << "func" << std::endl;
}

No clue why in the world you would want to do it this way though. Typically everything is included before main ideally, because you would be using the #include directive to include code. You would include the header file which contains the forward declarations, and the linker would match that off with the appropriate source file, but in that case, both definition and declaration would exist before main().


* - Nicktron - Jul 28, 2015

(Jul 28, 2015 05:27 PM)0xDEAD10CC Wrote:  Let this serve as an example:
Code
#include <iostream>

class myobj
{
public:
  void func();
};

int main()
{
  myobj o;
  o.func();
}

void myobj::func()
{
  std::cout << "func" << std::endl;
}

No clue why in the world you would want to do it this way though. Typically everything is included before main ideally, because you would be using the #include directive to include code. You would include the header file which contains the forward declarations, and the linker would match that off with the appropriate source file, but in that case, both definition and declaration would exist before main().

I'm coming from vb, I like keeping things under my main.. But thank you anyways!


* - 0xDEAD10CC - Jul 28, 2015

Well, get rid of that habit then.


* - Nicktron - Jul 29, 2015

(Jul 28, 2015 06:45 PM)0xDEAD10CC Wrote:  Well, get rid of that habit then.

I am going to! Thanks. :)