c ++中未解析的外部符号错误 [英] Unresolved external symbol error in c++

查看:134
本文介绍了c ++中未解析的外部符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的hw问题涉及命名空间,静态数据成员和函数。我遇到一个未解决的外部符号错误

 错误1错误LNK2001:未解析的外部符号private:static double JWong :: SavingsAccount: :annualInterestRate(?annualInterestRate @ SavingsAccount @ JWong @@ 0NA)SavingsAccount.obj SavingsAccount 

不知道为什么我得到这个错误。也许我不知道一些静态变量相比,导致此错误的常规数据成员。这是我的代码:



SavingsAccount.h文件

  #ifndef JWONG_SAVINGSACCOUNT_H 
#define JWONG_SAVINGSACCOUNT_H

命名空间JWong
{
class SavingsAccount
{
public:
//默认构造函数
SavingsAccount();
// constructor
SavingsAccount(double savingsBalance);

double getSavingsBalance();
void setSavingsBalance(double savingsBalance);
double calculateMonthlyInterest();

//静态函数
static void modifyInterestRate(double newInterestRate);
static double getAnnualInterestRest();
private:
double savingBalance;

// static members
static double annualInterestRate;
};
}

#endif

SavingsAccount.cpp文件



  #include< iostream> 
#includeSavingsAccount.h

//默认构造函数,将savingsBalance设置为0
JWong :: SavingsAccount :: SavingsAccount():savingsBalance(0)
{}

//构造函数
JWong :: SavingsAccount :: SavingsAccount(double savingsBalance):savingsBalance(savingsBalance)
{}

double JWong: :SavingsAccount :: getSavingsBalance()
{
return savingsBalance;
}

void JWong :: SavingsAccount :: setSavingsBalance(double savingsBalance)
{
this-> savingsBalance = savingsBalance;
}

//返回每月利息并将savingsBalance设置为新的金额
double JWong :: SavingsAccount :: calculateMonthlyInterest()
{
double monthlyInterest = savingsBalance * SavingsAccount :: annualInterestRate / 12;
setSavingsBalance(savingsBalance + monthlyInterest);
return monthlyInterest;
}

void JWong :: SavingsAccount :: modifyInterestRate(double newInterestRate)
{
SavingsAccount :: annualInterestRate = newInterestRate;
}

double JWong :: SavingsAccount :: getAnnualInterestRest()
{
return SavingsAccount :: annualInterestRate;
}


解决方案

实际上编译了.cpp文件(因为其他函数链接)。



错误很可能是由于未定义 annualInterestRate static variable。



你已经声明了它(在类头中),但是没有定义。在你的cpp文件中添加:

  //静态成员定义
double JWang :: SavingsAccount :: annualInterestRate = 。

请参阅文章,突出介绍声明和定义静态成员。 / p>

C ++标准的第9.4.2节说静态数据成员的定义应出现在包含成员类定义的命名空间范围内。


I am trying to do a simple hw problem involving namespace, static data members and functions. I am getting an unresolved external symbol error

Error   1   error LNK2001: unresolved external symbol "private: static double JWong::SavingsAccount::annualInterestRate" (?annualInterestRate@SavingsAccount@JWong@@0NA)    SavingsAccount.obj  SavingsAccount

And I don't see why I am getting this error. Maybe I don't know something about static variables compared to regular data members that is causing this error. Here is my code:

SavingsAccount.h file

#ifndef JWONG_SAVINGSACCOUNT_H
#define JWONG_SAVINGSACCOUNT_H

namespace JWong
{
    class SavingsAccount
    {
    public: 
        // default constructor
        SavingsAccount();
        // constructor
        SavingsAccount(double savingsBalance);

        double getSavingsBalance();
        void setSavingsBalance(double savingsBalance);
        double calculateMonthlyInterest();

        // static functions
        static void modifyInterestRate(double newInterestRate);
        static double getAnnualInterestRest();
    private:
        double savingsBalance;

        // static members
        static double annualInterestRate; 
    };
}

#endif

SavingsAccount.cpp file

#include <iostream>
#include "SavingsAccount.h"

// default constructor, set savingsBalance to 0
JWong::SavingsAccount::SavingsAccount() : savingsBalance(0)
{}

// constructor
JWong::SavingsAccount::SavingsAccount(double savingsBalance) : savingsBalance(savingsBalance)
{}

double JWong::SavingsAccount::getSavingsBalance()
{
    return savingsBalance;
}

void JWong::SavingsAccount::setSavingsBalance(double savingsBalance)
{
    this->savingsBalance = savingsBalance;
}

// returns monthly interest and sets savingsBalance to new amount
double JWong::SavingsAccount::calculateMonthlyInterest()
{
    double monthlyInterest = savingsBalance * SavingsAccount::annualInterestRate / 12; 
    setSavingsBalance(savingsBalance + monthlyInterest);
    return monthlyInterest; 
}

void JWong::SavingsAccount::modifyInterestRate(double newInterestRate)
{
    SavingsAccount::annualInterestRate = newInterestRate;
}

double JWong::SavingsAccount::getAnnualInterestRest()
{
    return SavingsAccount::annualInterestRate;
}

解决方案

I'm presuming you are actually compiling the .cpp file (because the other functions link).

The error is likely due to not defining the annualInterestRate static variable.

You have declared it (in the class header), but it's not defined. In your cpp file add:

// static member definition
double JWang::SavingsAccount::annualInterestRate = ...;

See an article highlighting the difference between declaration and definition of static members.

Section 9.4.2 of the C++ Standard says "The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition."

这篇关于c ++中未解析的外部符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆