虽然您不想这样做,但如果您有一个名称空间COMPANY,并且该名称空间中有一个类SOMECLASS。为什么在.cpp文件中,您可以将函数定义为
COMPANY::SOMECLASS::someFunction()
{}
但在主要方面,我做错了:
int main() {
COMPANY::SOMECLASS::someFunction();
}
但你要声明命名空间并执行以下操作:
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:
#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
#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);
}
答案 0 :(得分:0)
using指令可以在C ++源文件中使用,而不是在头文件中使用,因此您可以对您提到的“.cpp”文件使用using指令。如果没有使用它,那么创建文件的人可能只是决定不这样做,或者为了避免名称冲突(这就是命名空间存在的全部原因)可能是必要的。永远不会放置using指令的地方是在头文件中,因为这样做完全破坏了首先使用命名空间的要点(不幸的是,如果你这样做,编译器就不会说任何东西,因为它在技术上是允许的做)。
P.S。:“using directive”我指的是一个在全局范围内并且公开命名空间内容的指令。如果从一些其他命名空间中引入符号(假设它是API的一部分),并且还防止基类中的函数被遮盖(即,如果您定义了Derived ::),则可以在头文件中使用using指令。 foo,你可能想要使用Base :: foo)。
答案 1 :(得分:0)
int main() {
COMPANY::SOMECLASS::someFunction();
}
当然是合法的,
using COMPANY::SOMECLASS;
int main() {
someFunction();
}
后者只是为您节省打字。但是,使用第一个的一个原因是避免多个名称空间之间的冲突。例如,如果有一天你决定这个代码需要使用一个类foo
的库来声明static someFunction()
:
using COMPANY::SOMECLASS;
using foo;
int main() {
someFunction(); // Uhh, which someFunction()?
}
请参阅C ++常见问题解答中的[27.5] Should I use using namespace std in my code?。
答案 2 :(得分:0)
namespace COMPANY {
class SOMECLASS {
static void someFunction();
// Here you can call it COMPANY::SOMECLASS::someFunction,
// SOMECLASS::someFunction, or simply someFunction.
// You may define it as someFunction.
};
// Here you can call it COMPANY::SOMECLASS::someFunction,
// or SOMECLASS::someFunction.
// You may define it as COMPANY::SOMECLASS::someFunction or
// SOMECLASS::someFunction.
}
// Here you must call it COMPANY::SOMECLASS::someFunction,
// You may define it as COMPANY::SOMECLASS::someFunction:
COMPANY::SOMECLASS::someFunction()
{}
// Or call it the same way:
int main() {
COMPANY::SOMECLASS::someFunction();
}
// If you import the namespace,
using namespace COMPANY;
// or just the class,
using COMPANY::SOMECLASS;
// now you may call it SOMECLASS::someFunction
int main() {
someFunction(); // ERROR - but you cannot call it simply someFunction
} // outside of the class it lives in.
答案 3 :(得分:0)
问题只在于你在main中的声明 - 你正在访问构造函数,而不是类型,你不能直接引用构造函数。
将main更改为:
int main()
{
JWong::SavingsAccount *saver1 = new JWong::SavingsAccount(2000.00);
}
......你应该好好去。