简体   繁体   English

通过引用使用函数调用时发生运行时错误?

[英]Runtime Error in using function call by reference?

I have a class defined as, 我有一个定义为

class dimension{
public:
    dimension();
    const char* getname();
    long ing getlength();
    void setname(const char* text)
    void setlength(long int size)
    virtual ~dimension();
private:
    const char* name;
    long int length;
}

I am getting error regarding the functions setname and setlength are of importance so their definitions are as, 我在函数setnamesetlength的重要性方面遇到错误,因此它们的定义如下:

void dimension::setname(const char* text)
{
    dimension::name = text;
}
void dimension::setlength(long int size)
{
    dimension::length = size;    
}

Now i have 2 functions which i am using to read an array of objects of class dimension from a file. 现在,我有2个函数,可用于从文件中读取类维的对象数组。 Their definition is as. 他们的定义是。

void read_dimension(dimension** dims, int*ndims, const char* text, long int size)
{
    int i;
    *dims = new dimension[*ndims];
    for(i=0; i<ndims; i++)
    {
        (*dims)[i].setname(text)   
        (*dims)[i].setlength(size) 
    }
}

void read_file(char *path, dimension** dims, int *ndims)
{
    //do-- open file and read the variables ndims, text and size from it.
    read_dimension(dims, ndims, text, size);
    //do-- print name and length of all elements of (*dims).
}

Now, i am calling these functions in main as 现在,我主要将这些功能称为

int main()
{
    //do-- get file path
    dimesnion* gdims;
    int num_dims;
    read_file(path, &gdims, &num_dims);
    //do-- print name and length of each element of gdims.
    return 0;
}

When i run the code the variable "name" printed from the the functions read_dimension() and main() are different but the variable "length" is the same. 当我运行代码时,从函数read_dimension()main()打印的变量"name"不同,但变量"length"相同。
I can't figure out why this is happening. 我不知道为什么会这样。
I would be glad if anyone could help. 如果有人可以帮助我,我会很高兴。

There's no doubts text is a local variable in read_dimension function, probably an array of char. 毫无疑问,文本是read_dimension函数中的局部变量,可能是char数组。 dimension::setname assigns one pointer to another, as already mentioned, what happens when text variable in read_dimension function goes out of scope? 如上所述,Dimension :: SetName将一个指针分配给另一个指针,当read_dimension函数中的文本变量超出范围时会发生什么? It'll die... Now your pointer in dimension class points to "nowhere". 它会死...现在您在维度类中的指针指向“无处”。 I prefer to use const char * as string literals or as pointer to some char in array of chars. 我更喜欢使用const char *作为字符串文字或作为char数组中某些char的指针。 If you want to store a string you'd copy it via strcpy maybe, of course destination array of char with sufficient space must exists. 如果要存储字符串,则可以通过strcpy复制它,当然,必须存在具有足够空间的char目标数组。

What's the purpose of this code: dimension::name = text; 这段代码的目的是什么:Dimensions :: name = text; and dimension::length = size; 和维度::长度=大小; in setname and setlength methods? 在setname和setlength方法中? You're in the scope of dimension class, aren't you? 您属于维度类的范围,不是吗?

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

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