我经常看到人们说"如果你需要朋友/内部然后你的设计是错误的",有人可以告诉我如何重新设计以下代码以消除ChessPiece.Location中的内部?
它目前被使用,因此向ChessBoard添加一个片段设置ChessPiece.Location属性以匹配,显然使它公开将比内部更糟糕并使其私有将阻止ChessBoard更新位置.感谢您的任何见解.
public struct Coord
{
public Coord(int x, int y) { this.X = x; this.Y = y; }
public int X { get; private set; }
public int Y { get; private set; }
}
public class ChessBoard
{
public ChessBoard() { /*[...]*/ }
public ChessPiece this[int x, int y]
{
get
{
// Return ChessPiece at this position (or null)
}
set
{
// Add ChessPiece at this position and set its Location property
}
}
public class …Run Code Online (Sandbox Code Playgroud) 我对之前的帖子有一些很好的见解,但是我不知道这些编译错误意味着我可以使用一些助手.模板,朋友和重载都是新的,所以三合一给我一些问题......
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Point<double>::Point<double>(double,double)" (??0?$Point@N@@QAE@NN@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Point<int>::Point<int>(int,int)" (??0?$Point@H@@QAE@HH@Z) referenced in function _main
1>C3_HW8.exe : fatal error LNK1120: 3 unresolved externals
Run Code Online (Sandbox Code Playgroud)
Point.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <class T>
class Point
{
public:
Point();
Point(T xCoordinate, T yCoordinate);
template <class G>
friend std::ostream &operator<<(std::ostream &out, const Point<G> &aPoint);
private:
T xCoordinate;
T yCoordinate;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Point.cpp
#include "Point.h"
template <class …Run Code Online (Sandbox Code Playgroud) 我正在尝试与一个类成为朋友,以便它能够到达它的私有构造函数.
在some_file.h中
class B;
namespace some_name {
class A {
public:
A() {}
private:
A (int x) {}
friend class ::B;
};
}
Run Code Online (Sandbox Code Playgroud)
在other_file.h中
#include "some_file"
namespace {
class B {
protected:
A* get_a(int x) { return new A(x); }
};
}
Run Code Online (Sandbox Code Playgroud)
编译此代码时,我得到 - 错误:'some_name :: A :: A(int)'是私有的.
我现在,这是私人的,这就是我成为朋友的原因B.我在这里做错了什么?你不能和你的构造者成为朋友吗?是否有命名空间问题?
谢谢
我有一个Facebook php sdk检索朋友列表问题.这是我的基本代码......
<?php
require_once 'fb-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
'cookie' => true,
));
$accessToken = $facebook->getAccessToken();
$session = $facebook->getSession();
$uid = $facebook->getUser();
//echo "https://graph.facebook.com/".$uid."/friends/?access_token=".$accessToken;
$frnd = $facebook ->api('/me/friends?access_token='.$accessToken);
echo $frnd["data"][0]["name"];
?>
Run Code Online (Sandbox Code Playgroud)
但它返回了一种特殊的输出.

Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
在C++中朋友函数/类的真正用途是什么?你能给出一个只有friend正确方法的例子吗?
谢谢
如果我在模板类声明中定义了友元模板函数,如下所示,它可以编译.
#include <iostream>
template <typename T>
class X {
public:
X(int i) : n(i) {}
private:
T n;
template <typename U>
friend void doStuff(const X<T>& x, const U& u)
{
std::cout << (x.n + u) << std::endl;
}
};
int main()
{
X<int> x(1);
doStuff(x, 3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我移出doStuff()类decalaration 的定义,就在声明类之后X<>,如下,它就无法编译.
template <typename U>
template <typename T>
void doStuff(const X<T>& x, const U& u)
{
std::cout << (x.n + u) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
以下代码也不是.
template <typename …Run Code Online (Sandbox Code Playgroud) 我有以下两个类:
struct Entity
{
unsigned id;
Entity(unsigned id);
Entity();
};
class EntityManager
{
public:
Entity create();
...
private:
Entity make_entity(unsigned index, unsigned generation);
};
Run Code Online (Sandbox Code Playgroud)
这个目前工作正常.问题是封装.我不想允许直接创建类Entity.
因此,我的目标是使构造者成为Entity私有的.然后,我可以(从我的理解)十个分量的功能在EntityManager通过使Entity一个friend的EntityManager.
因此,进行更改将是结果:
struct Entity
{
unsigned id;
private:
Entity(unsigned id);
Entity();
};
class EntityManager
{
friend struct Entity;
public:
Entity create();
private:
Entity make_entity(unsigned index, unsigned generation);
};
Run Code Online (Sandbox Code Playgroud)
这打破了代码.我得到的错误就是这个:
entity_manager.cpp: In member function ‘Entity EntityManager::make_entity(unsigned int, unsigned int)’:
entity_manager.cpp:12:1: error: ‘Entity::Entity(unsigned int)’ is …Run Code Online (Sandbox Code Playgroud) 因此,我获得了一个带有入门代码的作业来实现一个链表(我已经成功完成了未分类的双向链表),并且在给定头文件的入门代码中有一个朋友声明似乎有允许的目标我使用cout语句打印链表.这是头文件; 请注意,我在私人部分写了一切.
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include <ostream>
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
private:
struct node
{
char data;
node * next;
node * prev;
};
node * head, * tail;
};
#endif // _LINKED_LIST_
Run Code Online (Sandbox Code Playgroud)
在main,这也是初学者代码的一部分,老师写道cout << list;,这让我相信头文件中的友元声明的目标是允许列表轻松地打印到控制台.通常我不在乎,但如果我不注释掉cout << list;语句,那么链接器会为每个实例提供以下错误cout << list;
app.o: In function 'main':
[code directory]/app.cpp:[line the statement is on]: undefined reference to …Run Code Online (Sandbox Code Playgroud) 我试图在clang上编译以下代码,但它失败并出现以下错误:
error: no member named 'operator<' in the global namespace
Run Code Online (Sandbox Code Playgroud)
我尝试使用/ Za编译visual studio代码以切换到标准一致性,但它似乎仍然接受它.请赐教.
struct A
{
int m_test;
A(int test)
: m_test(test)
{
}
friend bool operator<(A left, A right);
};
int main()
{
typedef bool(*TCompare)(A,A);
TCompare compare = &::operator<;
compare(9,7);
}
Run Code Online (Sandbox Code Playgroud)
VC++输出:https://godbolt.org/g/LAz56n
Clang输出:https://godbolt.org/g/zC2InO
我试图以类的朋友的身份重载ostream运算符以构建电路的组件,但是它不断返回地址。
在“ Circuit_classes.h”文件的串联电路类中:
friend ostream& operator<< (ostream& os, series_circuit const& myCircuit);
Run Code Online (Sandbox Code Playgroud)
在文件“ Circuit_classes.cpp”中:
ostream& operator<<(ostream& os, series_circuit const& myCircuit){
os << "Output: " << myCircuit.frequency << endl;
return os;
}
Run Code Online (Sandbox Code Playgroud)
在frequency类头文件中定义为60的位置。
在我的主程序中,“ AC Circuits.cpp”
vector<shared_ptr<circuit>> circuit_vector;
circuit_vector.push_back(shared_ptr<circuit>(new series_circuit));
cout << circuit_vector[0] << endl;
Run Code Online (Sandbox Code Playgroud)
运行程序时在命令行中输出:
0325E180
Run Code Online (Sandbox Code Playgroud)