我有以下课程
#ifndef Container_H
#define Container_H
#include <iostream>
using namespace std;
class Container{
friend bool operator==(const Container &rhs,const Container &lhs);
public:
void display(ostream & out) const;
private:
int sizeC; // size of Container
int capacityC; // capacity of dynamic array
int * elements; // pntr to dynamic array
};
ostream & operator<< (ostream & out, const Container & aCont);
#endif
Run Code Online (Sandbox Code Playgroud)
和这个源文件
#include "container.h"
/*----------------------------*********************************************
note: to test whether capacityC and sizeC are equal, must i add 1 to sizeC?
seeing as …Run Code Online (Sandbox Code Playgroud) 我正在尝试使MainScheduler的方法addJob成为Job类的友方函数,如下所示:
#include "MainScheduler.h"
//forward declaration
class MainScheduler;
class Job:
{
friend void MainScheduler::addJob( Job* const job );
...
}
Run Code Online (Sandbox Code Playgroud)
但我一直在收到错误
错误C2027:使用未定义类型'MainScheduler'
你知道为什么我收到这条消息,我该如何解决?
我有两个类,一个和两个,每个都有一个朋友成员函数,指向另一个.第一个头文件如下:
#ifndef FIRSTCLASS_H
#define FIRSTCLASS_H
class two;
class one {
private:
int one_data;
public:
friend void second_data (two *);
};
#endif
Run Code Online (Sandbox Code Playgroud)
第二个头文件如下所示:
#ifndef SECONDCLASS_H
#define SECONDCLASS_H
class one;
class two {
private:
int two_data;
public:
friend void first_data (one *);
};
#endif
Run Code Online (Sandbox Code Playgroud)
实际函数位于第三个.cpp文件中.我用适当的类限定符编写了函数,它给出了编译错误.我正在使用g ++.功能如下:
#include "firstclass.h"
#include "secondclass.h"
void two::first_data (one * class1) {
}
void one::second_data (two * class2) {
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
error:no ‘void two::first_data (one*)’ member function declared in class ‘two’
error: …Run Code Online (Sandbox Code Playgroud) 我正在研究一个类似于工资单的项目.部分提示说
"您可以定义一个重载<<的函数函数,用于显示Employee对象信息(即重载<<函数将调用虚拟打印函数.)"
我知道朋友的功能是什么,但我不记得了解过载<<部分.我试着寻找一些简单的复制粘贴实现...但无论我尝试什么,我的编译器都会突然爆发.
我该如何实际实现这个?这是我到目前为止的代码:
#include <iostream>
#include <string>
using namespace std;
//Base class
class Employee
{
public:
Employee(string a, string b, string c, string d);
~Employee();
int virtual earnings();
void virtual print();
protected:
string first, last, brithday, department;
};
Employee::Employee(string f, string l, string b, string d)
{
first = f;
last = l;
brithday = b;
department = d; //department code
cout << "Employee created." << endl;
}
Employee::~Employee(void)
{
cout << "Employee deleted." << endl;
}
int Employee::earnings() …Run Code Online (Sandbox Code Playgroud) 我无法理解我班上的operator <<的行为:
标题:
#ifndef VECTOR_H_
#define VECTOR_H_
#include <string>
#include <iostream>
template<class T>
class Vector {
static const int EXPANDER = 10;
T* array;
int next;
int length;
void expand();
void contract();
public:
Vector();
Vector(const Vector& v);
void add(const T e);
T get(int index) const;
bool removeByIndex(int index);
bool remove(T e);
int size() const;
T operator[](int i) const;
T& operator+=(const T& t);
T operator+(const T& s);
friend std::ostream& operator<< (std::ostream& os, const Vector<T>& obj);
friend std::istream& operator>> (std::istream& is, Vector<T>& …Run Code Online (Sandbox Code Playgroud) 我遇到以下情况:
template<typename T, int N>
class Foo
{
};
// specialization for 0
template<typename T>
class Foo<0>
{
friend class Foo<T, 1>;
};
Run Code Online (Sandbox Code Playgroud)
也就是说,我需要那Foo<T, 1>是朋友Foo<T, 0>,但我有编译错误.这可能吗?
我想知道为什么这个代码是正确的以及编译器如何知道首先创建类A的obj,我认为它不应该编译,因为B的ctor请求类型A的参数而不是int
class A
{
int a1;
public:
A(int i) { cout << i << "A"<<endl;}
friend class B;
};
class B
{
public:
B(A a) {cout <<"B" <<a.a1;}
};
void main()
{
B b(7);
}
Run Code Online (Sandbox Code Playgroud)
输出:7A B7
所以我正在尝试制作一个国际象棋游戏(对我来说是一个终极挑战),而我是这部分的残障......
所以我创建了一个片段对象,其思想是在主游戏代码中,我有一个片段数组,我将数组的地址传递给函数"InitilisePieces"和团队(黑色或白色),它将分配所有的碎片.所以我让这个函数成为了访问所有私有成员的朋友,并且它出现了一个错误,说"无法访问",我不明白我做的事情有什么问题.任何帮助将不仅仅是赞赏!
侧注:像State_和_Location这样的东西,以及正确定义的结构和枚举等,不是问题...(我不认为)
头文件包含:
class __Piece
{
private:
State_ e_state;
Piece_Type_ e_type;
Team_ e_team;
_Location st_location;
friend void InitilisePieces ( __Piece(*)[16], Team_);
public:
__Piece ();
};
Run Code Online (Sandbox Code Playgroud)
.cpp文件包含:
void InitilisePieces ( __Piece * pao_piece[16], Team_ )
{
int n_count;
for ( n_count = 0; n_count < 16; n_count++ )
{
pao_piece[ n_count ]->e_state;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
谢谢你的解释,我得到了我现在出错的地方......那么传递__Piece数组地址的参数是什么?
class Bishop : public ChessPiece {
public:
friend class Queen;
Bishop(string colorIn, string nameIn);
//isLegalMove for bishop
//implemented this function for queen
bool isLegalBishopMove(int xSrc, int ySrc, int xDest, int yDest);
//Can move along any diagnol
virtual bool isLegalMove(int xSrc, int ySrc, int xDest, int yDest) ;
};
class Queen : public ChessPiece {
public:
//friend class Bishop;
Queen(string colorIn, string nameIn);
//friend bool Bishop::isLegalBishopMove(int xSrc, int ySrc, int xDest, int yDest);
virtual bool isLegalMove(int xSrc, int ySrc, int xDest, …Run Code Online (Sandbox Code Playgroud) 无法识别朋友功能
#include <iostream>
#include <cmath>
class hello {
private:
int a, b;
public:
hello(int a, int b) {
this->a = a;
this->b = b;
}
friend int add();
};
int add() {
return a + b;
}
int main() {
hello number(1, 2);
std::cout << number.add();
}
Run Code Online (Sandbox Code Playgroud)
预期的:它应该添加类hello的2个membervariables(带有friend函数!)
实际结果:朋友功能“添加”未被识别为类成员
(错误消息:错误:“类你好”没有名为“添加”的成员)
add()中的a和b也无法识别。(明显)