Errors in classes

Hi all,

I'm trying to compile this program, but I'm getting errors;


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
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>
#include <cstdio>


class weight{
  private:
   double w;

  public:
   void setKilograms(double kg);
   void getStoneAndPounds(double stone, double pounds);
};

void :: setKilograms (double kg, double g)
{
     kg = g * 1000;
}

void :: getStoneAndPounds (double stone, double pounds)
{
     stone = pounds * 16;
     pounds = stone / 16;
}

int main(void)
{
weight w;
w.setKilograms(77.5);
double stone,pounds;
w.getStoneAndPounds(stone,pounds);
printf("Someone who weighs 77.5kg weighs %.2f stone and %.2f pounds\n",stone,pounds);

return 0;
}



and the errors;

[Linker error] undefined reference to `weight::setKilograms(double)'
[Linker error] undefined reference to `weight::getStoneAndPounds(double, double)'
ld returned 1 exit status
C:\Dev-Cpp\Makefile.win [Build Error] [classes.exe] Error 1


Any help would be appreciated.
you need to put the class name before the scope operator if you want to implement a class method:
20
21
22
void weight :: getStoneAndPounds ( double stone, double pounds)
{
 //... 

And your setKilograms looks a bit weird ( and doesn't match the class method with the same name )
Last edited on
"undefined reference to X" means you never gave 'X' a body.

In this case, you are declaring your function bodies incorrectly:

void :: setKilograms (double kg, double g)

This is defining a global function named setKilograms. It does not define the setKilograms that belongs to your weight class. Change it to this:

void weight::setKilograms(double g)

You have a similar problem getStoneAndPounds.


The logic for your functions is also wrong, but start with the above.

EDIT: ninja'd by Bazzy >=o
Last edited on
Ah, not entirely sure how I missed the class name out :P but thank you



But if I take the "kg" out then I can't use "kg" in the scope of void :: setKilograms

Yeah I know the logic's out - I have no idea how to write this AT ALL!
Basically this is a practice exam and we're given just the end bit:
1
2
3
4
5
6
7
8
9
10
int main(void)
{
weight w;
w.setKilograms(77.5);
double stone,pounds;
w.getStoneAndPounds(stone,pounds);
printf("Someone who weighs 77.5kg weighs %.2f stone and %.2f pounds\n",stone,pounds);

return 0;
}

to work with.
Topic archived. No new replies allowed.