c++ - 你如何在 C++ 中创建类的实例

标签 c++

我是 C++ 初学者。我有一个类,当我尝试编译它时说它缺少“main”。

我需要做什么来创建此类的实例并访问它的方法?

#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);

};

编辑:

我把主要方法放在哪里?

我试过将它放在不同的文件中,如下所示

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

但这给出了一个错误,指出目录中没有帐户。我猜这是因为它没有编译

编辑:

两个文件在同一个目录下

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);

};

最佳答案

所有 C++ 程序都需要所谓的入口点main()函数始终是标准 C++ 程序的入口点。您需要提供一个 main() , 函数否则链接器会报错。你可以写一个main()以两种方式之一运行:

int main()
{
    return 0;
}

或者,如果您需要命令行参数:

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

请注意 void main() 在 C++ 中无效。另请注意 return声明对于 main() 不是绝对必要的功能,但为了保持一致性,无论如何您都应该明确地编写一个。

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;

要回答有关您最近编辑的问题:

#include <Account>

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

main()的形式是正确的,但这不是您提供类成员定义的方式。构造函数需要在主函数之外。

#include <Account>

// Outside of main()
Account::Account(string n) : name(n), balance(0)
{
} 

int main()
{ 
     return 0 ; 
} 

创建 Account 的实例,您声明一个变量并传递所有必需的构造函数参数,如下所示:

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;
}

此外,检查 class Account 的确切文件路径是。如果Account类位于名为 Account.h 的文件中并且与包含 main() 的文件位于同一文件夹中功能,那么你需要使用#include "Account.h"而不是 #include <Account>在那个文件中。例如:

#include "Account.h" // Note .h and quotes

Account::Account(string n) : name(n), balance(0)
{
} 

int main()
{
    // ...
    return 0;
}

这实际上是 C++ 编程语言的一个相当基本的方面。你肯定有一本涵盖这个的书吗?事实上,main()函数和 #include语句通常是您在使用 C++ 编程时学习的第一件事。我强烈建议你拿起a good C++ book通读并做练习。

关于c++ - 你如何在 C++ 中创建类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7920531/

相关文章:

c++ - Char 数组正确输出但实际上未存储正确的值

c++ - 如何将 OpenGL 中的事物缩放到实际大小?

c++ - 在 Qt 中显示控制台

c++ - 当用户输入错误的数据类型时从单独的文件循环调用函数

c++ - Winsock 发送调用很慢

c++ - 未定义对我的函数的引用

c++ - 如何在不刷新缓冲区的情况下打印换行符?

c++ - 不可重复的二维 vector 问题

带有 Boost 库的 C++。读书专栏。 Excel/CSV文件

c++ - 主栈空间和任务栈空间