Error : undefined reference to

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    Error : undefined reference to

    im getting an error and I dont know why can anyone help.

    this is the error......

    Code:
    /tmp/ccxaenRY.o(.text+0x375): In function `main':
    : undefined reference to `average_quizes(records, float&)'
    collect2: ld returned 1 exit status



    [CODE=cpp]#include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    using namespace std;

    ifstream in_file;
    //class that holds student record information.
    class records{
    private:
    string name;
    float quizes[7];
    float projects[6];
    float exams[2];
    float labs[14];
    public:
    void set_name(ifstre am& in_file);
    void set_quizes(ifst ream& in_file);
    void set_projects(if stream& in_file);
    void set_exams(ifstr eam& in_file);
    void set_labs(ifstre am& in_file);
    float get_quizes(int val);
    //float get_projects(in t val);
    //float get_exams(int val);
    //float get_labs(int val);


    };


    void average_quizes( records,float&) ;

    int main()
    {


    cout.width(10);
    cout << "No." << " " << "Name" <<" " << " " << " Quiz" << " " << "Project" << " " << "Exam" << " " << "Lab" << " " << "Total" << " " << "Grade" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout << endl;



    records grades;
    ifstream in_file;// Define a file input stream and open the file
    in_file.open("r ecord.txt"); //opening file

    if (in_file.fail() )
    {
    // Failed to open the file (file doesn't exist or isn't readable)
    cout << "Could not open file: "<< "\n";
    exit(1);
    }


    int counter = 1;
    float quiz_average = 0;

    while(in_file)
    {
    average_quizes( grades,quiz_ave rage);
    cout << quiz_average;
    cout << counter;
    }

    return 0;

    }
    void records::set_na me(ifstream& in_file)
    {
    in_file >> name;
    }
    void records::set_qu izes(ifstream& in_file)
    {

    for (int i = 0; i < 7; i++)
    {
    in_file >> quizes[i];
    }
    }
    void records::set_pr ojects(ifstream & in_file)
    {
    for (int i = 0; i < 6; i++)
    {
    in_file >> projects[i];
    }
    }
    void records::set_ex ams(ifstream& in_file)
    {
    for (int i = 0; i < 2; i++)
    {
    in_file >> exams[i];
    }
    }
    void records::set_la bs(ifstream& in_file)
    {
    for (int i = 0; i < 14; i++)
    {
    in_file >> labs[i];
    }
    }
    //accessing quiz values.
    float records::get_qu izes(int val)
    {

    if(val < 7)
    return quizes[val];
    else
    return quizes[0];

    }
    //printing out quizes.
    void average_quizes( records* obj,float& quiz_average)
    {
    for (int i = 0; i < 7; i++)
    {
    obj-> get_quizes(i);
    quiz_average = i + quiz_average;
    quiz_average = (quiz_average/7);
    }
    }[/CODE]
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by gdarian216
    im getting an error and I dont know why can anyone help.

    this is the error......

    Code:
    /tmp/ccxaenRY.o(.text+0x375): In function `main':
    : undefined reference to `average_quizes(records, float&)'
    collect2: ld returned 1 exit status



    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    ifstream in_file;
    //class that holds student record information.
    class records{
         private:
            string name;
            float quizes[7];
            float projects[6];
            float exams[2];
            float labs[14];
         public:
            void set_name(ifstream& in_file);
            void set_quizes(ifstream& in_file);
            void set_projects(ifstream& in_file);
            void set_exams(ifstream& in_file);
            void set_labs(ifstream& in_file);
            float get_quizes(int val);
            //float get_projects(int val);
            //float get_exams(int val);
            //float get_labs(int val);
    
    
    };
    
    
    void average_quizes(records,float&);
    
    int main()
    {
    
    
        cout.width(10);
        cout << "No." << "  " << "Name" <<"  " << "  " << " Quiz" << "  " << "Project" << "  " << "Exam" << "  " << "Lab" << "  " << "Total" << "  " << "Grade" << endl;
        cout << "---------------------------------------------------------------"         << endl;
        cout << endl;
    
    
    
            records grades;
            ifstream in_file;// Define a file input stream and open the file
            in_file.open("record.txt"); //opening file
    
            if (in_file.fail())
            {
            // Failed to open the file (file doesn't exist or isn't readable)
            cout << "Could not open file: "<< "\n";
            exit(1);
            }
    
    
            int counter = 1;
            float quiz_average = 0;
    
            while(in_file)
            {
            average_quizes(grades,quiz_average);
            cout << quiz_average;
            cout << counter;
            }
    
            return 0;
    
    }
    void records::set_name(ifstream& in_file)
    {
            in_file >> name;
    }
    void records::set_quizes(ifstream& in_file)
    {
    
            for (int i = 0; i < 7; i++)
            {
                    in_file >> quizes[i];
            }
    }
    void records::set_projects(ifstream& in_file)
    {
            for (int i = 0; i < 6; i++)
            {
                    in_file >> projects[i];
            }
    }
    void records::set_exams(ifstream& in_file)
    {
            for (int i = 0; i < 2; i++)
            {
                    in_file >> exams[i];
            }
    }
    void records::set_labs(ifstream& in_file)
    {
            for (int i = 0; i < 14; i++)
            {
                    in_file >> labs[i];
            }
    }
    //accessing quiz values.
    float records::get_quizes(int val)
    {
    
            if(val < 7)
            return quizes[val];
            else
            return quizes[0];
    
    }
    //printing out quizes.
    void average_quizes(records* obj,float& quiz_average)
    {
            for (int i = 0; i < 7; i++)
            {
                obj-> get_quizes(i);
                quiz_average = i + quiz_average;
                quiz_average = (quiz_average/7);
            }
    }
    I think it is because your prototype is different from your declareation:
    Code:
    void average_quizes(records,float&);
    void average_quizes(records* obj,float& quiz_average)
    You ask for a pointer in one and an actual object in the other.
    You can overload a function by the type of arguements are passed to it. So the compiler sees the function, but doesn't see the actual body. The one you gave it is not looked for because it takes a pointer. Try changing one of them and see what happens.

    Comment

    • gdarian216
      New Member
      • Oct 2006
      • 57

      #3
      i changed that and now I got this


      test.cpp: In function `int main()':
      test.cpp:63: error: cannot convert `records' to `records*' for argument `1' to `void average_quizes( records*, float&)'

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by gdarian216
        i changed that and now I got this


        test.cpp: In function `int main()':
        test.cpp:63: error: cannot convert `records' to `records*' for argument `1' to `void average_quizes( records*, float&)'
        Ok, that means you are passing a "records" when you should pass a pointer to one. You can either pass a pointer or change the function to take an actual object instead of a pointer.

        Comment

        • gdarian216
          New Member
          • Oct 2006
          • 57

          #5
          I thought I was passing a pointer. What did I do wrong

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by gdarian216
            I thought I was passing a pointer. What did I do wrong
            You weren't passing a pointer. Add an ampersand in front of the variable or make it a pointer. Do you know how to do that?

            Comment

            • gdarian216
              New Member
              • Oct 2006
              • 57

              #7
              do u mean something like this

              void get_score(recor ds&,float&)

              but that would pass it by reference.

              how would I make it a pointer?

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by gdarian216
                do u mean something like this

                void get_score(recor ds&,float&)

                but that would pass it by reference.

                how would I make it a pointer?
                No, no. The way you are calling the function is the problem. You are passing in an object of "records" type. You need a pointer to the location of that object. To do that add an ampersand in front of the variable grades in your function call.
                Like this:
                Code:
                average_quizes(&grades, quiz_average);
                Post here if you are having problems.

                Comment

                Working...