4
class Student{
public:
Student(int test)
:key(705)
{
    if(test == key)
    {cout << "A student is being verified with a correct key: "<< test << endl;
    allow=1;
    }
    else
    {
        cout << "Wrong key" ;
    }
}

friend void printResult();


private:
const int key;
int allow;


};

void printResult()
{
 if(allow==1)
 {
   cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
  }
}

int main()
{
int testkey;
cout << "Enter key for Bob: ";
cin >> testkey;

Student bob(testkey);

printResult();

}

The function printResult can't seem to access the variable allow, which is private, from the Student class. Did I prototype the printResult in the wrong place or was the syntax wrong? AFAIK, we can prototype friends anywhere in the class.

4

5 回答 5

5

那是因为allow属于该类的一个实例,但没有引用任何实例。

您可能应该创建printResult类的成员函数,而不是使其成为外部函数使函数引用Student实例,以便您可以通过where is a parameter of type访问allow成员。instance.allowinstanceconst Student&

于 2013-06-18T15:09:15.090 回答
5

printResult不是成员函数,所以你需要给它一个Student实例来操作。例如

void printResult(const Student& s)
{
 if(s.allow==1)
 {
   cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
  }
}

然后

Student student(1);
printResult(student);
于 2013-06-18T15:09:20.240 回答
2
class Student{    
private:
const int key;
int allow;    
public:
Student(int test)
:key(705)
{
    if(test == key)
    {cout << "A student is being verified with a correct key: "<< test << endl;
    int allow=1;
    }
    else
    {
        cout << "Wrong key" ;
    }
}    
friend void printResult(); 
void printResult() //<---  printResult() is a member function, so keep it inside the class
{
 if(allow==1)
 {
   cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
  }
}
}; 

int main()
{
int testkey;
cout << "Enter key for Bob: ";
cin >> testkey;    
Student bob(testkey);   
bob.printResult(); // <--- you need to specify the owner of printResult()   
}
于 2013-06-18T15:15:08.037 回答
2

这是一些有效的代码:

#include <iostream>
class Student
{
public:
Student(int test) : key(705)
{
    if(test == key)
    {
        std::cout << "A student is being verified with a correct key: "<< test <<    std::endl;
        allow=1;
    }
    else
    {
        std::cout << "Wrong key" ;
    }
}

friend void printResult(Student* student);

private:
    const int key;
    int allow;
};

void printResult(Student* student)
{
    if(student->allow==1)
    {
        std::cout<< " Maths: 75 \n Science:80 \n English: 75" << std::endl;
    }
}

int main(int argc, char* argv[])
{
    int testkey;
    std::cout << "Enter key for Bob: ";
    std::cin >> testkey;

    Student bob(testkey);

    printResult(&bob);
}

我对其进行了修改以将打印功能保留在全局空间中(仅基于您想要的外观)。它需要一个 Student* 参数,因为它被声明为朋友,所以它会看到“允许”变量。然而,这不是您想要滥用的 C++ 的一部分。小心你如何使用朋友。在更大的代码库中像这样使用它是危险的。全局功能通常不是要走的路。将 print 函数作为学生类中的公共成员函数可能是一种更好的做事方式。其他答案提供了显示该实现的代码。我决定向您展示此实现,因为它似乎更接近您在问题中寻找的内容。

我还确保在提及 cout 和 endl 时使用 'std::'。这消除了对“使用命名空间标准;”的需要 在顶部。这只是在更复杂的项目中的良好编程实践。

于 2013-06-18T15:29:52.607 回答
1

类友元函数被允许访问类实例的成员。您应该将实例传递给函数,例如:

void printResult(Student s)
{
 if(s.allow==1)
 {
   cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
  }
}
于 2013-06-18T15:10:46.260 回答