• Log in or sign up for an account here: https://www.sythe.org/login

My Own Function Error...

Esuom

Guru
Banned
Joined
May 3, 2007
Sythe Gold
0
Messages
1,731
Location
EST Time Zone.
My Own Function Error...
Code:
#include <iostream>
#include <iomanip>

using namespace std;

string A, B, C, D, ans;
int num;


int question(int num, string A, string B, string C, string D, int ans);
int main()
{
    question(1, "Test A", "Test B", "Test C, correct", "Test D", 3);
    system("pause");
    return 0;
}

int question(){
    cout << num << endl;
    cout << A << endl;
    cout << B << endl;
    cout << C << endl;
    cout << D << endl;
    cout << ans << endl;
}


I dont know what I am doing wrong with that code. When I compile, I end up with...

Code:
  [Linker error] undefined reference to `question(int, std::string, std::string, std::string, std::string, int)'
 
My Own Function Error...
Well in your main you call the function
Code:
question(1, "Test A", "Test B", "Test C, correct", "Test D", 3);

Since it compiles line by line when it gets to that point in the file it's found the declaration of that function which is at the top but there is no matching definition since your question() function definition doesn't have any parameters.

For the declaration and definition to be associated with each other you have to match their function name as well as the number of parameters, type of each parameter, and order the parameter types come in. You are permitted to give the parameters different names in the declaration and definition or even to completely omit the paramaters' names in the declaration though it's generally not recommended since it makes it harder to understand.

What you want is
Code:
int question(int num, string A, string B, string C, string D, int ans){
    cout << num << endl;
    cout << A << endl;
    cout << B << endl;
    cout << C << endl;
    cout << D << endl;
    cout << ans << endl;
}

Instead of using the windows-specific system("pause") try and get used to using getchar() in pausing your programs as it's part of the standard.

EDIT:
You also don't need to declare the variables at the top since they are never used. To make it more obvious that they are never used you accidently declared ans as a string while the other ones were all integers. You can remove the following anyway.
Code:
string A, B, C, D, ans;
int num;
 
My Own Function Error...
Okay, that helped, I now have a really nice looking final project...although expect more help needed from me...I used namespace std but my teacher doesnt want me using it because "it isn't in the book". *sigh* That means I am going to have to go back and edit the whole thing...
 
Runescape Gamblingrunescape gambling
Back
Top