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); }; 
4
  • 4
    If this is homework then don't you have a book you're reading out of? I'm quite certain it must cover something this fundamental. Commented Oct 27, 2011 at 18:28
  • Page about main in C++. Commented Oct 27, 2011 at 18:29
  • What are you learning from? I think you need one or more of the books listed here: stackoverflow.com/q/388242/10077 Commented Oct 27, 2011 at 18:34
  • 2
    Are you defining the class constructor in the definition of main? Really, even if it's homework, get a good C++ book and work on the very basic fundamentals. Commented Oct 27, 2011 at 18:37

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for that. I have added to the original post please have a look.
I have the main method in main_account.cc it is in the same folder as account.cc. when i try to compile main_account.cc it says Account cant be found in the directory. I've tried #include<Account.cc> and #include<Account> but it doesn't work. also there is no executable of Account in that directory and I can't make it because when I try to compile Account it says no main method
@user521180: What file is class Account located in? Is it called Account.h? Because you need to also specify the file extension in include statements. Also, when you compile a file you pass the file path to the source file, not the header file i.e. main_account.cc, not Account.
I've added the content of both files to the original message please have a look and post a fixed version if you can. thanks
@user521180: Rename Account.cc to Account.h and do a #include "Account.h" in Account_main.cc.
|
1

For your latest edit: not Account in the directory

try this:

#include "Account.h" //quotes, and .h Account:: Account(string n) //this must be outside of main, it's its own function : name(n), balance(0) //I put these on three seperate lines but {} //that's a personal choice int main(){ //this is what the code should do when it starts Account person1("George"); //create a Account called person1 with the name George cout << person1.get_name(); //c-output person1's name. return 0 ; //return success (success=0) } 

4 Comments

#include "Account.cc" worked. the class with the main method is compiling but Account.cc hasn't compiled yet is that normal?
When you #include a file, it basically copy-pastes that file into the current one, so the stuff in that file is indeed getting compiled. Why do you say it isn't compiling?
Find someone (in person) to teach you to use whatever your compiler is. There's a lot of steps and concepts you're missing.
#include basically pastes the contents of the named file at the location of the include directive. So with #include "Account.cc" the contents of Account.cc is compiled as part of main_account.cc. Generally it's considered bad practice to include .cc files like this. Instead you should use a '.h' header file and figure out how to tell what goes in a header vs. what goes in a .cc file. In your case you just need to take out #include <iostream> and using namespace std;, add the right instances of std::, and then rename Account.cc to Account.h.
1

as has been pointed out, you need a decent book. To answer your questions you should know the following: Classes are usually defined in .h file and implemented in .cpp or .cc file. You should have three files: Account.h, Account.cc and main.cc. You only compile the .cc files and the .h files are included in the parts of code where you need to know something about the class (i.e. when you are actually doing something with the class). If you are using g++ (linux, unix or mingw I think) you can compile the program using the following command: g++ main.cc Account.cc -o program_name

main.cc:

#include <iostream> using namespace std; #include "Account.h" int main() { Account a("first account"); cout << a.get_balance() << endl; a.deposit(100.0); cout << a.get_balance() << endl; return 0; } 

Your Account.h file should look like this:

#include<string> //#include <iostream> -- not needed here // using namespace std; -- don't use 'using' in header files class Account { std::string name; double balance; public: Account(std::string n); Account(std::string n, double initial_balance); std::string get_name() const; double get_balance() const; void deposit(double amount); void widthdraw(double amount); }; 

And finally your Account.cc file should implement the class.

#include "Account.h" using namespace std; Account::Account(string n) : name(n), balance(0.0) {} Account::Account(string n, double initial_balance) : name(n), balance(initial_balance) {} string Account::get_name() const { return name; } double Account::get_balance() const { return balance; } void Account::deposit(double amount) { balance += amount; } void Account::widthdraw(double amount) { balance -= amount; // generously allow overdraft... } 

Hope that helps.

Roman

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.