#include<iostream> #include<string> using namespace std; class Sales_item;//前置申明,让Test识别Sales_item class Test { public: void GetName(Sales_item &item); std::string TestName; }; class Sales_item { friend void Test::GetName(Sales_item &item); //freind class Test;的话Test任何方法都能访问 public: Sales_item(const std::string &book) :bookName(book){} std::string GetBookName() const { return bookName; } private: std::string bookName; }; void Test::GetName(Sales_item &item)//必须放友元申明之后 { cout<<item.bookName<<endl;; } int main() { Sales_item item1="bookName1"; Test t; t.GetName(item1); }