我是初学者 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/