Thread: Calling functions

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Calling functions

    Hello,

    I have an assignment I have been struggling with,specifically with regards to calling a function from within a member function. Instead of using the actual assignment, I have thrown together an example to in hopes that someone could point me in the right direction. my example is comprised of two header files and two source files.

    Code:
    // somenum.h
    
    #ifndef SOMENUM_H
    #define SOMENUM_H
    
    
    
    int aNumber(int num, int num2);
    
    
    #endif
    
    
    //second.h
    
    #ifndef SECOND_H
    #define SECOND_H
    
    
    
    
    struct Value {
        
        int zero;
        int one;
        
        Value(int num1, int num2);
        
        void addNum (int value1, int value2);
    }
    
    
    //number.cpp
    
    #include "second.h"
    #include "somenum.h"
    
    using namespace std;
    
    
    Value value ( int a, int b)
    {
        zero = a;
        one = b;
    };
    
    
    void Value::addNum(int someVal1, int someVal2)
    {
        //call aNumber here, pass params
    }
    
    
    
    //somenum.cpp
    
    #include "somenum.h"
    
    using namespace std;
    
    int aNumber(int num, int num2)
    {
        int num3 = num + num2;
        
        return num3;
    }

    Hopefully my example makes sense, I didn't include a main() as it doesnt come into play at this point. Basically I want to call aNumber inside addNum, but I have not been able to figure out how to accomplish this. I have tried the following:
    Code:
    void Value::addNum(int someVal1, int someVal2)
    {
       aNumber(someVal1, someVal2);
    }
    
    void Value::addNum(int someVal1, int someVal2)
    {
       value.aNumber(someVal1, someVal2);
    }
    
    void Value::addNum(int someVal1, int someVal2)
    {
       Value value;
       value.aNumber(someVal1, someVal2);
    }
    If someone could help me out I would greatly appreciate it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember, aNumber is a non-member function (and it looks rather badly named: I would have expected addNumber or addNumbers to be the name instead), therefore you would not expect the use of notation for a member function:
    Code:
    value.aNumber(someVal1, someVal2);
    It also returns a value that is the entire point of the function, so you would not expect something like this that discards the return value:
    Code:
    aNumber(someVal1, someVal2);
    Rather, you would expect something like this:
    Code:
    return aNumber(someVal1, someVal2);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions
    By cda67 in forum C Programming
    Replies: 2
    Last Post: 10-14-2011, 11:56 PM
  2. calling functions
    By njasmine1 in forum C Programming
    Replies: 2
    Last Post: 12-29-2010, 10:28 AM
  3. Help calling and using a functions
    By method in forum Windows Programming
    Replies: 0
    Last Post: 07-08-2006, 04:08 PM
  4. not calling functions
    By BungleSpice in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:36 AM
  5. calling functions::
    By Yoshi in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 02:34 PM

Tags for this Thread