linker error?

Started by
7 comments, last by Aardvajk 17 years, 6 months ago
Why cant I compile this? It comes up with a linker error(dev-c++): [Linker error] undefined reference to `WinMain@16' ld returned 1 exit status

//median.cpp
#include <algorithm>
#include <stdexcept>
#include <vector>

using std::domain_error;    using std::sort;    using std::vector;

//compute the median of a vector<double>
double median(vector<double> vec)
{
       typedef vector<double>::size_type vec_sz;
       
       vec_sz size = vec.size();
       if (size == 0)
          throw domain_error("median of an empty vector");
       
       sort(vec.begin(),vec.end());
       
       vec_sz mid = size/2;
       
       return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

Advertisement
You need a main function, a program entry point.

//median.cpp#include <algorithm>#include <stdexcept>#include <vector>using std::domain_error;    using std::sort;    using std::vector;//compute the median of a vector<double>double median(vector<double> vec){       typedef vector<double>::size_type vec_sz;              vec_sz size = vec.size();       if (size == 0)          throw domain_error("median of an empty vector");              sort(vec.begin(),vec.end());              vec_sz mid = size/2;              return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];}int main(){       DoSomeStuffHere();       return 0;}


Edit: In a larger application you would want to make vec a reference instead of a copy. vector<double> &vec
aah ok.. first time trying to splitt up my programs.. thanks
If this is some code that you want to use from somewhere else, and the "somewhere else" is where the main() function lives, then don't add one here. Instead, you need to tell Dev-C++ to compile each source file separately, and then use both resulting 'object files' when linking. With IDEs, this is usually as simple as just adding the source file to the same "project".

But you really, really, should read this if you haven't already.
I'd also just note that since you are getting WinMain as the unresolved symbol, your compiler is compiling as a Windows app rather than a console app.

I'm not sure if this makes any difference with Dev but with the compilers I'm used to you have to specifiy whether you are compiling a Windows or console app with a switch in order to make it try to link correctly to WinMain or main.

I suppose Dev may figure this out automatically. BCC55 and DMC don't.

[EDIT] Thinking about it, I don't see how it could figure this out since AFAIK in a Windows app it would be legal to have a normal function int main(int,const char**) that wasn't the entry point, and vice versa.

[Edited by - EasilyConfused on September 28, 2006 5:56:20 AM]
Quote:Edit: In a larger application you would want to make vec a reference instead of a copy. vector<double> &vec


Yeah.. but its an example in my book and it doesnt want the function to change the vector when its sorting it..


zahlman: jupp.. I've read it.. just didnt know that I needed a project in dev-c++ so it knew that all my source file where connected.. :)
Quote:Original post by Nanook
Quote:Edit: In a larger application you would want to make vec a reference instead of a copy. vector<double>; &vec


Yeah.. but its an example in my book and it doesnt want the function to change the vector when its sorting it..


Then it should be a const vector<double> &vec. Copying a vector can get expensive so is best avoided unless there is a reason to do so.

Do note then though that if you are using std::vector<double>::iterator (appreciate you aren't here, but you may well do so in the future), you need to change it to std::vector<double>::const_iterator.

As long as you are indeed not modifying the vector then, apart from the declaration, it won't affect the rest of the code using the iterator.
but would it be able to use the sort function if it is a const reference?
Oh yeah, of course not. Didn't think of that. Not sure what the correct way around that one would be. I suppose you have to have a copy in order to find the median without modifying the order of the original vector. Didn't look at the code properly. Sorry.

This topic is closed to new replies.

Advertisement