Unresolved externals?

Pages: 12
Jul 13, 2012 at 2:46am
This code is a work in progress, it's nowhere near finished...but I'm getting these unfamiliar errors when attempting to compile it.error LNK1120 and error LNK2019. What the heck is going on here?
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
27
28
29
30
31
32
33
34
35


#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void getinput ();
void average (float num);
void average12 (float num);
void sum3rd (float num);
void range (float num);
void closest200 (float num);
void average2lines (float num);
void sumlessthan100 (float num);


ifstream infile;
ofstream outfile;

int main()
{
cout<<getinput<<endl;
}

void getinput ()
{
int num;
infile.open("E:\\CFiles\\DataFile2.txt");
     while (infile)
     {
          infile>>num; 
     }
}
Jul 13, 2012 at 2:51am
99 times out of 100, "unresolved external symbol" means you are trying to call a function that doesn't have a body. The name of the function should be in the error message.

Also, this is unrelated to your error, but line 24 is all wrong. getinput doesn't return anything (it's a void), so how can you print it? Also, you need to give it parenthesis if you want to call the function (ie getinput())
Jul 13, 2012 at 2:53am
When you prototype functions (like you have on lines 10 through 16) you are telling the compiler that they exist somewhere.

The error you are getting is the compiler's polite way of saying that it doesn't believe you.

Notice how the compiler didn't give you grief about line 9, since it was able to find the function on lines 27 through 35.

Hope this helps.
Jul 13, 2012 at 6:05pm
So in other words I can't run this program until I've coded all 8 functions?
Jul 13, 2012 at 6:17pm
It's only a problem if you try to use one that you haven't written yet. The error messages should tell you which functions it can't find (unresolved external). Which functions can't it find?
Jul 13, 2012 at 6:25pm
I've eliminated all but the first function from the code and these are the errors I'm getting:

Error 2 error LNK1120: 1 unresolved externals \visual studio 2010\Projects\8A\Debug\8A.exe 1 1 8A

Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\\documents\visual studio 2010\Projects\8A\8A\MSVCRTD.lib(crtexew.obj) 8A

Jul 13, 2012 at 6:27pm
Here's the code I'm attempting to run

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
27
28
29
30
31
32
33
34
35
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void getinput (int num);
int num;


ifstream infile;
ofstream outfile;

int main()
{
cout<<getinput<<endl;
}

void getinput (int num)
{
	
	
	infile.open("E:\\CFiles\\DataFile2.txt");
     while (infile)
     {
          infile>>num; 
     }
}






Jul 13, 2012 at 6:32pm
Jul 13, 2012 at 6:36pm
So I'm trying to run the wrong program type???

FFFFFFFFFF
Jul 13, 2012 at 6:41pm
The joy of learning to program using a huge IDE that you don't understand. You have to learn to program AND learn C++ AND learn how to use the IDE all at the same time.
Jul 13, 2012 at 6:44pm
Hell I didn't even know what IDE was until I just googled it. :)
Jul 13, 2012 at 6:51pm
"cout <<" means output something, for example a string like "Hello", in this case to the console. You cant output a function in that way, You have to call the function. Like this

int main()
{
getinput();
}

Also main does not return anything, Set its type to void or return with 0.

Wish you best luck!
Jul 13, 2012 at 7:01pm
Thanks for the tip! For the first function I'm just wanting to output what's on the file.

So I need to call the function in main, and then output from main?
Jul 13, 2012 at 7:04pm
I believe what you wanted to do here is take this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
cout<<getinput<<endl;
}

void getinput (int num)
{
	
	
	infile.open("E:\\CFiles\\DataFile2.txt");
     while (infile)
     {
          infile>>num; 
     }
}


And turn it into this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
      getinput();
      return 0;
}

void getinput ()
{
     infile.open("E:\\CFiles\\DataFile2.txt");
     while (infile)
     {
          infile>>num;
          cout << num;
     }
}

Jul 13, 2012 at 7:07pm
Thats now quite how it works, Read on functions again, You can only output a function if it returns something. And your function returns void. So this is impossible.

http://www.cprogramming.com/tutorial/lesson4.html This is a decent tutorial

EDIT: I saw that you got a good answer, But i still would suggest you to read my link and not move on in your program before you understand it completely!
Last edited on Jul 13, 2012 at 7:10pm
Jul 13, 2012 at 7:20pm
Yeah....that's exactly what I was trying to do. Thanks!

This is what gives me the hardest time with this stuff. Understanding why your code works and mine didn't. The syntax and logic on this stuff gives me nightmares.
Last edited on Jul 13, 2012 at 7:20pm
Jul 14, 2012 at 1:36am
It's obvious I still don't fully grasp this function stuff...I tried writing the code for the second function, and while it compiles, it doesn't output anything. I know something is not quite right with the code, but I'm not sure what. I know the problem must be in lines 8,23, 24, or 42....or maybe I don't need to open the file again?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void getinput ();
void average ();
void average12 (float num);
void sum3rd (float num);
void range (float num);
void closest200 (float num);
void average2lines (float num);
void sumlessthan100 (float num);

ifstream infile;
ofstream outfile;

int main()
{
      getinput();
      return 0;
	  average ();
	  return 0;
}

void getinput ()
{
    int num; 
	infile.open("E:\\CFiles\\DataFile2.txt");
	 cout<<"The numbers on the file are ";
     while (infile)
     {
         
		 infile>>num;
          cout<<num<<" ";
		 
		  
     }
}

void average()
{
	int count=0;
	int num;
	int sum=0;
	float solution;
	infile.open("E:\\CFiles\\DataFile2.txt");

	while (infile)
	{
		infile>>num;
	}

	while (count>0)
	{
		infile>>num;
		sum=sum+num;
		count++;
		solution=sum/count;
	}

	cout<<setprecision(5)<<"The average of the numbers is  "<<solution<<endl;
}
Last edited on Jul 14, 2012 at 2:41pm
Jul 14, 2012 at 5:05am
It's because once main returns a value, the program ends. The function looks like it should work. Also, I know C++ ignores whitespace, but having all of those extra lines at the end of your program is not very nice -.-
Jul 14, 2012 at 2:45pm
Sorry about that. Fixed it to get rid of all the empty space. ::embarassed::

So in your opinion the second function should work? It's compiling but not giving me output.

The only output I get is from the first function,
The numbers on the file are 55, etc....
Jul 14, 2012 at 5:04pm
It's because once main returns a value, the program ends.
1
2
3
4
5
6
7
8
9
10
11
int main()
{
      getinput();
      // main function ends once it returns
      // omit this line
      return 0;
      // Everything after this doesn't get run
      average ();
      // But leave this
      return 0;
}
Pages: 12