简体   繁体   English

成员变量在一个成员函数的范围内而不是另一个成员函数

[英]Member variable in scope of one member function but not the other

I'm seriously confused why this is happening. 我真的很困惑为什么会这样。 I get an error 'enzyme_acronyms_ was not declared in this scope'. 我得到一个错误'enzyme_acronyms_未在此范围内声明'。 It points to my writeAcronym function but not getAcronym, and both use enzyme_acronyms_. 它指向我的writeAcronym函数,但不是getAcronym,都使用了酶_acronyms_。 What can possibly cause this? 什么可能导致这种情况?

SequenceMap.h SequenceMap.h

#ifndef SequenceMap_h
#define SequenceMap_h

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class SequenceMap
{
    private:
        string recognition_sequence_;
        vector<string> enzyme_acronyms_;
    public:
        string getAcronym();
        void writeAcronym(string an_enz_acro);
}

SequenceMap.cpp SequenceMap.cpp

#include "SequenceMap.h"

string SequenceMap::getAcronym()
{
    return enzyme_acronyms_[0];        //works fine
}

void writeAcronym(string an_enz_acro)
{
    enzyme_acronyms_.push_back(an_enz_acro);     //enzyme_acronyms_ not declared in this scope
}

你错过了第二个函数定义的SequenceMap:: qualified:

void SequenceMap::writeAcronym(string an_enz_acro)

It must be declared like this: 它必须像这样声明:

void SequenceMap::writeAcronym(string an_enz_acro)
{
    enzyme_acronyms_.push_back(an_enz_acro);
}

You forgot the class scope SequenceMap:: . 你忘了了类范围SequenceMap::

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

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