Good afternoon, i'm getting an error when I compile

"undefined reference to 'TestScores:theAverage(int *,int)"

I'm not sure what the compiler is telling me.

Any assistance is welcomed.

Art

header

// this is the header file for class TestScores
#ifndef TESTSCORES_H
#define TESTSCORES_H
//this idea with this class is to take an array, look at each element
//if element > 100 or element < 0, to throw an error back to the program
//if neither of these conditions is met then, the average is take and returned
//to the calling program

using namespace std;

const int ARRAYSIZE = 5;  //constant for arraysize

class TestScores
{
      private:
              int holdelement;
              double average; 


      public:
             double theAverage(int[],int);
             double getAverage()
             {return average;}

             class NegativeNumber //throw class for neg
                 {
                   private:
                          int value;
                   public:
                          NegativeNumber(int val)
                          {value = val;}

                          double getValue() const
                          {return value;}
                   };

             class LargerThan //throw class for more than 100
                   {
                   private:
                           int value;
                   public:
                          LargerThan(int val)
                          {value = val;}

                          double getValue() const
                          {return value;}
                   };


};
#endif

cpp

//This is the cpp file for TestScores
#include "TestScores.h"

double TestScores::theAverage(int scores[],int size)
{
     for(int count=0;count < size;count++)
     {
             if(scores[count]<0)//check for element < 0
             {
                throw NegativeNumber(scores[count]);//throw error if < 0
                //this uses the value in the array and returns it to main
                }
             else if (scores[count] > 100)//check for element > 0
             {
                throw LargerThan(scores[count]);//throw error if > 0
                //this uses the value in the array and returns it to main
                }
             else
             {
                 holdelement += scores[count];//no error add it to holdval
                 average = (holdelement / 5.0);//keep a running average of holdval
                 }
                 return average;
     }
}

main

//This is the main file for TestScores
#include <cstdlib>
#include <iostream>
#include "TestScores.h"


using namespace std;

int main(int argc, char *argv[])
{
    int testscores[ARRAYSIZE];//set the size of the array
    //Enter 5 integers
    TestScores maintestscores;//define the object

    cout << "Enter 5 integers: ";
    cin >> testscores[0];
    cin >> testscores[1];
    cin >> testscores[2];
    cin >> testscores[3];
    cin >> testscores[4];
    try
    {
            maintestscores.theAverage(testscores,ARRAYSIZE);//pass in the array
            cout << "The average score is: "<< maintestscores.getAverage();
    }
    catch (TestScores::NegativeNumber e)
    {
          cout << "Error: " << e.getValue() << "is less than zero \n";
    }
    catch (TestScores::LargerThan e)
    {
          cout << "Error: " <<e.getValue() << "is greater than 100 \n";
    }
    cout << "End of program: ";

    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 6 Replies

What are you using to build your program? Visual Studio? GCC?

I'm using Dev C++

Art

This should work. Your function's definition matches your function's declaration. Your function's call matches both.

Dev C++ causes problems and has compatability issues. Basically, it sucks and doesn't work well with established standards. You should consider getting a new IDE and compiler toolchain. I've heard that Code::Blocks is a nice alternative to Dev C++

Thanks for the suggestion,

Still I think there might be something wrong. Have you tried to compile it to see if you get a similar error?


Art

It does compile fine on my system using gcc. So, again, I think the problem is Dev c++.

I did discover a logic problem in your code however

double TestScores::theAverage(int scores[],int size)
{
     for(int count=0;count < size;count++)
     {
             if(scores[count]<0)//check for element < 0
             {
                throw NegativeNumber(scores[count]);//throw error if < 0
                //this uses the value in the array and returns it to main
             }
             else if (scores[count] > 100)//check for element > 0
             {
                throw LargerThan(scores[count]);//throw error if > 0
                //this uses the value in the array and returns it to main
             }
             else
             {
                 holdelement += scores[count];//no error add it to holdval
                 average = (holdelement / 5.0);//keep a running average of holdval
             }
             // Inside the for loop
             return average; // <------------PROBLEM!!!
     }
     // Outside the for loop
}

You can see here that your function is returning after only the first iteration of the loop. You need to move your return statement outside of the for loop.

I'm not sure that your running average is behaving correctly either. What is the constant of 5.0? Why not just compute the average after all of the elements have been examined and right before the function returns?

Thanks,

I looked at the return and did move earlier to see if this was creating the problem. This was after I posted this code.

The purpose of the statement is just to get a total of the average. It never gets incremented just recalculated, if all elements pass, then it should give the sum of the elements which can then be divided and returned as a real.

I'm going to try the program you suggest.

Art

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.