Hard Error

Hi, I have the following error:
1
2
3
4
1
1>  Extra Mark Project.cpp
1>Extra Mark Project.obj : error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * line" (?line@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
1>D:\C++ Projects\Extra Bonus Project\Debug\Extra Bonus Project.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


and here is the function that the program does not like:
1
2
3
4
5
6
7
8
9
10
11
12
void lines(string a[],string line[]){
	
   
	int counter=a[wordchoice].length();
	for(int i=0;i<counter;i++){
		//cout<<"_ ";
		line[i]='_';
	}
		for(int i=0;i<counter;i++){
		cout<<line[i]<<" ";
	}
}


what is wrong?!?!?!?!
This is so random becuase if i do not use the array line, it works perfectly. (btw line[] is global and type string, wordchoice is also global]
first of all globals are bad to use most of the time and secondly if they are global why are you using them like that in the function? wouldn't it be
1
2
3
void lines( void )
{
}
instead?
Also you never actually told us what wordchoice is? but anycase it should be a.length(); unless it is an array of arrays like a[2][2]; a[0].size(); a[1].size();
(size and length are the same I am pretty sure I never had a different result)
Last edited on
How did you define line. Is it static? A static class member maybe? One of your cpp files uses it, but never seen it defined or declared.
Last edited on
Looks like your problem is elsewhere in the program. Please post more code.

Here is the entire code (program is not complete though):

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include <ctime>
#include <cstdlib>
#include <string>


using namespace std;


int select;
int category;
static int wordchoice;


string line[];


string words_sport[11]={"LionelMessi","CristianoRonaldo","Pep","Drogba","Soccer","DavidVilla","Pique","Nemar","Cruyff","Mohammad","Mohamad"};
string movies[5]={"IronManThree","BatMan","SpiderMan","Avengers","GhostBusters"};


string brands[9]={"Nike","Sony","Microsoft","Toshiba","LG","Apple","Nokia","Nintendo","Sega"};




void lines(string a[],string line[]){
int counter=a[wordchoice].length();
for(int i=0;i<counter;i++){
//cout<<"_ ";
line[i]='_';
}
for(int i=0;i<counter;i++){
cout<<line[i]<<" ";
}
}


int main(){


cout<<"*************************Welcome to the Hang Man Game!*************************"<<endl;
cout<<"                              Type 1 to start                                   "<<endl;
cin>>select;
while(select!=1){
cout<<"Type 1 to start";
    cin>>select;
}


system("CLS");


cout<<"Please chose a category: 1-Sports, 2-Movies, 3-Brand Names"<<endl;
cin>>category;
while(category!=1 && category !=2 && category != 3){
cout<<"Please chose a category: 1-Sports, 2-Movies, 3-Brand Names"<<endl;
cin>>category;
}




system("CLS");


switch(category){
case 1:
cout<<"Sports selected, please guess the word you have 10 chances."<<endl;
wordchoice=rand()%10+0;
lines(words_sport,line);
break;
case 2:
cout<<"Movies selected, please guess the word you have 10 chances."<<endl;
wordchoice=rand()%4+0;
lines(movies,line);
break;
case 3:
cout<<"Brands selected, please guess the word you have 10 chances."<<endl;
wordchoice=rand()%8+0;
lines(brands,line);
break;
}






}
You should learn to indent =p
and you are using global variables which aren't good to use most the time. ps you are declaring line twice in your function. Once in the global scope then again in the local scope.
The standard layout for functions is like this btw:
1
2
3
4
5
6
7
8
9
returnType functionName( parameterType parameterName )
{
     stuff

     if( stuff )
     {
          stuff
     }
}
Last edited on
1. Change line 15 from string line[]; to string line;
2. Change line 27 from void lines(string a[],string line[]) to void lines(string a[],string line)
Program now should compile and run.

3. Change line 31 from line[i]='_'; to line += '_';
Program now should not crash

4. Follow giblits advice, and change line 27 from void lines(string a[],string line) to void lines(string a[]).
Now you won't be tearing your hair out trying to understand, that this function creates a local copy of global variable, and operates on it. Now all the changes are made on global variable.
Last edited on
To get it to work, all you really need to do is give your line[] array a size. Meaning change this string line[]; to this string line[50]; or something similar. You have to give your array a size when you declare it. While your at it you might as well make your line array local to main. With the way your code is set up, all you have to do is cut and paste string line[50]; into your main function. Your line array would probably be better as a char array instead of string. Scope shouldn't be a problem your compiler should be able to tell the differance between the global variable line, and the local variable line (its not good practice though). Local variables should take precedence over global. To access the global variable line from your lines function, you would need to use the scope resolution operator.
Last edited on
Topic archived. No new replies allowed.