Struggling with dynamically allocating an array within a class

I am attempting to create three different students using three different constructors and getting a error codes LNK2019 and LNK 1120.
It may have something to do with me attempting to dynamically allocate an array to hold test scores.
The only functions I have only shown the main function as well as the functions involving the dynamically allocated array
Any help would be appreciated!

1
2
3
4
5
6
7
8
  void makeArray()//Private class member that creates dynamically allocated array
		{
			test = new int[num];
			for(int count = 0; count < num; count++)
			{
				test[count] = 0;
			}
		}

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
//Constructors, public class members
		Student()
		{
			setName("None");
			setID(10);
			num = 3;
			makeArray();
		}
		Student(int n)
		{
			setName("None");
			setID(10);
			if(n > 0)
			{
				num = n;
			}
			else
			{
				num=3;
			}
			makeArray();
		}		
		Student(string nm, int i, int n)
		{
			nm = "None";
			setName(nm);
			i = 10;
			setID(i);
			if(n>0)
			{
				num = n;
			}
			else
			{
				num = 3;
			}
			makeArray();
		}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//public class member functions that use the array
		void setScore(int i, int s)
		{
			if(0 <= i <= num && 0<=s<=100)
			{
				test[i] = s;
			}
			else
			{
				cout << "Cannot set test " << i+1 << 
"to " << s <<" for " <<getName() << "\n";
			}
		}
void showScore()
		{
			for(int count = 0; count < num; count++)
			{
				cout << "Test " << count+1 << " had a score of " << test[count] << "\n";
			}
		}

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
int main() //main function
{
	Student A();
	Student B(4);
	Student C("Joe", 40, 5);

	A().setName("Tom");
	A().setID(200);
	A().setID(20);
	A().setScore(0, 75);
	A().setScore(1, 85);
	A().setScore(2, 95);

	B.setName("John");
	B.setID(30);
	B.setScore(0, 70);
	B.setScore(1, 80);
	B.setScore(2, 90);
	B.setScore(3, 100);
	
	C.setScore(0, 90);
	C.setScore(1, 91);
	C.setScore(2, 92);
	C.setScore(3, 93);
	C.setScore(4, 94);
	C.setScore(5, 95);
	C.setScore(4, 105);
	C.setScore(-5, -105);

	A().display();
	B.display();
	C.display();
	system("Pause");
	return 0;
}

Sorry for the wall of code!
The linker error numbers are not helpful. Please copy and paste the exact error message that you are getting.

Usually linker errors are because you defined something 0 times (e.g. forgot to define it) or more than 1 times (e.g. two or more definitions, even if they are the same). Everything in a program must be defined exactly once.
error LNK2019: unresolved external symbol "class Student __cdecl A(void)" (?A@@YA?AVStudent@@XZ) referenced in function _main

and

error LNK1120: 1 unresolved externals

I also am getting 3 warnings

Edit:
The three warnings are about the use of <=,
I'm not sure if they are actually important
Last edited on
Aha, I see it now. On line 3 of your main function snippet, you have this:

Student A();

This is a common mistake that goes unnoticed due to something called Most Vexing Parse. What happens is, this line ends up declaring a function named A which takes no parameters and returns a Student by value. To fix it, remove the parenthesis:

Student A;

Lines 7-12 looks like you were getting an error and you decided to fix it by not thinking about why you weren't getting errors for B and C.
Last edited on
Thanks! I appreciate the help, I've got it at least running.
Now my problem is getting my error messages, such as the one in the function setScore, to work properly.
if(0 <= i <= num && 0<=s<=100)

You cannot write conditions that way. You have to do this instead:

if(0 <= i && i <= num && 0 <= s && s <= 100)
Last edited on
Topic archived. No new replies allowed.