Function Link Error

Im still trying to get the hang of linking functions and their constructors to other files. Im trying to link this header file to another cpp file, which i will eventually add to yet another cpp file. Can I get an explanation as to what I am doing wrong? Help is greatly appreciated. Thanks.


Header: Functions.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Header for Functions.cpp

#ifndef FUNCTIONS_h_INCLUDED
#define FUNCTIONS_h_INCLUDED

class Leveler
{
public:
	int levels(int& level);

private:
	double build;
	double constant;
	double determinant;
	double total;
	int exp[100];
};

#endif 



Source: Functions.cpp

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
#include <iostream>
#include <cmath>
#include <ctime>
#include "Functions.h"

using namespace std;

int main()
{
	int Leveler::levels(int& level)
	{
		cout << "\nTable\n\n";
		for(int i = 0; i <= 99; i++)
		{
			constant += 0.3;
			determinant = (i*(1+constant));
			build += (determinant/100);
			total += (build/8) * (1+constant);

			exp[i] = floor(exp[i] + 0.5);
            		cout << "Level " << i << ": " << exp[i] << "\n";
		}
	}

return 0;
}



In my .cpp im getting "Error: member function 'Leveler::levels' may not be redeclared outside its class." And just below that on the bracket im getting "Error: expected a ';'"

I've had similar problems, but i've actually been able to get around them.
Last edited on
You can't define a function inside another function. You're trying to define the function Leveler::levels inside the function main.
Last edited on
...lols -_-
How can I call/link my functions from the cpp file, Functions.cpp, to another cpp file? My header file, Functions.h, has the function declarations, where as Functions.cpp has the directions/contents. I could really use some help. Thanks.
I expect you're using some kind of IDE. You need to include Functions.cpp in your "project" or whatever your IDE calls it.
I'm using Microsoft Visual Studio 2010. And I have included Functions.cpp. I even tried including both Functions.h and Functions.cpp, then just Functions.h, and still there are errors.
Okay, I give up guessing. You're just going to have to tell us what the error is.
Last edited on
In Test.cpp I have Functions.cpp included.

Leveler::levelGain(playerExp, level); is what Im messing around with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <ctime>
#include "Functions.cpp"

using namespace std;

int main()
{

    int playerExp = 0, level = 0;

   cout << Leveler::levelGain(playerExp, level);

system("pause");
return 0;
}


That's what I have written in my test file for calling Leveler::levelGain(playerExp, level);

In Functions.cpp Leveler::levelGain is:

1
2
3
4
5
6
7
8
9
10
11
12
int Leveler::levelGain(int playerExp, int level) //EXPERIENCE TABLE
{
    if(playerExp >= exp[level - 1]) //exp[] to be added later
    {
          level++;
          return level;
    }
    else
    {
          return level;
    }
}
Last edited on
cout << Leveler::levelGain(playerExp, level);

This demonstrates a fundamental misunderstanding of what a class is. The function levelGain is a member function of the class Leveler. You must first make an object of type Leveler, and then you can call its functions:

1
2
Leveler anObject;
cout << anObject.levelGain(playerExp, level);
Like I stated above, I'm still pretty new to this. The only thing related to classes that I've worked with are structs, and even then the work dealing with that in my first programming course was quite tedious.

Thanks again for the tips on how to do this. One last thing I would like to ask; would this stuff I'm working on now be considered "object oriented" and more directed toward C#? Thanks again.

Edit: Dont get me wrong, im not trying to confuse words. I've just recently heard about object oriented programming and wanted to find out if this met the match.
Last edited on
In C++ a struct and a class are identical, except for default member visibility (public vs. private).

"Object oriented" is one of a number of ambiguously defined terms, freuqently used as a poorly-defined buzzword. You can code in C++ in an "Object oriented" way, and you can code in C# in an "Object oriented" way.
Thanks for clearing that up. I never understood why people used that reference when it seems as though C++ is just as capable and applicable in definition and application. However, I have yet to use C#; im sure there is some sort of difference.

Nevertheless, thanks for your help.
Topic archived. No new replies allowed.