我创造了一个班级角色和一个班级角色。然后我使用point作为我的角色类中的对象。在我的角色类中,我想在函数“moveCharacter”中设置点对象,但编译器给出了错误:
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);
}
答案 0 :(得分:0)
更改
void moveCharacter(const double dX, const double dY, const double dZ)
到
void Character::moveCharacter(const double dX, const double dY, const double dZ)
因此,它具有类成员变量的范围,而不是独立的函数