简体   繁体   中英

Was not declared in scope when declared in header file?

I want to declare the functions, a vector, and a struct in the header file to be used globally in the main method.

The sicxe_asm.h looks like this:

#ifndef SICXE_ASM_H
#define SICXE_ASM_H

using namespace std;
#include <string>
#include <sstream>
#include <vector> 

class sicxe_asm 
{
public:

private: 
    string filename;    // file to be assembled 
    string validate_hex_address(string str);
    string decimal_to_hex(int dec);
    int hex_to_decimal(string hexvalue);
    bool is_blank_or_comment(vector<string> command);
    bool is_decimal(string tempStr);    

    struct listingFileLine
    {
        string address;
        string label;
        string opcode;
        string operand;

        listingFileLine() : address(""), label(""), opcode(""), operand("") {}   
    };
    vector <listingFileLine> listingFileVec; 
}; 

#endif

In sicxe_asm.cpp i used them like this:

string validate_hex_address(string str)
{
    if(str.at(0) != '$')
    {
        throw driver_exception("Invalid START address");  
    }   
    str.erase(0,1);  // remove the $
    return str; 
}

string decimal_to_hex(int dec)
{
    stringstream ss;
    ss << hex << dec;
    string hexvalue = ss.str();
    return hexvalue; 
}

int hex_to_decimal(string hexvalue)
{
    stringstream ss;
    int decimalvalue; 
    ss << hexvalue;
    ss >> hex >> decimalvalue;
    return decimalvalue; 
}

bool is_blank_or_comment(vector<string> command)
{
    if(command[LABEL] == "" && command[OPCODE] == "" && command[OPERAND] == "")
        return true;
    return false; 
}

bool is_decimal(string tempStr)
{
    const char * str = tempStr.c_str(); 

    for(unsigned int i = 0; i <= strlen(str)-1; i++)
    {
        if(!isdigit(str[i]))
        return false;
    } 
    return true;
} 

Even if I add the scoping style like bool sicxe_asm::is_decimal(string tempStr) I still get not declared in this scope when i call these functions in main().

First, the methods are in the private part, so you can not call them directly. Put them in the public part if you want to call them outside of the class declaration.

Second, the definition (implementation) of the methods in the .cpp file must be with :: , for example:

bool sicxe_asm::is_decimal(string tempStr) { ... }
     ^^^^^^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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