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)から値を出力するにはどうすればよいですか?

以下はAgent.hです。

#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;
}
4

1 に答える 1

5

関数を定義する必要がありますostream& operator<<(ostream&, const Agent&)

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魔法で印刷するのではなく、その方法を説明する必要があります。

于 2012-10-15T11:56:53.403 に答える