简体   繁体   English

c ++从标头访问变量与cpp文件中的变量

[英]c++ Accessing variable from header vs in cpp file

So I've been putting a lot of my variables (ie strings, ints, chars etc) into my header files for all my classes that I am creating. 因此,我已经将许多变量(即字符串,整数,字符等)放入我正在创建的所有类的头文件中。 What I have noticed today is that by doing so I often get stackdumps when I try and access methods that use these variables. 我今天注意到的是,通过这样做,在尝试访问使用这些变量的方法时,经常会得到堆栈转储。 However if I remove the variable from the header file and place it directly into my cpp file it works a treat. 但是,如果我从头文件中删除该变量并将其直接放入我的cpp文件中,则可以使用。 Are we only supposed to use method declaration inside c++ header files? 我们只应该在c ++头文件中使用方法声明吗? If not why would this be occurring (all the variables are private but are being accessed via set and get methods). 如果不是这样,为什么会发生(所有变量都是私有的,但是可以通过set和get方法进行访问)。

Thanks 谢谢

In main.cpp: 在main.cpp中:

GameManager gamemngr;

GameManager.h 游戏管理器

#include <string>
#include "Location.h"
#ifndef GAMEMANAGER_H_
#define GAMEMANAGER_H_

class GameManager {
public:
    GameManager();
    Location* curLoc;
    std::string gethelpMenu();
    void movePlayer(int i);
private:
    Location one, two;
    std::string helpMenu;
    void initialiseLocations();
};

#endif /* GAMEMANAGER_H_ */

Location.h 位置.h

#include <string>
#ifndef LOCATION_H_
#define LOCATION_H_

class Location {
public:
    Location();
    void setEdges(Location *n, Location *e, Location *s, Location *w);
    Location* getEdge(int i);
    void setDescription(std::string s);
    std::string getDescription();
private:
    Location* pathways[];
    bool blocked[4];
    bool locked[4];

};

#endif /* LOCATION_H_ */

If I add a std::string description; 如果我添加一个std::string description; to the location header and then try and access it via curLoc->getDescription it just stack dumps as soon as it gets to that line in the program. 到位置标头,然后尝试通过curLoc->getDescription访问它,只要到达程序中的该行,它就将堆栈转储。 I'm assuming my pointer is pointing to invalid memory but curLoc has the same memory address as the object "one". 我假设我的指针指向无效的内存,但是curLoc与对象“一个”具有相同的内存地址。 Am I incorrectly instantiating my objects or something? 我是否错误地实例化了我的对象或其他东西?

EDIT: I will also add I do set it to a default value in the constructor to make sure the string is properly initialised but that has no effect. 编辑:我还将添加我确实将其设置为构造函数中的默认值,以确保正确初始化了字符串,但这没有效果。

Location Implementation (with description placed inside the header file as per my original implementation): 位置实现(根据我的原始实现,其说明位于头文件中):

#include "Location.h"
#include <string>
#include <iostream>

Location::Location() {
    description = "";
    for (int i = 0; i < 4; i++) {
        pathways[i] = NULL;
        blocked[i] = false;
        locked[i] = false;
    }
}

void Location::setEdges(Location *n, Location *e, Location *s, Location *w) {
    pathways[0] = n;
    pathways[1] = e;
    pathways[2] = s;
    pathways[3] = w;
}

Location* Location::getEdge(int i) {
    if(pathways[i] == NULL) {
        return this;
    } else {
        return pathways[i];
    }
}

void Location::setDescription(std::string s) {
        description = s;
}

std::string Location::getDescription() {
    return description;
}

I should probably also add this only seems to be happening with my description string and not the edges methods I have defined as well, as far as I can tell they are working (I need the descriptions to double check my pointers location to be sure but it doesn't stackdump or throw errors). 据我可能还应该补充一点,这似乎只发生在我的描述字符串中,而不是我已经定义的edge方法,据我所知它们正在运行(我需要描述来再次检查指针位置,以确保但它不会stackdump或引发错误)。

There is compiler behavior where if the compiler doesn't see your variables being used in a .cpp file, then it will cut the variables from the class, unless there is a clear compilation flag to tell it not to. 存在编译器行为,如果编译器看不到.cpp文件中正在使用您的变量,则它将从该类中删除变量,除非有明确的编译标志告诉它不要使用。 You should always declare your methods in .cpp files. 您应该始终在.cpp文件中声明您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM