#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Having Unresolved Errors in Input and Output Functions


    Hello,

    I'm having 'unresolved external errors' that I'm trying to figure out.

    I'm basically trying to make a program that will have separate functions for input and output to input an integer and output an integer, and later on two more functions to input a name and output the name.

    There's three ways I could possibly do this: pass-by-value, pass-by-pointer, or pass-by-reference.

    Now I went back in Chapter 5, and took appropriate notes near the top.

    The book itself had 1 okay example for each, that was pretty straightforward in its use, and I tried to model them appropriately.

    My first try was with pointers, which didn't work out too well, so I'm trying it with pass-by-reference this time since it sort of makes more sense in this case.

    So basically, I have a function prototype before the main function that will take in the address of whatever integer I send it via the main function.

    I do a function call within main to send the integer variable I created in main (since this is specified in the instructions to keep relevant data in main), and I allowed the user in the int_inp_func to literally input the integer that they want.

    I then returned this value to the main function.

    I'm thinking maybe I should add the address & ampersand operator on line 111 instead of just returning the integer and hoping the function ITSELF will return the address of the variable.

    I'm getting these two errors which don't really give me the line they occur on, just in how they're 'unresolved' and to me, they seem like fatal errors.

    Here are those errors:

    Code:
    Chapt5_Exercise4.obj : error LNK2019: unresolved external symbol "int __cdecl int_inp_func(int &)" (?int_inp_func@@YAHAAH@Z) referenced in function _main
    Code:
    C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Chapt5_Exercise4\Debug\Chapt5_Exercise4.exe : fatal error LNK1120: 1 unresolved externals
    Here is my code, forgive the notes in the beginning and just ignore them because they have helped me keep track of the different pass-by mechanisms:
    Code:
    #include <iostream>
    // #include <cstring> : later used for string input
    
    /*
    4. Write a native C++ program that reads a number (an integer) and a name 
    (less than 15 characters)from the keyboard. 
    
    Design the program so that the data entry is done in one function, and
    the output in another. 
    
    Keep the data in the main program. 
    
    The program should end when zero is entered for the number. 
    
    Think about how you are going to pass the data between functionsóby value, 
    by pointer, or by reference?
    */
    
    // 4 Functions Will Be Needed, 2 for inputting the number and name separately
    // and the other 2 will be needed to output them
    
    // Notes on passing parameters by value:
    
    // Pass-by-value: Variables or constants that you specify as arguments 
    // are NOT passed to a function AT ALL. 
    
    // COPIES of the arguments are made, and are used as values to be transferred.
    
    // Continue on p. 283!
    
    // Notes on passing parameters by pointer:
    
    // Works just like the pass-by-value mechanism, except that a pointer 
    // is an address of ANOTHER variable, and if you take a copy of the address, 
    // you're STILL pointing to the same variable.
    
    // So if you were to send a pointer that points to an address that 
    // contains the integer value of 3, send that pointer to an increment 
    // function, add 1 to it's '*' form of the pointer, and return the pointer, 
    // you should see a change in both the pointer's memory address as well as 
    // the original variable that you intended to modify
    
    // This is NOT like passing-by-value, the parameter acts as an alias for the 
    // argument passed. 
    
    // This allows the function to access the caller argument directly. 
    
    // 'De-referencing' via pointers is unnecessary in this case.
    // In the example that they give, the parameter already specifies that 
    // its going to take the address, '&', of whatever integer that you pass to 
    // the function as an argument. 
    
    // Inside a function that's going to pass it by reference, 
    // it basically already has the address of whatever integer you sent it, 
    // and its just going to literally add 10 to it, and return it
    
    // Notes on passing parameters by references
    
    // Integer input function prototype:
    
    int int_inp_func(int& int_input);
    
    using namespace std;
    
    int main()
    {
    	int int_input;
    	// char name[14];
    
    	int_input = 1;
    
    	// Function call for integer input:
    	int_inp_func(int_input);
    
    	cout << "Here is the int that you entered: " << int_input << endl;
    
    	cout << endl;
    
    	/* Name Block: Work on later
    
    	cout << "Please enter a name: ";
    	cin >> name;
    	cout << endl;
    
    	cout << "Here is the name that you entered: " << name << endl;
    
    	cout << endl;
    	*/
    
    	return 0;
    
    }
    
    // Integer input function:
    int int_input_func(int& int_input)
    {
    	// Let the user enter the integer:
    	cout << "Please enter an integer: ";
    	cin >> int_input;
    	cout << endl;
    
    	// Give the pointer the address of the value that the user just entered
    	// Error: Cannot convert int* to int
    
    	// I tried adding the dereferencing operator to the actual function,
    	// because I want to take in a pointer, and return a pointer, hence I want
    	// to return a pointer to int, but apparently I can't convert int to int*
    
    	// So: even if I remove the dereferencing operator * before the variable
    	// in the return statement, it gives me an ambiguous error.
    	return int_input;
    
    }
    Thank you so much guys for reading my post!
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Notice what you call the function when you implement it.

    Code:
    // Integer input function:
    int int_input_func(int& int_input)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Should that line be changed to:

    Code:
    int& int_inp_func(int& int_input);
    to reflect the fact that I'm going to return the address back to main?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Actually, going through the book itself, I found this line:
    // One thing to point out:
    // Never ever return a reference to a local variable from a function.

    So, should I redo this function using pointers, correct?

    Obviously, I need to go back and forth between main, passing a variable from one function to the next for input and output.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Saw a similar example and did the following.

    Basically I made the two functions void. One obtains the input by taking in the address of whatever integer you send it, and I also had a char[] array for the second parameter.

    I had calls to both of these functions within a for loop in main that ran as long as number was not equal to 0.

    Here is the code:
    Code:
    #include <iostream>
    // #include <cstring> : later used for string input
    
    /*
    4. Write a native C++ program that reads a number (an integer) and a name 
    (less than 15 characters)from the keyboard. 
    
    Design the program so that the data entry is done in one function, and
    the output in another. 
    
    Keep the data in the main program. 
    
    The program should end when zero is entered for the number. 
    
    Think about how you are going to pass the data between functionsóby value, 
    by pointer, or by reference?
    */
    
    // 4 Functions Will Be Needed, 2 for inputting the number and name separately
    // and the other 2 will be needed to output them
    
    // Notes on passing parameters by value:
    
    // Pass-by-value: Variables or constants that you specify as arguments 
    // are NOT passed to a function AT ALL. 
    
    // COPIES of the arguments are made, and are used as values to be transferred.
    
    // Continue on p. 283!
    
    // Notes on passing parameters by pointer:
    
    // Works just like the pass-by-value mechanism, except that a pointer 
    // is an address of ANOTHER variable, and if you take a copy of the address, 
    // you're STILL pointing to the same variable.
    
    // So if you were to send a pointer that points to an address that 
    // contains the integer value of 3, send that pointer to an increment 
    // function, add 1 to it's '*' form of the pointer, and return the pointer, 
    // you should see a change in both the pointer's memory address as well as 
    // the original variable that you intended to modify
    
    // This is NOT like passing-by-value, the parameter acts as an alias for the 
    // argument passed. 
    
    // This allows the function to access the caller argument directly. 
    
    // 'De-referencing' via pointers is unnecessary in this case.
    // In the example that they give, the parameter already specifies that 
    // its going to take the address, '&', of whatever integer that you pass to 
    // the function as an argument. 
    
    // Inside a function that's going to pass it by reference, 
    // it basically already has the address of whatever integer you sent it, 
    // and its just going to literally add 10 to it, and return it
    
    // Notes on passing parameters by references
    
    // Integer input function prototype:
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void Input_Data(int& number, char name[]) 
    { 
    	cout << "Please enter a number: (Press '0' to exit)" << endl; 
    	
    	cin >> number; 
    
    	cout << endl;
    	
    	if (number != 0) 
    	{ 
    		cout << "Please enter a name: " << endl; 
    		cin >> name; 
    		cout << endl;
    	} 
    }
    
    void Output_Data(int number, char name[]) 
    { 
    	cout << "Your number was: " << number << endl 
    		 << "You're name was: " << name << endl; 
    
    }
    
    using namespace std;
    
    int main()
    {
    	int number;
    	// char name[15];
    
    	char name[15]; 
    	
    	for (;;)
    	{ 
    		Input_Data(number, name); 
    		if (number == 0) 
    			break; 
    		
    		Output_Data(number, name); 
    	} 
    	
    	return 0; 
    
    }
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    The issue you have a different name on the function. You have the prototype for a different function.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,223
    Rep Power
    2222
    Originally Posted by HauntJemimah
    Should that line be changed to:

    Code:
    int& int_inp_func(int& int_input);
    to reflect the fact that I'm going to return the address back to main?
    This is the function prototype that you declared:
    int int_inp_func(int& int_input);

    This is the function call that you made:
    int_inp_func(int_input);

    However, this is the function you created:
    int int_input_func(int& int_input)

    Are they the same? No, they are not. Do they even have the same name? No, they do not.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by dwise1_aol
    This is the function prototype that you declared:
    int int_inp_func(int& int_input);

    This is the function call that you made:
    int_inp_func(int_input);

    However, this is the function you created:
    int int_input_func(int& int_input)

    Are they the same? No, they are not. Do they even have the same name? No, they do not.
    True, the naming on my part was absolutely horrible! Should have asked before I made the functions actually.

IMN logo majestic logo threadwatch logo seochat tools logo