Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;阶级与朋友_C++_Class_Friend - Fatal编程技术网

C++ C++;阶级与朋友

C++ C++;阶级与朋友,c++,class,friend,C++,Class,Friend,这是我的代码: #include <iostream> #include <string> using namespace std; class C { private: string str; friend void func(); }; void func() { str = "Lala"; cout << str << endl; } int main() { func();

这是我的代码:

#include <iostream>
#include <string>

using namespace std;

class C
{
private:
    string str;
    friend void func();
};


void func()
{
        str = "Lala";
        cout << str << endl;

}

int main()
{
    func();
}
#包括
#包括
使用名称空间std;
C类
{
私人:
字符串str;
friend void func();
};
void func()
{
str=“拉拉”;

cout它不起作用,因为
void func();
不是类的成员函数,它只是声明为它的朋友函数,这意味着它可以访问私有成员


您没有
类C
的实例,因此不可能引用任何有效的
str
变量。

下次,请同时引用您得到的错误。在这种情况下,将出现编译错误,说明func()中未定义符号“str”

如果要访问C类实例的成员str,则需要此类实例,如中所示:

void func(){
    C c;
    c.str = "Lala";
    cout << c.str << endl;

}
void func(){
C C;
c、 str=“拉拉”;

cout这不起作用,因为在func()中没有定义str。 你应该有一个C的实例

void func()
{
    C foo;
    foo.str = "Lala";
    cout << str << endl;
}
void func()
{
C foo;
foo.str=“拉拉”;
cout
func()
不是成员函数,并且它没有接收任何类型为
C
的参数,它应该在什么对象上操作

func
必须是
C
的成员函数(在这种情况下,您将通过
C
的实例调用它,并且
friend
不是必需的),或者是接收
C
类型参数的函数(或者创建本地
C
对象),甚至可以访问其私有字段。

void func(C*对象)
void func(C* object)
{
        object->str = "Lala";
        cout << object->str << endl;

}
{ 对象->str=“Lala”; 不能解决这个问题吗 让我们逐一查看您的代码:

#include <iostream>
#include <string>

using namespace std;
这里您定义了一个类C。您声明该类的对象将包含一个私有字符串(即只能由类成员和朋友访问),并且您将全局函数
void func()
声明为朋友,即允许它访问私有成员(在本例中为
str
)类
C
和类型为
C
的任何对象。请注意,除该权限外,
func
与类
C
没有任何关系

void func()
{
        str = "Lala";
        cout << str << endl;

}
好的,这里没有什么特别的,您只是在调用
func

如何修复它 现在,如何修复您的代码?有几种可能性:

供应对象 局部对象 由于
str
是类
C
对象的成员,因此您需要该类的对象。例如,您可以执行以下操作:

void func()
{
  C object;
  object.str = "Lala";
  std::cout << object.str << std::endl;
}
这将输出
Lala--Lele
,因为第一个对象的
str
成员具有值
“Lala”
,而第二个对象的
str
成员具有值
“Lele”

函数参数 另一个选项是将对象作为参数传递,例如

void func(C object)
{
  std::cout << object.str << " -- ";
  object.str = "Lele";
  std::cout << object.str << " -- ";
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
  std::cout << object.str << std::endl;
}
参数
C const&
表示“我想直接访问调用者给我的对象,但我保证不更改它。“直接访问”部分由
&
表示,“我保证不更改它”由
常量表示。编译器实际上会检查您是否遵守承诺,并且不会试图更改承诺(也就是说,如果在
func
中尝试执行
object.str=“Lele”
,编译器会抱怨(有很多方法可以告诉编译器闭嘴,但您不应该这样做;只要遵守承诺即可)。但是请注意,这同样仅适用于该特定对象;例如,以下代码完全可以:

void func(C const& object)
{
  C another_object;
  another_object.str = "Lele";
  std::cout << object.str << " -- " << another_object.str << std::endl;
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
}
这将打印
Lala--Lele--Lele

现在,您再次直接访问作为参数从
main
传递的对象,但这一次,您不能保证不更改它,而且确实更改了它。
main
的输出表明
main\u object
确实已更改

使变量成为静态成员 现在,您可能真的希望在
C
中只有一个
str
,而不是该类型的每个对象都有一个单独的
str
。如果您绝对肯定这是您想要的,那么您可以将
str
设置为类的静态成员:

class C
{
private:
  static std::string str; // note the keyword "static" here
  friend void func();
};

std::string C::str; // You have to have an extra definition for class static variables!
现在,您可以访问
str
,而无需使用
C
对象。但是请注意,您仍然需要告诉
func
内部的编译器您想要访问
C
内部的
str

void func()
{
  C::str = "Lala";
  std::cout << C::str << std::endl;
}

现在我们将得到输出
Lele--Lele
,因为只有一个
str
,它不依赖于对象(在本例中,语法
object1.str
在这方面是误导性的;实际上这里它意味着“为
object1
的类型定义的
str
,即
C

对象是什么?代码> FUNC应该如何操作?“很快它就不太好了?”自从我用C++编译以来,它已经太长了。因为OP是一个完整的初学者,我认为在这个答案中解释为什么你使用一个指针是很有用的。为什么不使用一个引用?OMG,为什么我被否决了?见我的评论。顺便说一句,不是我。
void func(C object)
{
  std::cout << object.str << " -- ";
  object.str = "Lele";
  std::cout << object.str << " -- ";
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
  std::cout << object.str << std::endl;
}
void func(C const& object)
{
  std::cout << object.str << std::endl;
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
}
void func(C const& object)
{
  C another_object;
  another_object.str = "Lele";
  std::cout << object.str << " -- " << another_object.str << std::endl;
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
}
void func(C& object)
{
  std::cout << object.str << " -- ";
  object.str = "Lele";
  std::cout << object.str << " -- ";
}

int main()
{
  C main_object;
  main_object.str = "Lala";
  func(main_object);
  std::cout << object.str << std::endl;
}
class C
{
private:
  static std::string str; // note the keyword "static" here
  friend void func();
};

std::string C::str; // You have to have an extra definition for class static variables!
void func()
{
  C::str = "Lala";
  std::cout << C::str << std::endl;
}
void func()
{
  C object1, object2;
  object1.str = "Lala";
  object2.str = "Lele";
  std::cout << object1.str << " -- " << object2.str << "\n";
}