Sign in to follow this  

linker error?

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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];
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this