在两个构造函数之前将 CPP 文件中的静态数据成员初始化为 0
Initialisation of my static data member to 0 in CPP file before two constructors
我可以这样做吗?
它告诉我未解析的外部符号 WTH!
我真的需要将 GameObjectCount
的值初始化为 Zero
。
此外,当我使用默认构造函数构建对象时,我无法访问其任何成员!
为什么?
main.cpp
#pragma once
#include <iostream>
#include <string>
#include "MapTile.h"
#include "GameOb.h"
MapTile backGRIND("Name");
GameOb test();
int main(int, char**){ ////this main is for SDL2////
std::cout << backGRIND.texturePath << std::endl;
std::cout << backGRIND.GameObjectCount << backGRIND.x << backGRIND.ObjectID << std::endl;
GameOb.cpp
#include "GameOb.h"
int GameOb::GameObjectCount = 0;
GameOb::GameOb()
{
GameOb::GameObjectCount++;
GameOb::x = 0;
GameOb::y = 0;
GameOb::ObjectID = GameObjectCount;
GameOb::texturePath = std::string("Ressources/GameObjects/");
std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}
GameOb::GameOb(int x, int y)
{
GameOb::GameObjectCount++;
GameOb::x = x;
GameOb::y = y;
GameOb::ObjectID = GameObjectCount;
GameOb::texturePath = std::string("Ressources/GameObjects/");
std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}
GameOb.h
#pragma once
#include <string>
#include <iostream>
class GameOb
{
public:
GameOb();
GameOb(int, int);
~GameOb();
int x;
int y;
int ObjectID;
std::string texturePath;
void update(int, int, int);
static int GameObjectCount;
};
MapTile.cpp
#include "MapTile.h"
MapTile::MapTile(std::string name)
{
GameOb::texturePath += std::string("MapTile/");
}
MapTile::~MapTile()
{
}
MapTile.h
#pragma once
#include <string>
#include <iostream>
#include "GameOb.h"
class MapTile :
public GameOb
{
public:
MapTile(std::string);
~MapTile();
};
好的,完成了
为什么我不能访问测试实例成员??当我写测试时。(没有显示)??帮助...我真的很新。
除非 GameOb.h
至少包含以下内容:
class GameOb
{
static int GameCount;
static int ObjectID;
static int x;
static int y;
static std::string texturePath;
GameOb();
GameOb(int x, int y);
}
你的代码不可能编译。
我不认为 x
和 y
以及 ObjectID
和 texturePath
真的是静态的,但这就是您发布的代码。所以你还需要查找在构造函数中初始化数据成员的正确方法,以及引用非静态成员的正确方法。
我可以这样做吗? 它告诉我未解析的外部符号 WTH!
我真的需要将 GameObjectCount
的值初始化为 Zero
。
此外,当我使用默认构造函数构建对象时,我无法访问其任何成员!
为什么?
main.cpp
#pragma once
#include <iostream>
#include <string>
#include "MapTile.h"
#include "GameOb.h"
MapTile backGRIND("Name");
GameOb test();
int main(int, char**){ ////this main is for SDL2////
std::cout << backGRIND.texturePath << std::endl;
std::cout << backGRIND.GameObjectCount << backGRIND.x << backGRIND.ObjectID << std::endl;
GameOb.cpp
#include "GameOb.h"
int GameOb::GameObjectCount = 0;
GameOb::GameOb()
{
GameOb::GameObjectCount++;
GameOb::x = 0;
GameOb::y = 0;
GameOb::ObjectID = GameObjectCount;
GameOb::texturePath = std::string("Ressources/GameObjects/");
std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}
GameOb::GameOb(int x, int y)
{
GameOb::GameObjectCount++;
GameOb::x = x;
GameOb::y = y;
GameOb::ObjectID = GameObjectCount;
GameOb::texturePath = std::string("Ressources/GameObjects/");
std::cout << "Object has been created with Object ID : " << GameOb::ObjectID << std::endl;
}
GameOb.h
#pragma once
#include <string>
#include <iostream>
class GameOb
{
public:
GameOb();
GameOb(int, int);
~GameOb();
int x;
int y;
int ObjectID;
std::string texturePath;
void update(int, int, int);
static int GameObjectCount;
};
MapTile.cpp
#include "MapTile.h"
MapTile::MapTile(std::string name)
{
GameOb::texturePath += std::string("MapTile/");
}
MapTile::~MapTile()
{
}
MapTile.h
#pragma once
#include <string>
#include <iostream>
#include "GameOb.h"
class MapTile :
public GameOb
{
public:
MapTile(std::string);
~MapTile();
};
好的,完成了
为什么我不能访问测试实例成员??当我写测试时。(没有显示)??帮助...我真的很新。
除非 GameOb.h
至少包含以下内容:
class GameOb
{
static int GameCount;
static int ObjectID;
static int x;
static int y;
static std::string texturePath;
GameOb();
GameOb(int x, int y);
}
你的代码不可能编译。
我不认为 x
和 y
以及 ObjectID
和 texturePath
真的是静态的,但这就是您发布的代码。所以你还需要查找在构造函数中初始化数据成员的正确方法,以及引用非静态成员的正确方法。