0
는 다음 코드에 의해 반환 된 값을 인쇄하기 위해 노력하고있어 :이중 포인터에서 값을 얻는 방법은 무엇입니까?
Agent** Grid::GetAgent(int x, int y)
{
return &agents[x][y];
}
그것은 이중 포인터를 반환, 그리고
std::cout << *grid.GetAgent(j, k) << endl;
는 메모리 위치를 제공 인쇄,하지만 난
을하려고 할 때
std::cout << **grid.GetAgent(j, k) << endl;
오류가 발생합니다.
main.cpp:53: error: no match for ‘operator<<’ in ‘std::cout << * * grid.Grid::GetAgent(j, k)’
* grid.GetAgent (j, k)의 값을 어떻게 인쇄 할 수 있습니까?
다음은, 당신은
ostream& operator<<(ostream& out, const Agent& x)
{
// your code to print x to out here, e.g.
out << (int)x.GetType() << ' ' << x.GetFitness() << ' ' << x.GetAge() << '\n';
return out;
}
이
C++ 마법에 의해 Agent
를 인쇄하지 않는 기능 ostream& operator<<(ostream&, const Agent&)
#ifndef AGENT_H
#define AGENT_H
enum AgentType { candidateSolution, cupid, reaper, breeder};
class Agent
{
public:
Agent(void);
~Agent(void);
double GetFitness();
int GetAge();
void IncreaseAge();
AgentType GetType();
virtual void RandomizeGenome() = 0;
protected:
double m_fitness;
AgentType m_type;
private:
int m_age;
};
#endif // !AGENT_H
및 Agent.cpp
#include "Agent.h"
Agent::Agent(void)
{
m_age = 0;
m_fitness = -1;
}
Agent::~Agent(void)
{
}
int Agent::GetAge()
{
return m_age;
}
double Agent::GetFitness()
{
return m_fitness;
}
void Agent::IncreaseAge()
{
m_age++;
}
AgentType Agent::GetType()
{
return m_type;
}
'상담원'이란 무엇입니까? – avakar
나는 똑같은 것을 묻기 시작했다. 멤버 "에이전트"의 타입은 뭐지? – willsteel
'operator << (ostream &, const agent &)'또는 이와 비슷한 것을 정의해야합니다. 당신은 이미 그것이 정의되어 있다고 생각합니까? 네가 그렇게하지 않으면 그게 너의 문제 야. C++이 복잡한 값을 '마법에 의해'인쇄 할 수 있다고 생각하는 첫 번째 초보자는 아닐 것입니다. – john