C++ 如何为类声明友元函数?
离题:我想问,这是问这类问题的合适地方吗? 什么地方更适合问这种“天真”的问题?我想要个网站什么的。非常感谢。现在我的问题是: 我的任务是编写检查\u name函数(问题) 我发现错误:“first_name”未在此范围内声明已解决 编辑:我刚刚意识到这还不够,我必须在字符串中找到一个字符后删除每个字符 这是代码。多谢各位C++ 如何为类声明友元函数?,c++,function,class,friend,C++,Function,Class,Friend,离题:我想问,这是问这类问题的合适地方吗? 什么地方更适合问这种“天真”的问题?我想要个网站什么的。非常感谢。现在我的问题是: 我的任务是编写检查\u name函数(问题) 我发现错误:“first_name”未在此范围内声明已解决 编辑:我刚刚意识到这还不够,我必须在字符串中找到一个字符后删除每个字符 这是代码。多谢各位 #include <iostream> #include <string> using namespace std; class student
#include <iostream>
#include <string>
using namespace std;
class student
{
private:
string first_name;
string last_name;
public:
void set_name(string f, string l)
{
first_name = f;
last_name = l;
}
friend void check_name(student k);
};
bool isInside(const string &str, char c)
{
return str.find(c) != string::npos;
}
void check_name(student k)
{
bool ok = true;
for(int i = 0; i < first_name.size(); i++)
{
if(!isInside(last_name, first_name[i]))
{
ok = false;
break;
}
}
if (ok) cout << "ANAGRAM" << endl;
else cout << "NOT ANAGRAM" << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
string f, l;
cin >> f >> l;
student s;
s.set_name(f, l);
check_name(s);
}
}
#包括
#包括
使用名称空间std;
班级学生
{
私人:
字符串名;
字符串last_name;
公众:
无效集合名称(字符串f、字符串l)
{
first_name=f;
姓氏=l;
}
朋友姓名(学生k);
};
布尔isInside(常量字符串和字符串,字符c)
{
return str.find(c)!=string::npos;
}
无效检查姓名(学生k)
{
bool ok=true;
对于(int i=0;i>l;
学生证;
s、 集合名称(f,l);
检查您的姓名;
}
}
您想使用
void check_name(student k)
{
bool ok = true;
for (int i = 0; i < k.first_name.size(); i++)
// ^^
{
if (!isInside(k.last_name, k.first_name[i]))
// ^^ ^^
{
ok = false;
break;
}
}
if (ok) cout << "ANAGRAM" << endl;
else cout << "NOT ANAGRAM" << endl;
}
void check\u姓名(学生k)
{
bool ok=true;
对于(int i=0;i
void check_name(student k)
{
bool ok = true;
for (int i = 0; i < k.first_name.size(); i++)
// ^^
{
if (!isInside(k.last_name, k.first_name[i]))
// ^^ ^^
{
ok = false;
break;
}
}
if (ok) cout << "ANAGRAM" << endl;
else cout << "NOT ANAGRAM" << endl;
}
void check\u姓名(学生k)
{
bool ok=true;
对于(int i=0;i 如果(确定)CUT如何访问来自非朋友函数的学生变量?<代码>第二名No./COD>不在任何地方声明,并且您可能想使用<代码> K.FiSTyNeX。也考虑使用引用。Oh,是的,这是错误的第二个名字,而不是LASTYNAMED。是的,我正在测量K。谢谢。JhonWillson也请感觉到。可以在代码中自由使用空格。离题:我想问,这是问这类问题的合适地方吗?
是的,也可以,只要你表明你做了研究,尽了最大努力,并在适当的地方附加。你如何从非好友函数访问学生变量?第二个名字
不在任何地方声明,您可能想使用<代码> k.FirStIdEng/<代码>。也可以考虑使用引用。Oh,是的,这是错误的第二个名字,而不是LASTYNEND。是的,我正在测量K。谢谢。JhonWillson也请在代码中使用空白。<代码>主题:我想问一下,这是问的正确位置吗?这是一个问题吗?
是的,只要你证明你做了研究,尽了最大努力,并在适当的地方附上,就可以了。