英文:
How do i reference a nonstatic member of a class in C++
问题
"在编写一个头文件,我试图在主类中更改一个变量,但出现了错误:
“非静态成员必须与特定对象相关”
我并不想创建一个对象,而且这个变量必须是非静态的。我找到的解决方案涉及创建一个对象,或将其设为constexpr静态,虽然可以解决问题,但会使其不可修改。
Engine.h:
class Engine {
public:
playerObject2D mainPlayer;
/* 其他定义 */
};
main.cpp:
#include "Engine.h";
playerObject2D player;
int main(int argc, char* argv[]){
Engine::init(640, 480, "Test", 0, 0, 0, argc, argv);
player.setPos(320, 240); /* 设置玩家位置为屏幕中心 */
player.pspd = 8; /* 设置玩家速度 */
Engine::mainPlayer = player; /* 错误 */
}
英文:
I am writing a header file, i am trying to change a variable in the main class, but an error pops up:
"a nonstatic member must be relative to a specific object"
I am not trying to make an object, and the variable must be nonstatic, The solutions i found involve making an object, or making it a constexpr static, while that works, it would make it unmodifiable
Engine.h:
class Engine {
public:
playerObject2D mainPlayer;
/* other definitions */
};
main.cpp:
#include "Engine.h"
playerObject2D player;
int main(int argc, char* argv[]){
Engine::init(640, 480, "Test", 0, 0, 0, argc, argv);
player.setPos(320,240); /* set player positon to center of screen */
player.pspd = 8; /* set player speed */
Engine::mainPlayer = player; /* Error */
}
答案1
得分: 1
你必须使用以下其中一种方法来创建一个类的实例。
class Engine
{
public:
void DoStuff() {}
};
你可以直接在堆栈上创建实例:
int main(int argc, char* argv[])
{
Engine engine;
engine.DoStuff();
}
或者你可以在堆上创建它,并通过指针访问它:
int main(int argc, char* argv[])
{
Engine* engine = new Engine();
engine->DoStuff();
}
英文:
You have to use one of the ways to create an instance of a class.
class Engine
{
public:
void DoStuff() {}
};
You can either do it directly on the stack
int main(int argc, char* argv[])
{
Engine engine;
engine.DoStuff();
}
Or you can create it on the heap and access it via pointer.
int main(int argc, char* argv[])
{
Engine* engine = new Engine();
engine->DoStuff();
}
答案2
得分: 0
Engine::init
不会初始化全局对象Engine
,它只是Engine
类命名空间中的静态函数(即非成员函数)。如果您的意图是创建一个单例模式,那么您必须通过静态函数来访问该单例的成员:
// 在Engine类中
static Engine& instance()
{
static Engine inst;
return inst;
}
// 在主函数中
Engine::instance().mainPlayer = player;
否则,可以按照其他评论所说,编写一个常规的C++构造函数并创建一个Engine
类的单一实例:https://stackoverflow.com/a/75683067/16062280
英文:
Engine::init
does not initialize a global object Engine
, it's just a static (meaning non-member) function in the namespace of class Engine
. If your intention is to write a singleton, then you have to access members of that singleton through a static function:
// within class Engine
static Engine& instance()
{
static Engine inst;
return inst;
}
// within main
Engine::instance().mainPlayer = player;
otherwise write a regular C++ constructor and create a single instance of class Engine
like the other comment said: https://stackoverflow.com/a/75683067/16062280
答案3
得分: -1
mainPlayer doesn't need to be constexpr. That's a choice you as the programmer makes depending upon your requirements.
BUT a member variable is either an instance member or a class member (i.e. static
), you can't have it both ways.
英文:
mainPlayer doesn't need to be constexpr. That's a choice you as the programmer makes depending upon your requirements.
BUT a member variable is either an instance member or a class member (i.e. static
), you can't have it both ways.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论