0
我做了一個類的點和一個類的字符。然後我在角色類中使用了點作爲對象。在我的性格類我想設置的功能 「moveCharacter」 點對象,但編譯器給我的錯誤:(對象)在這個範圍內沒有聲明m_cPoint
character.cpp: In function 'void moveCharacter(double, double, double)':
character.cpp:17:2: error: 'm_cPoint' was not declared in this scope
m_cPoint.setPoint(dX, dY, dZ);
^
point.h:
#ifndef POINT_H
#define POINT_H
#include <iostream>
class Point
{
public:
Point(const double dX, const double dY, const double dZ);
double getX() const { return this->m_dX; }
double getY() const { return this->m_dY; }
double getZ() const { return this->m_dZ; }
void setPoint(const double dX, const double dY, const double dZ);
friend std::ostream& operator<<(std::ostream &out, const Point &cPoint);
private:
double m_dX, m_dY, m_dZ;
};
#endif
point.cpp:
#include "point.h"
Point::Point(const double dX, const double dY, const double dZ)
: m_dX{dX}, m_dY{dY}, m_dZ{dZ}
{
}
std::ostream& operator<<(std::ostream &out, const Point &cPoint)
{
out << "(" << cPoint.getX() << ", " << cPoint.getY() << ", " << cPoint.getZ() << ")";
return out;
}
void Point::setPoint(const double dX, const double dY, const double dZ)
{
this->m_dX = dX;
this->m_dY = dY;
this->m_dZ = dZ;
}
character.h:
#ifndef CHARACTER_H
#define CHARACTER_H
#include <iostream>
#include <string>
#include "point.h"
class Character
{
public:
Character(const std::string strName, const Point &cPoint);
friend std::ostream& operator<<(std::ostream &out, const Character &cCharacter);
std::string getName() const { return this->m_strName; }
Point getLocation() const { return this->m_cPoint; }
void moveCharacter(const double dX, const double dY, const double dZ);
private:
std::string m_strName;
Point m_cPoint;
};
#endif
character.cpp:
#include "character.h"
Character::Character(const std::string strName, const Point &cPoint)
: m_strName{strName}, m_cPoint{cPoint}
{
}
std::ostream& operator<<(std::ostream &out, const Character &cCharacter)
{
out << cCharacter.getName() << " is at location: " << cCharacter.getLocation();
return out;
}
void moveCharacter(const double dX, const double dY, const double dZ)
{
m_cPoint.setPoint(dX, dY, dZ);
}
和可能修復'this-> m_cPoint.setPoint(dX,dY,dZ)'? – mostruash
噢,我的上帝......有時可能有時監督最簡單的事情?謝謝! – Simpler
你不需要'this->' –