0

I'm getting some crazy errors while trying to use a function to find the median of a vector. I'm not exactly sure what's causing these errors.

  1. 'Error 1 error C2601: 'calcMed' : local function definitions are illegal'

The function I'm trying to use is declared as

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std; 

double calcMed(int, vector<int>& studentScores);

//

double homeworkMed = calcMed(vectorSize, studentScores);

double calcMed(int vectorSize, vector<int>& studentScores)
{
 double median;

  sort(studentScores.begin(), studentScores.end());

  median = studentScores[vectorSize / 2];

  return median;
}

I know that it wouldn't work if I had an odd vector but I just want to get this first one working. Any help would be appreciated.

Removed the semicolon on "double calcMed(int vectorSize, vector& studentScores)" and now I'm getting two different errors, both are on the line right after the function name.

Both of the errors occur on the line with the {

double calcMed(int vectorSize, vector<int>& studentScores)
{
user2781666
  • 167
  • 1
  • 2
  • 11
  • Are you sure there isn't a `calcAverage` involved? Give us a [minimal complete example](http://sscce.org), and I'll bet the problem will be obvious. – Beta Jan 19 '14 at 22:00
  • How does that compile unless you're trying to define it in `main`? – chris Jan 19 '14 at 22:00
  • @chris it doesn't. CNR. =P – WhozCraig Jan 19 '14 at 22:02
  • "Getting errors while running a function" - no, not quite. This is not a runtime error. This is a compilation error. You should at least have some idea as to what you are even doing. –  Jan 19 '14 at 22:12
  • @H2CO3 this is my first time using functions on C++, sorry for using incorrect words, I just know that if I comment out my function then my code runs without any errors, so I thought it was having issues running the function. – user2781666 Jan 19 '14 at 22:15
  • @user2781666 If you are just starting out with a language, and learning by trial and error, then it's inevitable to **do your own research.** These are basic (khm, ugh, trivial...) errors and solutions, and you should have googled the error messages instead of dumping them on Stack Overflow. To reiterate, Stack Overflow is not a website where we teach you a language -- you're expected to learn the basics yourself (there's [this excellent list of C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) around here, too, which may help you in the process). –  Jan 19 '14 at 22:17

3 Answers3

2
double calcMed(int vectorSize, vector<int>& studentScores);
{
  double median;

  sort(studentScores.begin(), studentScores.end());

  median = studentScores[vectorSize / 2];

  return median;
}

There shouldn't be a semicolon after the function signature in the definition. Change the first line to double calcMed(int vectorSize, vector<int>& StudentScores)

Tyler Gaona
  • 479
  • 1
  • 4
  • 14
  • Thanks, I removed that and got two more errors on the { on the next line. I posted the new errors in the question – user2781666 Jan 19 '14 at 22:07
  • You can't define `calcMed` within `main()`. It should be declared and defined outside of `main()`. – Tyler Gaona Jan 19 '14 at 22:09
  • I moved if before the 'main()' thanks. I still have the same errors though – user2781666 Jan 19 '14 at 22:10
  • 1
    Could you post the entirety of your code? – Tyler Gaona Jan 19 '14 at 22:12
  • I'm hesitant to do that since it would make the question a lot more cluttered – user2781666 Jan 19 '14 at 22:18
  • In this case I believe it is the only way we will be able to help you fix the problem, so I encourage you to do it. I say this assuming your source isn't very long. – Tyler Gaona Jan 19 '14 at 22:20
  • The total code is around 120 lines. I moved the function out of main() I had no idea why I put it inside, and now am down to just the one error – user2781666 Jan 19 '14 at 22:23
  • Please post it in its entirety. – Tyler Gaona Jan 19 '14 at 22:24
  • @user2781666: You should not have written so much code before testing it. – Beta Jan 20 '14 at 01:33
  • @Beta How would you recommend writing code then? I'm mostly curious how you do it, I mostly start in the beginning (after making a plan in my head or in pseudo code) and then I work from the start to finish. This function takes a result that I go through several different loops to get all of the information for the code. Would you recommend doing a function, or at least setting it up, or something similar first then? – user2781666 Jan 21 '14 at 02:23
  • 1
    I would start with something small small and simple, like `HelloWorld`, *test it*, then add a function that returns 0, *test it*, then edit it to take a vector as an argument, *test it*, then edit it to print the vector, *test it*, then to sort it before printing it, *test it*, then to print the index it thinks is the middle one, *test it*, then to return the median, and finally remove the printout statements and *test it*, and above all I would **never add to code that doesn't work**. If you write from start to finish, it won't work at all until the end, so you can't test it, so it'll fail. – Beta Jan 21 '14 at 03:31
0

The linker error is saying that it can't find an implementation of calcAverage(). Did you possibly rename calcAverage() to calcMed() and not update any calls to this function?

To fix this error, I would search all your code for calcAverage(), and either rename them to calcMed() or provide an implementation for it.

Andy
  • 30,088
  • 6
  • 78
  • 89
0

Sorry, I figured it out. For some reason I put the function inside my main(). After I moved it out of the brackets it solved the error. Thanks for everyone's help.

user2781666
  • 167
  • 1
  • 2
  • 11