使用声明在main(C ++) [英] using declarations in main (C++)

查看:186
本文介绍了使用声明在main(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然你不想这样做,如果你有一个命名空间COMPANY和一个类在该命名空间SOMECLASS。为什么在.cpp文件中,您可以将函数定义为

Although you wouldn't want to do this, if you have a namespace COMPANY, and a class in that namespace SOMECLASS. Why is it that in the .cpp file, you might define the functions as

COMPANY::SOMECLASS::someFunction()
{}

但是在main中,我遇到了错误:

But in main, I get errors for doing:

int main() {
  COMPANY::SOMECLASS::someFunction();
}

,而是声明命名空间并执行类似的操作:

but instead you declare the namespace and do something like:

using COMPANY::SOMECLASS;

int main() {
  someFunction();
}

我的编译错误是:

1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2065: 'saver1' : undeclared identifier
1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2277: 'JWong::SavingsAccount::{ctor}' : cannot take address of this member function
1>c:\documents and settings\wongj\desktop\main.cpp(14) : error C2061: syntax error : identifier '{ctor}'

SavingsAccount.cpp:

SavingsAccount.cpp:

#include "SavingsAccount.h"

// initialize static data member
double JWong::SavingsAccount::annualInterestRate = 0;

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

// added these functions to make program cleaner
double JWong::SavingsAccount::getMonthlyInterest()
{
    return monthlyInterest;
}

void JWong::SavingsAccount::setMonthlyInterest(double monthlyInterest)
{
    this->monthlyInterest = monthlyInterest;
}

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

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

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

SavingsAccount.h

SavingsAccount.h

#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();
        double getMonthlyInterest();
        void setMonthlyInterest(double monthlyInterest);

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

        // static members
        static double annualInterestRate; 
        double monthlyInterest;
    };
}



#endif

main.cpp:

#include <iostream>
#include <iomanip>

#include "SavingsAccount.h"
using std::cout;
using std::setprecision;
using std::fixed;
//using JWong::SavingsAccount;

int main()
{
    JWong::SavingsAccount::SavingsAccount *saver1 = new JWong::SavingsAccount::SavingsAccount(2000.00);
}


推荐答案

将主要更改为:

int main()
{
    JWong::SavingsAccount *saver1 = new JWong::SavingsAccount(2000.00);
}

...你应该很好。

这篇关于使用声明在main(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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