这个私有变量是怎样的;未在此范围内声明“;? 我现在正在尝试更多地了解C++中面向对象的设计(熟悉java),并且我正在运行一些墙。我正试图将这个项目放在一起,以便在一个使用SFML构建的图形和音频游戏中学习这些原则。我有以下两个文件
WorldObject.h这个私有变量是怎样的;未在此范围内声明“;? 我现在正在尝试更多地了解C++中面向对象的设计(熟悉java),并且我正在运行一些墙。我正试图将这个项目放在一起,以便在一个使用SFML构建的图形和音频游戏中学习这些原则。我有以下两个文件,c++,oop,scope,sfml,declare,C++,Oop,Scope,Sfml,Declare,WorldObject.h #ifndef WORLDOBJECT_H #define WORLDOBJECT_H #include <SFML/Graphics.hpp> #include <string> #include "ImageManager.h" class WorldObject { private: sf::Sprite _sprite; void SetImagePath(std::string path); sf::Sprite Ge
#ifndef WORLDOBJECT_H
#define WORLDOBJECT_H
#include <SFML/Graphics.hpp>
#include <string>
#include "ImageManager.h"
class WorldObject
{
private:
sf::Sprite _sprite;
void SetImagePath(std::string path);
sf::Sprite GetGraphic();
};
#endif
我认为这两个都没有问题,但是当我试图编译它们时,我从g++收到以下错误:
WorldObject.cpp: In function ‘sf::Sprite GetGraphic()’:
WorldObject.cpp:9: error: ‘_sprite’ was not declared in this scope
make: *** [WorldObject.o] Error 1
我在这段代码中遗漏了什么?试图理解设置继承层次结构的正确方法是迄今为止游戏开发中出现的最大问题,但我知道这主要是因为我更习惯于使用Java的继承模型,而不是C++的多重继承模型。
sf::Sprite GetGraphic()
不正确,它正在声明一个全局GetGraphic
函数。由于GetGraphic
是类WorldObject
的函数,因此它应该是sf::Sprite WorldObject::GetGraphic()
您在WorldObject.cpp>中定义的函数GetGraphics
。使用
sf::Sprite WorldObject::GetGraphic()
{
return _sprite;
}
而不是
sf::Sprite GetGraphic()
{
return _sprite;
}
<>注意,C++编译器只抱怨丢失的<代码> Word对象:GETCGRAPH/代码>如果这个函数从程序中的某个地方调用。< /P> < P>我没有做很多C++,但是我认为你需要<代码> Word对象::GETGISTION/COR>而不是< GetGraphic > <代码> WorldObject。CPP < P>我相信你的意思是:
sf::Sprite WorldObject::GetGraphic()
不是
sf::Sprite GetGraphic()
在WorldObject.cpp中,/`GetGraphic()`是`WorldObject`类的成员函数。因此,您有两个选项需要更正-
// `GetGraphic()` is a member function of `WorldObject` class. So, you have two options to correct-
//Either define the functionality of `GetGraphic()` in the class definition itself.
#ifndef WORLDOBJECT_H
#define WORLDOBJECT_H
#include <SFML/Graphics.hpp>
#include <string>
#include "ImageManager.h"
class WorldObject
{
private:
sf::Sprite _sprite;
void SetImagePath(std::string path);
sf::Sprite GetGraphic() // Option 1
{
return _sprite;
}
};
#endif
//When providing the member function definition, you need to declare that it is in class scope.
// Option 2 => Just prototype in class header, but definition in .cpp
sf::Sprite WorldObject::GetGraphic()
{
return _sprite;
}
//或者在类定义本身中定义`GetGraphic()`的功能。
#ifndef WORLDOBJECT_H
#定义世界对象
#包括
#包括
#包括“ImageManager.h”
类世界对象
{
私人:
雪碧;
void SetImagePath(std::string path);
sf::Sprite GetGraphic()//选项1
{
返回精灵;
}
};
#恩迪夫
//提供成员函数定义时,需要声明它在类范围内。
//选项2=>只在类标题中使用原型,但在.cpp中使用定义
sf::Sprite WorldObject::GetGraphic()
{
返回精灵;
}
// `GetGraphic()` is a member function of `WorldObject` class. So, you have two options to correct-
//Either define the functionality of `GetGraphic()` in the class definition itself.
#ifndef WORLDOBJECT_H
#define WORLDOBJECT_H
#include <SFML/Graphics.hpp>
#include <string>
#include "ImageManager.h"
class WorldObject
{
private:
sf::Sprite _sprite;
void SetImagePath(std::string path);
sf::Sprite GetGraphic() // Option 1
{
return _sprite;
}
};
#endif
//When providing the member function definition, you need to declare that it is in class scope.
// Option 2 => Just prototype in class header, but definition in .cpp
sf::Sprite WorldObject::GetGraphic()
{
return _sprite;
}