开发者

I get an "undefined reference to ..." for two of my functions

I have created three separate files. The first is the main.cpp the 2nd is a header file called "statistics.h" which has the declarations for the two functions i am getting the error and the 3nd is a file called statistics.cpp which holds the implementation of the two functions.

Here is my main file:

#include <iostream>
#include "statistics.h"

using namespace std;

int main()
{
   cout<<"This program provides the average and the standard deviation of 1,2,3 or 4 numbers."<< endl;
   while(true){
   start:
   unsigned int howmany;

   cout<<endl;
   cout<<"How many numbers do u wish to receive as input??  ";
   cin >> howmany;

   if (howmany>4){
   cout<<"You should pick at most 4 numbers u idiot!!!"<< endl<<endl;
   goto start;
   }
   if(howmany==0){
   return 0;


   }
   cout<< endl;
   double nums[howmany];



   for (int i=0;i<howmany;i++){
   cout<<"Give me the number "<<i+1<<":";
   cin>>nums[i];

   }


   double avg=average(nums,howmany);     /////////////////////////////
   double stdev=standard_deviation(nums,howmany,avg);   ////////////////////

   cout<< endl<<"Average: "<<avg<<". Standard Deviation: "<< stdev<< endl;

   }




}

My header file is:

#ifndef STATISTICS_H_INCLUDED
#define STATISTICS_H_INCLUDED





double average(double ar[],int hm);
double standard_deviation(double ar[],int hm,double avrg);


#endif // STATISTICS_H_INCLUDED

And my implementation file statistics.cpp is:

#include<iostream>
#include"statistics.h"
#include <math.h>

using namespace std;


double average(double ar[],int hm){


double sum=0.0;
double average;

for(int i=0;i<hm;i++){

sum+=ar[i];

}

average=sum/hm;

return average;

}


double standard_deviation(double ar[],int hm,double avrg){

    double std_dev;
    double sum=0;
    double ans;

    for(int i=0;i<hm;i++){

    sum+= ((ar[i]-avrg)*(ar[i]-avrg));

    }

    ans=sqrt(sum/hm);
    return ans;


}

I am getting the errors in my main file (i have marked the corresponding lines with consecutive ////////). What could be wrong?? I am sure its something stupid from my part.


As i am开发者_如何转开发 using codeblocks i finally found the solution. I just needed to add #include "statistics.cpp" to my main.cpp file.


You forgot to link statistics.o together with main.o, so your hypothetical executable will not contain the function definitions.

(The hypothetical executable can thus not exist, so you're getting a linker error.)


Variation 1

Where you're writing something like:

g++ main.cpp -o myExecutable

instead write:

g++ main.cpp statistics.cpp -o myExecutable

Variation 2

Where you're writing something like:

g++ -c main.cpp
g++ main.o -o myExecutable

instead write:

g++ -c main.cpp
g++ -c statistics.cpp
g++ main.o statistics.o -o myExecutable

If you're still having trouble, try reversing the order in which you provide the filenames to g++ (though the above should be correct).


Incidentally, you are not allowed to define an array with variable dimensions. Use a std::vector instead.


As i am using codeblocks i finally found the solution. I just needed to add #include "statistics.cpp" to my main.cpp file.

!!! This is NOT a solution. Do not include implementation files. You will fall into a pit of despair, decay and disorganization.

Everything was fine with your use of header files. You just need to tell your IDE to compile both files as a single project. Consult the documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜