【问题标题】:an error within context上下文中的错误
【发布时间】:2010-06-17 14:35:33
【问题描述】:

谁能解释我的错误,我有这个课:

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

但是当我调用它时:

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

我收到错误within context,有人可以解释原因吗?

【问题讨论】:

  • 错误信息只是“在上下文中?”这是运行时错误还是编译时错误?没有其他消息?
  • 请注意我给你的建议last time 并查看错误消息的其余部分。 “在上下文中”只是出现在编译器输出中的一行,用于 join 错误的其他两个部分。上面是实际错误,下面是编译器当时试图编译的函数的名称。摘下眼罩,放眼大局。
  • -1 表示未发布整个错误。
  • 除了建议您在将来提供完整的错误消息之外,我还建议您对您喜欢的答案进行投票。在我写这篇文章时,接受的答案没有赞成票。投票需要 15 次代表,当我写这篇文章时,你显示的是 30:点击至少你接受的答案的向上箭头。

标签: c++ class oop


【解决方案1】:

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

问题在于,从 reportAccounts() 函数的角度来看,i-&gt;strFirstName 是私有的。更好的解决方案可能是:

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

然后

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

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

然而,函数被称为 print Account 的事实表明它作为一个公共类函数可能会更好。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 事实上,printAccount 没有理由像你写的那样成为朋友。如果您只是将 printAccount 传递给 const Account& 并让它提取所需的值,那就更好了。或者只是让它成为一个不带参数的成员函数,字符串 print() const。
    【解决方案3】:

    您在类定义中缺少分号。

    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?
    

    【讨论】:

      【解决方案4】:

      这不应该是全部错误..应该还有更多..

      顺便说一句,您的朋友语法似乎是正确的,但在 reportAccounts 中,您似乎使用了 Account 类的所有私有字段,例如 strFirstName 并且函数的名称是 reportAccounts 而不是 @987654325 @ 所以可能你刚刚交了一个方法朋友,但你正试图与另一个方法访问私有字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 2011-03-04
        • 2020-09-17
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多