c++ - 错误 : ‘length’ was not declared in this scope c++

标签 c++ inheritance scope

我是初学者 c++ 程序员,这甚至是我的第一个程序(对于那些非常热衷于否定的人)。我用 c 编写了相同的代码,但现在尝试用 c++ 编写。 我在哪里收到以下错误。

错误:“长度”未在此范围内声明

我的代码如下。

#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;


 class Huffman
 {
    public:

    int data_size, length; //THis length variable is not accessible in main function below in main function.
     Huffman(char *filename);
   ~Huffman();

    struct Huffman1
    {
    int value;
    unsigned char sym;                 /* symbol */
    struct Huffman1 *left,*right;    /* left and right subtrees */
    };   typedef struct Huffman1 Node;

 };

 Huffman::Huffman(char * file_name)
 {

 //I will do something here soon

 }
  Huffman::~Huffman()
 {

 }


 int main(int argc, char * * argv)
 {
      length=10; //Not accessible here.
     if (argc < 2)
    {
        cout<<"Ohh.. Sorry , you forgot to provide the Input File please" <<endl;
        return(0);
    }
     Huffman Object1(argv[1]);

     return(0);


}

我不确定是不是c++编程错误,因为可能是因为我正在编译它g++ Filename.c -o filename。如果这是编程错误或者是由于我的编译方式,有人可以更正吗? 谢谢。

最佳答案

length是类的成员,所以在类外不存在。

您可以在创建类 Huffman 的对象后访问 lenth,如下所示

Huffman Object(argv[1]);
Object.length = 10;

关于c++ - 错误 : ‘length’ was not declared in this scope c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21714562/

相关文章:

c# - 作用域如何影响 "do...while"循环,反之亦然?

c++ - 将 char * 转换为 wchar_t *

c++ - 如何避免使用 Try Catch (C++) 在整数值中输入字符串

c++ - 如何判断 c 函数 atoi 是否失败或是否是一串零?

java - 继承的输入不匹配异常错误

sql-server - 在存储过程中的 while block 内定义的变量范围 - SQl Server

javascript - $watch 没有在函数之外更新数组?

c++ - 如何调整数组的大小 C++

c++ - new with inheritance 的不当使用?

java显式调用super方法