Why won't this simple code build?

Just a simple homework for class:

main.cpp---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

#include "averager.h"

using namespace std;

int main ()
{
  Averager my_avg(0, 0);

  my_avg.add(33);

  cout << "avg " << my_avg.getAverage();

  return 0;
}


averager.cpp --
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>

#include "averager.h"

using namespace std;

double Averager::getAverage ()
{
    try {
        if (count == 0) throw 1;
        return (double) total/count;
    }
    catch (int exception) {
        cerr << "no number." << endl;
        exit(1);
    }
}

void Averager::add (int a) {
  total += a;
  count++;
}

Averager::Averager (int a, int b) {
  total = a;
  count = b;
}


averager.h---
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef AVERAGER_H
#define AVERAGER_H

class Averager {
    int total, count;
  public:
    void add(int);
    Averager (int,int);
    double getAverage ();
};

#endif 



built in BodeBlocks on Ubuntu. Thanks. I am told that there is "undefined reference" to all three functions.
Last edited on
the code look ok.

Can you give the exact error messages?
"undefined reference" means that the compiler is happy, but the linker has gone looking for the compilerd object code that is the actual functions (i.e. what averager.cpp is turned into by the compiler) and cannot find it.

This means that either you are not compiling averager.cpp, or you are compiling it but you have not told your linker to link to it. In Code::Blocks, I expect you have to make sure that averager.cpp is in your "project" or whatever it calls it that abstracts away from you the mechanics of what's happening to your code.

It is really, really, really in your interests to take a few minutes to learn what the compiler does, what the linker does, and how they work together to make a library or executable. If you had known already, you wouldn't be trying to fix a linking error by looking at your code. The time saving you will get by understanding what your tools do is really, really worth it.
Last edited on
Hello DCoates,

I got your code to compile. But I had to change the getAverage() function to this:
1
2
3
4
5
6
7
8
9
10
11
double Averager::getAverage ()
{
    try {
        if (count == 0) throw 1;
        return (double) total/count;
    }
    catch (int exc) {
        cerr << "exception no number. " << exc <<endl;
        exit (1);
    }
}


"exception" was what it was getting stuck on, just change it to "exc" so its name wont conflict with however "exception" might be defined in your libraries.

you also need to add #include <iostream> to the averager.cpp file becuase you are trying to write some output to std error stream.
Last edited on
Thanks a lot for the responses! Both are very helpful. I am curious, though, what was causing the confusion with the other functions? I do have a lot to learn, this is for sure, haha. The #include was there, I'm not sure why it wasn't copied.
what was causing the confusion with the other functions?


See my answer above? That applied to every function for which the linker complained "undefined reference".

You didn't link against the compiled averager.cpp object code, so all the function in that were undefined as far as the linker was concerned.

The "undefined reference" isn't in the functions; the functions themselves didn't exist because you didn't link properly.
Last edited on
I completely forgot your response. Been a couple of days since I've last slept.
Topic archived. No new replies allowed.