Adblock breaks this site

My Own Function Error...

Discussion in 'Programming General' started by Esuom, Mar 29, 2009.

  1. Esuom

    Esuom Guru
    Banned

    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0
    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)' 
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    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;
    
     
  3. Esuom

    Esuom Guru
    Banned

    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0
    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...
     
< New Stealrs? | What programming language should I start with? >


 
 
Adblock breaks this site