C++ 上下文中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3062616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:56:35  来源:igfitidea点击:

an error within context

c++classoop

提问by helloWorld

can somebody please explain my mistake, I have this class:

有人可以解释我的错误吗,我有这门课:

class Account
{
private:
    string strLastName;     
    string strFirstName;    
    int nID;              
    int nLines;             
    double lastBill;
public:
    Account(string firstName, string lastName, int id);
    friend string printAccount(string firstName, string lastName, int id, int lines, double lastBill);
}

but when I call it:

但是当我调用它时:

string reportAccounts() const
{
    string report(printAccountsHeader());
    for(list<Account>::const_iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i)
    {
        report += printAccount(i->strFirstName, i->strLastName, i->nID, i->nLines, i->lastBill);;
    }
        return report;
}

I receive error within context, can somebody explain why?

我收到错误within context,有人可以解释为什么吗?

回答by jwhitlock

I imagine the full error has something to do with "These members are private within context" and some line numbers.

我想完整的错误与“这些成员是私有的within context”和一些行号有关。

The issue is that i->strFirstNameis private from the perspective of the reportAccounts()function. A better solution may be:

问题是i->strFirstNamereportAccounts()函数的角度来看是私有的。更好的解决方案可能是:

class Account{
    private:
        string strLastName;     
        string strFirstName;    
        int nID;              
        int nLines;             
        double lastBill;
    public:
        Account(string firstName, string lastName, int id);
        string print() const
        {
           return printAccount(this->strLastName, this->strFirstName, this->nID,
              this->nLines, this->lastBill);
        }
};

And then

进而

string reportAccounts() const {
    string report(printAccountsHeader());
    for(list<Account>::const_iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
        report += i->print();
    }
    return report;
}

Another option is to make printAccounttake a reference to an Account (friend printAccount(const Account& account)), and it can then access the private variables through the reference.

另一种选择是printAccount引用 Account ( friend printAccount(const Account& account)),然后它可以通过引用访问私有变量。

However, the fact that the function is called print Accountsuggests that it might be better as a public class function.

但是,该函数被称为打印帐户这一事实表明它可能更适合作为公共类函数。

回答by Didier Trosset

You're declaring that function printAccountis friend of class Account. But in the example, you're accessing the members of the class (i->strFirstName...) in the function reportAccounts. This latter is not declared as friend.

您声明该函数printAccountclass Account. 但在示例中,您正在访问i->strFirstName函数中的类 ( ...)的成员reportAccounts。后者不被声明为朋友。

回答by PeterK

You are missing a semicolon in the class definition.

您在类定义中缺少分号。

class Account{
    private:
        string strLastName;     
        string strFirstName;    
        int nID;              
        int nLines;             
        double lastBill;
    public:
        Account(string firstName, string lastName, int id);
    friend string printAccount(string firstName, string lastName, int id, int lines, double lastBill);
};
 ^--- see the semicolon here?

回答by Hyman

That shouldn't be the whole error.. there should be something more..

这不应该是整个错误..应该还有更多..

by the way your friend syntax seems correct, but in reportAccountsyou seem to use all the fields of Accountclass that are private, like strFirstNameand the name of the function is reportAccountsnot printAccountsso probably you just made a method friend but you are trying to access private fields with another one.

顺便说一下,您的朋友语法似乎是正确的,但是在reportAccounts您看来,您似乎使用了Account私有类的所有字段,例如strFirstName,函数的名称reportAccounts并非printAccounts如此,您可能只是将方法设为朋友,但您正尝试使用以下方法访问私有字段另一个。