C++ Function supposed to return Long, returning Integer like value instead

trueCamelType

While working on a fairly large project, I happened to notice that one of my functions that is supposed to return a Long value is either returning an Integer. I reproduced the error in a very small environment thinking that it would make the problem clear to me, but I'm still not seeing the issue. The input is 1.123, and the return value is 1. If I input any Long, for example; 123.456, it will only return 123. What am I not seeing?

Source1.cpp

#ifndef HEADER_H
#define HEADER_H


using namespace std;

class testClass
{

private:
    long m_testLong = 0.0;

public:

    long getTestLong();
    void setTestLong(long sn);
};

#endif

Header.h

#include "Source1.cpp"
#include <string.h>

void testClass::setTestLong(long sn)
{
    m_testLong = sn;
}

long testClass::getTestLong()
{
    return m_testLong;
}

Source.cpp

#include <iostream>
#include "Source1.cpp"
#include "Header.h"

using namespace std;

int main(void)
{

testClass *myClass = new testClass;

cout << "Setting test long value using setTestLong function -- 1.123" << endl;
myClass->setTestLong(1.123);
long tempTestLong = 0.0;
tempTestLong = myClass->getTestLong();
cout << tempTestLong << endl;

system("Pause");
return 0;
}

OK, so the answer was painfully simple. I hadn't worked with longs before, but I thought I knew what they were. I didn't.

So longs and integers both are whole numbers, and having the type listed as long made me assume an integer wouldn't work, and I tested the function with a double because of my misunderstanding. Thanks for the help!

paxdiablo

The long and int types are integral types, they can only hold whole numbers like 7 or 42.

You should be using float or double as a type, preferably the latter for increased range and precision. That will allow you to hold real numbers such as 3.141592653589 or 2.718281828459.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

c function returning nan instead of double

분류에서Dev

Seems like My stored procedure is returning Integer

분류에서Dev

PHP function not returning value

분류에서Dev

C call a different function based on a given integer value

분류에서Dev

How to manage long integer value in url

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Function not returning the Value of its Key

분류에서Dev

Return statement in JavaScript is not returning the right value

분류에서Dev

APL return value of a function

분류에서Dev

get function error in c. trying to return string value.

분류에서Dev

How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

분류에서Dev

Object function returns function instead of value

분류에서Dev

function returning wrong value while executing

분류에서Dev

Why is AES function returning different value?

분류에서Dev

Returning a value from a function with Alamofire and SwiftyJson

분류에서Dev

Setting Integer value in Objective c

분류에서Dev

json_decode is returning a null value instead of an array

분류에서Dev

nodejs function doesnt return a value

분류에서Dev

Pass in function or value and always return the value

분류에서Dev

Python recursive function returns None instead of value

분류에서Dev

Returning an integer array

분류에서Dev

C++ User enters a non-integer value for integer variable

분류에서Dev

Can long int be returned by a function in c?

분류에서Dev

C - trying to assign return value from a function to a variable causes segmentation fault

분류에서Dev

C Structure initialization (Array of integers and an integer value)

분류에서Dev

How to get a pointer on the return value of a function?

분류에서Dev

Python: return a default value if function or expression fails

분류에서Dev

Why does a void function return a value?

분류에서Dev

getting address of pointer from function return value

Related 관련 기사

  1. 1

    c function returning nan instead of double

  2. 2

    Seems like My stored procedure is returning Integer

  3. 3

    PHP function not returning value

  4. 4

    C call a different function based on a given integer value

  5. 5

    How to manage long integer value in url

  6. 6

    Returning a value from a recursive function

  7. 7

    Function not returning the Value of its Key

  8. 8

    Return statement in JavaScript is not returning the right value

  9. 9

    APL return value of a function

  10. 10

    get function error in c. trying to return string value.

  11. 11

    How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

  12. 12

    Object function returns function instead of value

  13. 13

    function returning wrong value while executing

  14. 14

    Why is AES function returning different value?

  15. 15

    Returning a value from a function with Alamofire and SwiftyJson

  16. 16

    Setting Integer value in Objective c

  17. 17

    json_decode is returning a null value instead of an array

  18. 18

    nodejs function doesnt return a value

  19. 19

    Pass in function or value and always return the value

  20. 20

    Python recursive function returns None instead of value

  21. 21

    Returning an integer array

  22. 22

    C++ User enters a non-integer value for integer variable

  23. 23

    Can long int be returned by a function in c?

  24. 24

    C - trying to assign return value from a function to a variable causes segmentation fault

  25. 25

    C Structure initialization (Array of integers and an integer value)

  26. 26

    How to get a pointer on the return value of a function?

  27. 27

    Python: return a default value if function or expression fails

  28. 28

    Why does a void function return a value?

  29. 29

    getting address of pointer from function return value

뜨겁다태그

보관