简体   繁体   English

从单独的类C ++访问类的成员时遇到麻烦

[英]Trouble accessing members of class from a separate class C++

I am having a lot of trouble with a relatively simple task. 我在一个相对简单的任务上遇到了很多麻烦。 I have two header files, input.h and grains.h , both of which have classes defined within them. 我有两个头文件input.hgrains.h ,它们都具有在其中定义的类。 I have included all header and source files for this project below. 我在下面包含了该项目的所有头文件和源文件。

My problem is that when input->from_file(fname) is executed, the value printed to the screen is correct, let's say it is 4 . 我的问题是,当执行input->from_file(fname) ,打印到屏幕上的值是正确的,假设它是4 Then when it moves on the grains->get_pars() , the value printed to the screen is garbage, usually ~605937280 . 然后,当它在grains->get_pars()上移动时,打印到屏幕上的值是垃圾,通常是~605937280 I know this type of garbage value is indicative of the variable not being set, but I don't understand how it is not being set. 我知道这种类型的垃圾值表示未设置变量,但是我不明白如何未设置它。

My goal is to have input.cpp read some file for some parameters, which are important to grains.cpp , and pass them along. 我的目标是让input.cpp读取一些文件以获取一些参数,这些参数对于grains.cpp很重要,并将它们传递给其他人。 I thought that derived classes would do the trick, but something is not working right. 我以为派生类可以解决问题,但是有些方法行不通。 Any hints on what I have done wrong would be greatly appreciated. 关于我做错了什么的任何提示将不胜感激。 Also, any suggestions to achieve this goal aside from the one I have presented are very welcome, thanks. 此外,非常感谢我提出的除实现上述目标外的任何建议。 Note, the code as shown compiles just fine. 注意,所示的代码可以正常编译。

//input.h
#ifndef Input_H
#define Input_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstring>

class Input {
  protected:
    int in_grains;
  public:
    void from_file(std::string);
};
#endif

//grains.h
#ifndef Grains_H
#define Grains_H
#include "input.h"

class Grains : protected Input {
  protected:
    int grains;
  public:
    void get_pars(void);
};
#endif


//input.cpp
#include "input.h"

void Input::from_file(std::string infile)
{ 
    std::ifstream input(infile.c_str());
    std::istringstream iss;
    std::string line, keyword;

    char arg1[50], arg2[50], arg3[50];

    while(std::getline(input,line)) {
        iss.clear();
        iss.str(line);
        iss >> keyword >> arg1 >> arg2 >> arg3;
        if ((keyword == "GRAINS") || (keyword == "Grains") || (keyword == "grains")) {
            this->in_grains = atoi(arg1);
        }
    }
    fprintf(stdout,"%i\n",in_grains );
}


//grains.cpp
#include "grains.h"

void Grains::get_pars(void)
{ 
    this->grains = in_grains;
    fprintf(stdout,"%i\n",grains );
}


//main.cpp
#include "input.h"
#include "grains.h"

int main(int nargs, char *argv[])
{ 
    Input obj1;
    Input *input = &obj1;

    Grains obj2;
    Grains *grains = &obj2;
    std::string fname = argv[1];

    input->from_file(fname.c_str());
    grains->get_pars();

    return 0;

}

I am guessing that when you execute: 我猜你执行时:

input->from_file(fname.c_str());
grains->get_pars();

you are expecting the in_grains from input to be available as grains->grains . 您期望inputin_grains可以用作grains->grains input and grains are two different objects. inputgrains是两个不同的对象。 in_grains has not been set on the object grains points to. 尚未将in_grains设置为指向对象的grains

Perhaps you meant to use: 也许您打算使用:

int main(int nargs, char *argv[])
{ 
    Grains obj;
    Input *input = &obj;
    Grains *grains = &obj;
    std::string fname = argv[1];

    input->from_file(fname.c_str());
    grains->get_pars();

    return 0;    
}

However, to use that, you have to make Input a public base class of Grain , not a protected base class. 但是,要使用它,必须将Input设为Grainpublic基类,而不是protected基类。

class Grains : public Input {

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

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