Multiple file programs, not defining functions correctly.

Hey all, I've been slacking on my c++ for the last year and a half, and I just cannot seem to instantiating or setting up my header file correctly. I keep getting the error redefinition of void stats::updateAvg and previously defined here in my methods.cpp and class.h files. I believe my problem is that I'm not making the class object correctly, or I'm not grabbing the data correctly. Any help would be greatly appreciated thanks!

P.S. my program is very simple I'm just not used to making them with multiple files!

P.S.S. Should I put my input function out of the class in the main driver function?

class.h file

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef CLASS_
#define CLASS_

class stats{

 private:

  double sum;
  double avg;
  double lastNum;
  double minNum;
  double maxNum;

  double temp;
  int totalNum;
  int n;

 public:

  stats();

  void input(){

  }

  void updateSum(double temp){

  }

  void updateLast(double temp){

  }

  void updateMin(double temp){

  }

  void updateMax(double temp){

  }

  void updateTotal(double temp){

  }

  void updateAvg(double temp){

  }


  // Declaring Methods
  double getSum()const;
  /*


   */

  double getAvg()const;
  /*



  */
  double getLast()const;
  /*


  */
  double getMin()const;
  /*


  */
  double getMax()const;
  /*


  */
  int getTotal()const;
  /*



  */

};
#endif



method.cpp file

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include "class.h"

using namespace std;


void stats::input(){
  //Checking to see if input is a number


}


void stats::updateSum(double temp){

 sum += temp;
}

void stats::updateLast(double temp){
  lastNum = temp;
}

void stats::updateMin(double temp){
  minNum = temp;
  if (temp < minNum)
    minNum = temp;

}

void stats::updateMax(double temp){

  stats.maxNum = stats.temp;

  if(temp > maxNum)
    maxNum = temp;
}

void stats::updateTotal(double temp){

  if (temp >= 0 || temp <= 0)
    totalNum++;
}

void stats::updateAvg(double temp){
  avg = sum/totalNum;
}



double stats::getSum() const{

  return sum;

}

double stats::getAvg() const{

  return avg;


}

double stats::getLast() const{

  return last;

}

double stats::getMin() const{

  return min;

}

double stats::getMax() const{

  return max;
}

int stats::getTotal() const{

  return total;

}
Last edited on
I keep getting the error redefinition of void stats::updateAvg and previously defined here in my methods.cpp and class.h files. I believe my problem is that I'm not making the class object correctly, or I'm not grabbing the data correctly.

No, you're trying to define the function in both your header file and your implementation file.

Look at these snippets:

From the header:
1
2
3
 void updateSum(double temp){

  }

This defines (implements) the member function updateSum(). The function isn't doing anything but it is defined.

From the source file:
1
2
3
4
void stats::updateSum(double temp){

 sum += temp;
}


Here you're redefining updateSum().

You can only define a function in one place.

To fix the problems you can just declare the function in the header instead of defining it:
void updateSum(double temp);

In class.h, you define a method that does nothing:

46
47
48
  void updateAvg(double temp){

  }


In method.cpp, you redefine exactly the same method, only this time, it does something:

44
45
46
void stats::updateAvg(double temp){
  avg = sum/totalNum;
}


You can't have two (or more) definitions for the same function; otherwise, how would the linker know which one to use?
Last edited on
I am supposed to have a header file, an implementation file and a driver file. So in my header file I should just do void updateSum(double temp); ?

I knew it was going to be something stupid. That's what I get for not keeping up on my c++ thanks guys appreciate it! I'll be sure to post on here if I have any more questions!
Yeah, that's right. It's the difference between declaring a method, and defining it.

You're welcome!
Last edited on
Topic archived. No new replies allowed.