Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Can't access functions inside class

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
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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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?
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().
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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!
Well, get rid of that habit then.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Jul 28, 2015 06:45 PM)0xDEAD10CC Wrote:  Well, get rid of that habit then.

I am going to! Thanks. :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C] by minerwOct 7, 2020 05:44 AM 1 117 Oct 7, 2020 05:44 AM
Last Post: hell-o-world
[Python] by CharlyhackrNov 9, 2019 11:43 AM 2 101 Nov 9, 2019 11:43 AM
Last Post: Charlyhackr