【问题标题】:Content error fundamental about friend declaration in c++关于 C++ 中友元声明的内容错误基础
【发布时间】:2013-01-13 02:31:47
【问题描述】:

我只是不明白,这个朋友声明有什么问题,第一个错误消息:

test.cpp: In function 'int main()':
test.cpp:34:7: error: 'void steuerung::exe_testa(testa)' is private
test.cpp:48:15: error: within this context

当 testa 的类声明中的构造函数被删除时,问题正在解决。但我需要一个构造函数。谁能帮帮我吗?真的很感谢你。

#include <iostream>

class steuerung;
class testb;
class testa;

class testa
{
friend class steuerung;
public:

private:
double a_;
double b_;
};

class testb
{
friend class steuerung; 
public:
testb(double a, double b) : a_(a), b_(b) {}

private:
double a_;
double b_;
};

class steuerung
{
friend class testa; 
friend class testb; 
void exe_testa(testa t_) { t_.b_++; }
//template<class t> void exe(t *t_) { std::cout << t_->a_ << std::endl; }
};




int main ()
{
std::cout << "Laeuft zumindest an.." << std::endl;

testa a;

steuerung s;
s.exe_testa(a);

return 0;
}

【问题讨论】:

    标签: c++ class constructor private friend


    【解决方案1】:

    exe_testa 是私有的,所以只能从steuerung 的成员函数、steuerung 的朋友类的成员函数和steuerung 的朋友函数调用。 main 不是这些,所以调用是不合法的。

    【讨论】:

    • 该死的,没想到这个。我假设班级成员默认是公开的。谢谢!
    • @user1456766 - 对。一个类的默认访问是私有的;对于一个结构,它是公开的。
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多