简体   繁体   English

C ++-在另一个函数中使用的返回值

[英]C++ - return value used in another function

I have a problem with using return value from other function. 我在使用其他函数的返回值时遇到问题。 Here are some details: 以下是一些详细信息:

Class1.cpp : Class1.cpp:

Double Class1::SetValue (double value2)
{
    // method code
    Return value_set;
}

Class2.cpp: Class2.cpp:

Void Class2::CountValue(double a)
{
    Class1 *object1;
    Double value_received= object1->SetValue(object1->value2)
    // do something with value_received
}

The problem is that I need to use value_set from SetValue in CountValue in Class2. 问题是我需要在Class2的CountValue中使用SetValue中的value_set。 The code above is giving me error: 上面的代码给我错误:

Unhandled exception at 0x6462696c in xxx.exe: 0xC0000005: Access violation.

Can you help me, please? 你能帮我吗?

In your SetValue call, you're passing a parameter of object1->value2 . SetValue调用中,您传递的是object1->value2的参数。 But you haven't set the pointer object1 yet, it's still uninitialized. 但是您尚未设置指针object1 ,它仍未初始化。 That's undefined behavior. 那是未定义的行为。 You're lucky it crashed or you might have had a much harder time finding it. 您很幸运它崩溃了,或者您可能很难找到它。

There are a couple of issues. 有几个问题。 There should be no issues with initializing a variable with the return value to some function. 用某个函数的返回值初始化变量应该没有问题。 We do this all the time, even when you don't normally see the typical function calls like the arithmetic operators or bitwise operators. 即使您通常看不到算术运算符或按位运算符之类的典型函数调用,我们也始终这样做。

With your function 随着你的功能

double Class1::SetValue(double value2)
{
  //generally you are setting some variable that is a part of Class1
  this->value = value2;

  //if you are returning something you might want to have some error code 
  //just in case something went wrong, but it looks like you are just
  //returning the number that you are sending in as a parameter.

  return value2;  
}

I'm not sure what is going on in your next function but we can dissect it. 我不确定您的下一个函数中发生了什么,但是我们可以对其进行剖析。

void Class2::CountValue(double a)
{
    Class1 *object1;//this is a pointer to an object, not an instance of an object

    //                       object1 is not initialized so you cannot
    //                       use object1->value2 even if it is set in
    //                       your default constructor

    double value_received = object1->SetValue(object1->value2);
    // do something with value_received
}

In order for your code to work you might want to alter it to 为了使您的代码正常工作,您可能需要将其更改为

void Class2::CountValue(double a)
{
  Class1 object1;//not a pointer to Class1 anymore

  // This will work if object1.value2 is set in the default constructor

  double value_received = object1.SetValue(object1.value2);
  // do something with value_received
}

or if you are trying to set both value_recieved and object1.value2 we could do 或者,如果您尝试同时设置value_recieved和object1.value2,我们可以做

void Class2::CountValue(double a)
{
  Class1 object1;//not a pointer to Class1 anymore

  double value_received = object1.SetValue(12.2);
  // do something with value_received
}

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

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