Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:12:07+00:00 2026-05-26T13:12:07+00:00

I’m a C++ beginner. I have a class and when I try to compile

  • 0

I’m a C++ beginner. I have a class and when I try to compile it says it’s missing ‘main’.

What do I need to do to create an instance of this class and access its methods?

#include <string>
#include <iostream>

using namespace std;

class Account
{
      string name;
      double balance;

  public:
      Account(string n);
      Account(string n, double initial_balance);

      string get_name() const;
      double get_balance() const;

      void deposit(double amount);

      void widthdraw(double amount);

};

edit:

Where do I put the main method?

I have tried putting it in a different file like the following

    #include <Account>
    int main(){
    Account:: Account(string n) : name(n), balance(0){}
    return 0 ;
    }

but this gives an error saying there is not Account in the directory. I’m guessing this is because it’s not compiled

edit:

Both files are in the same directory

account_main.cc

    #include<string>
    #include <iostream>
    #include "Account.cc"

    Account:: Account(string n) : name(n), balance(0){} 
    int main()
    {
        Account account("Account Name"); // A variable called "account"
        account.deposit(100.00); // Calls the deposit() function on account

        return 0 ;
    }

Account.cc

#include<string>
#include <iostream>

using namespace std;

class Account
{
      string name;
      double balance;

  public:
      Account(string n);
      Account(string n, double initial_balance);

      string get_name() const;
      double get_balance() const;

      void deposit(double amount);

      void widthdraw(double amount);

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:12:07+00:00Added an answer on May 26, 2026 at 1:12 pm

    All C++ programs require what’s called an entry point. The main() function is always the entry point for standard C++ programs. You need to provide a main(), function otherwise the linker will complain. You can write a main() function in one of two ways:

    int main()
    {
        return 0;
    }
    

    Or, if you are expecting command-line arguments:

    int main(int argc, char ** argv)
    {
        return 0;
    }
    

    Note that void main() is not valid in C++. Also note that a return statement isn’t strictly necessary for main() functions, but you should explicitly write one anyway, for the sake of consistency.

    C++ Standard 3.6.1 Main function [basic.start.main]

    5. A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and
    calling exit with the return value as the argument. If control reaches
    the end of main without encountering a return statement, the effect is
    that of executing

        return 0;
    

    To answer your question about your latest edit:

    #include <Account>
    
    int main(){ 
        Account:: Account(string n) : name(n), balance(0){} 
        return 0 ; 
    } 
    

    The form of main() is correct, but this is not how you provide class member definitions. The constructor needs to be outside the main function.

    #include <Account>
    
    // Outside of main()
    Account::Account(string n) : name(n), balance(0)
    {
    } 
    
    int main()
    { 
         return 0 ; 
    } 
    

    To create an instance of Account, you declare a variable and pass all the required constructor arguments like this:

    int main()
    {
        Account account("Account Name"); // A variable called "account"
        account.deposit(100.00); // Calls the deposit() function on account
                                 // Make sure you provide a function
                                 // definition for Account::deposit().
        return 0;
    }
    

    Also, check the exact file path of where class Account is. If the Account class is in a file called Account.h and is in the same folder as the file containing the main() function, then you need to use #include "Account.h" instead of #include <Account> in that file. For example:

    #include "Account.h" // Note .h and quotes
    
    Account::Account(string n) : name(n), balance(0)
    {
    } 
    
    int main()
    {
        // ...
        return 0;
    }
    

    This is actually a rather fundamental aspect of the C++ programming language. Surely you have a book that covers this? In fact, main() functions and #include statements are usually the first thing that you learn when programming in C++. I highly recommend that you pick up a good C++ book and read through them and do the exercises.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.