未定义的参考

发布于 2024-12-16 19:28:36 字数 1756 浏览 2 评论 0原文

我的程序很简单。一类银行账户。每个帐户都有余额和欠款。每个帐户的利率相同。我编译时遇到错误。怎么了?提前致谢。

  2 #include <iostream>
  3 #include <string>
  4 using namespace std;
  5 class bank
  6 {
  7 private:
  8         double balance;
  9         string owner;
 10         static double InterestRate;
 11 public:
 12         static void AccountInfo(const bank& ac)
 13         {
 14                 cout << "name: " << ac.owner << endl << "balance: " << ac.balance;
 15         }
 16         static void SetAccount(bank& ac)
 17         {
 18                 cout << "enter a name: " << flush;
 19                 cin >> ac.owner;
 20                 cout << "enter a balance: " << flush;
 21                 cin >> ac.balance;
 22         }
 23         static void SetRate(const double& n)
 24         {
 25                 InterestRate = n;
 26         }
 27         static void Balance(bank& ac)
 28         {
 29                 ac.balance += ac.balance * InterestRate;
 30         }
 31 };
 32 int main ()
 33 {
 34         bank NewAccount;
 35         bank::SetAccount(NewAccount);
 36         bank::SetRate(0.15);
 37         bank::Balance(NewAccount);
 38         bank::AccountInfo(NewAccount);
 39 return 0;
 40 }

输出是:

  /tmp/ccUh8Sd9.o: In function `bank::SetRate(double const&)':
    e1237.cpp:(.text._ZN4bank7SetRateERKd[bank::SetRate(double const&)]+0x12): undefined reference to `bank::InterestRate'
    /tmp/ccUh8Sd9.o: In function `bank::Balance(bank&)':
    e1237.cpp:(.text._ZN4bank7BalanceERS_[bank::Balance(bank&)]+0x1c): undefined reference to `bank::InterestRate'
    collect2: ld returned 1 exit status

my program is simple. a class of a bank account. Each account has a balance and an ower. each account has the same interest rate. I got the error when compile it. What's wrong? Thanks in advance.

  2 #include <iostream>
  3 #include <string>
  4 using namespace std;
  5 class bank
  6 {
  7 private:
  8         double balance;
  9         string owner;
 10         static double InterestRate;
 11 public:
 12         static void AccountInfo(const bank& ac)
 13         {
 14                 cout << "name: " << ac.owner << endl << "balance: " << ac.balance;
 15         }
 16         static void SetAccount(bank& ac)
 17         {
 18                 cout << "enter a name: " << flush;
 19                 cin >> ac.owner;
 20                 cout << "enter a balance: " << flush;
 21                 cin >> ac.balance;
 22         }
 23         static void SetRate(const double& n)
 24         {
 25                 InterestRate = n;
 26         }
 27         static void Balance(bank& ac)
 28         {
 29                 ac.balance += ac.balance * InterestRate;
 30         }
 31 };
 32 int main ()
 33 {
 34         bank NewAccount;
 35         bank::SetAccount(NewAccount);
 36         bank::SetRate(0.15);
 37         bank::Balance(NewAccount);
 38         bank::AccountInfo(NewAccount);
 39 return 0;
 40 }

and the output is:

  /tmp/ccUh8Sd9.o: In function `bank::SetRate(double const&)':
    e1237.cpp:(.text._ZN4bank7SetRateERKd[bank::SetRate(double const&)]+0x12): undefined reference to `bank::InterestRate'
    /tmp/ccUh8Sd9.o: In function `bank::Balance(bank&)':
    e1237.cpp:(.text._ZN4bank7BalanceERS_[bank::Balance(bank&)]+0x1c): undefined reference to `bank::InterestRate'
    collect2: ld returned 1 exit status

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山有枢 2024-12-23 19:28:36

您刚刚在类定义中声明了静态成员 InterestRate,但是,
您还需要在您的 cpp 文件之一中定义静态成员:

double bank::InterestRate = 0.0;

如果没有这个,链接器将无法找到其定义并给出链接错误。

You just declared the static member InterestRate in your class definition but,
You also need to define your static member in one of your cpp file:

double bank::InterestRate = 0.0;

Without this the linker cannot find its definition and gives the linking error.

格子衫的從容 2024-12-23 19:28:36

发生这种情况是因为行:

static double InterestRate;

是声明而不是定义 - 即它不创建变量,而只是指示这样的变量存在。这在 C++ 中非常违反直觉,但您可以处理它:-)

在任何 C++ 文件中添加:

double bank::InterestRate;

来定义此变量。

理想情况下,如果将项目拆分为 .cpp 和 .h 文件,则应将其放在相应的 .cpp 文件中,而类声明位于 .h 中。

this happens because the line:

static double InterestRate;

is a declaration not a definition - i.e. it doesn't create a variable, but merely indicates that such a variable exists. This is very counter-intuitive in C++, but you can deal with it :-)

Add in any C++ file:

double bank::InterestRate;

to define this variable.

Ideally, if you split your project into .cpp anad .h files, you should have it in the corresponding .cpp file, while the class declaration is in .h.

猫烠⑼条掵仅有一顆心 2024-12-23 19:28:36

我认为你想做的是这样的:

#include <iostream>
#include <string>

using namespace std;

class bank
{
    private:
            static double InterestRate;

            double balance;
            string owner;

    public:
            void AccountInfo()
            {
                    cout << "name: " << owner << endl;
                    cout << "balance: " << balance << endl;
            }
            void SetAccount()
            {
                    cout << "enter a name: " << flush;
                    cin >> owner;
                    cout << "enter a balance: " << flush;
                    cin >> balance;
            }
            static void SetRate(const double& n)
            {
                    InterestRate = n;
            }
            void Balance()
            {
                    balance += balance * InterestRate;
            }
};

double bank::InterestRate=0;

int main ()
{
    bank NewAccount;

    bank::SetRate(0.15);
    NewAccount.SetAccount();
    NewAccount.Balance();
    NewAccount.AccountInfo();
    return 0;
}

我仍然不确定为什么你首先使用静态,但静态成员用于与类本身相关的内容,而不是与实例相关的内容。您正在创建一个帐户类(提示:最好将该类称为帐户),现在您正在创建该类的一个实例,该实例很可能不会与其他帐户共享其数据!

顺便说一句,银行账户通常并不都具有相同的利率,因此即使财产不应该是静态的。我建议在类中添加一个static double defaultInterest成员,并为每个帐户添加一个doubleinterest实例变量,默认情况下为该变量分配默认利率,但可以进行调整为 VIP 客户提供不同的利率;-)

I think what you want to do is this:

#include <iostream>
#include <string>

using namespace std;

class bank
{
    private:
            static double InterestRate;

            double balance;
            string owner;

    public:
            void AccountInfo()
            {
                    cout << "name: " << owner << endl;
                    cout << "balance: " << balance << endl;
            }
            void SetAccount()
            {
                    cout << "enter a name: " << flush;
                    cin >> owner;
                    cout << "enter a balance: " << flush;
                    cin >> balance;
            }
            static void SetRate(const double& n)
            {
                    InterestRate = n;
            }
            void Balance()
            {
                    balance += balance * InterestRate;
            }
};

double bank::InterestRate=0;

int main ()
{
    bank NewAccount;

    bank::SetRate(0.15);
    NewAccount.SetAccount();
    NewAccount.Balance();
    NewAccount.AccountInfo();
    return 0;
}

I'm still not sure why you used static in the first place, but static members are used for stuff that relates to the class as such, and not to an instance. You're creating an account class (hint: better call this class Account), and now you're creating an instance of this class that most probably won't share its data with other accounts!

As a sidenote, bank accounts usually don't all have the same interest rate, so event that property should not be static. I suggest adding a static double defaultInterest member to the class, and a double interest instance variable to each account, which is assigned the default interest rate by default, but can be tweaked to contain a different interest rate for VIP customers ;-)

一腔孤↑勇 2024-12-23 19:28:36

我猜你一定有

双bank::InterestRate = 0。;

在类声明之外的 C 文件中

Guess you must have

double bank::InterestRate = 0.;

in your C file outside of the class declaration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文