Program doesnt run. Need help with constructors!

closed account (Syv9216C)
Not sure why my code doesn't run. In main, student a is supposed to call the default constructor, student b should call the constructor with 1 parameter and pass in 4, student c should call the constructor with 3 parameters and pass in "Joe", 40, and 5. Then I'm asked to call the necessary set functions and call the display function for each of the student objects at the end.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  #include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:

    //Delcare variables
     string name;
    int id;
    int *testptr;
    int num;

    //Member functions
    void makeArray()
    {
        testptr = new int[num];  // Allocate num ints and save ptr in testptr.
        
         for (int i=0; i<num; i++) 
        {
        testptr[i] = 0;    // Initialize all elements to zero.
        }
    }

public:

    //Member Functions
    Student()
    {
        setName();
         setID();
        num = 3;
        makeArray();
    }

    Student(int n)
    {
        setName();
        setID();
        if (n > 0)
        {
            num = n;
        }
         else
        {
            num = 3;
        }
        makeArray();
    }

    Student(string nm, int i, int n)
    {
        setName();
        setID();
        if (n > 0)
        {
             num = n;
        }
        else
        {
            num = 3;
        }
        makeArray();
    }

    void setName(string nm)
    {
        name = nm;
    }

    void setID(int i)
     {
        if (i >=10 && i <=99)
        {
            id = i;
        }
        else
        {
            id = 10;
            cout << "Error. Can't set " << getName() << "'s id to i." << endl;
         }
    }

    void setScore(int i, int s)
    {

    }

    string getName() const
    {
        return name;
    }

    int getID() const
    {
        return id;
    }

    void showScore()
    {
        for (int i=0; i<num; i++) 
        {
        cout << "The test number is " << testptr[i] << ", the test score is " << s << endl;    //Displays the test number and the score
         }
    }

    void display()
    {
        getName();
        getID();
        showScore();

        for (int i=0; i<num; i++) 
        {
        cout << "The Name: " << name << endl;    
         cout << "The ID: " << id << endl;
        cout << "Test " << testptr[i] << " had a score of " << s << endl; 
        }
    }

    ~Student() 
    {
        delete[]testptr;
    }
};

int main()
{
    Student studentA, studentB, studentC;

    studentA.Student();
	studentB.Student(int x = 4);
	studentC.Student(string Joe, int y = 40, int z = 5);

	studentA.setScore();
	studentB.setScore();
	studentC.setScore();


	studentA.display();
	studentB.display();
	studentC.display();

	system("pause");
	return 0;
}


Appreciate any contributions!
This just isn't going to work.
1
2
3
studentA.Student();
studentB.Student(int x = 4);
studentC.Student(string Joe, int y = 40, int z = 5);


Try:

1
2
3
Student studentA;
Student studentB(4);
Student studentC("Joe", 40, 5);


http://www.cplusplus.com/doc/tutorial/classes/

After that you will see you are making calls to functions that require variables to be passed into them to work.

setName();

Isn't going to work because setName gets a string.

1
2
3
4
void setName(string nm)
    {
        name = nm;
    }


Got a few functions calls with similar errors. And what is s in line 106 and 120? I assume you want to print out a score but it is an uninitialized variable.
Last edited on
closed account (Syv9216C)
Updated my code. Still getting the same error.
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\users\louis\documents\visual studio 2010\Projects\Lab8\Debug\Lab8.exe : fatal error LNK1120: 1 unresolved externals

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:

    //Delcare variables
     string name;
    int id;
    int *testptr;
    int num;

    //Member functions
    void makeArray()
    {
        testptr = new int[num];  // Allocate num ints and save ptr in testptr.
        
         for (int i=0; i<num; i++) 
        {
        testptr[i] = 0;    // Initialize all elements to zero.
        }
    }

public:

    //Member Functions
    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)
    {
        setName(nm);
        setID(i);
        if (n > 0)
        {
             num = n;
        }
        else
        {
            num = 3;
        }
        makeArray();
    }

    void setName(string nm)
    {
        name = nm;
    }

    void setID(int i)
     {
        if (i >=10 && i <=99)
        {
            id = i;
        }
        else
        {
            id = 10;
            cout << "Error. Can't set " << getName() << "'s id to i." << endl;
         }
    }

    void setScore(int i, int s)
    {

		if(i < num) //STUDENT NUM
		{
		if(s >= 0 && s <= 100)
		{
			test[i] = s;
		}
		else
		{
			cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
		}
		}
		else
		{
		cout << "Invalid.  Can not set test " << i << " to " << s << " for " << getName() << endl;
		}

    }

    string getName() const
    {
        return name;
    }

    int getID() const
    {
        return id;
    }

    void showScore()
    {
        for (int i=0; i<num; i++) 
        {
        cout << "Test " << i << " had a score of " << testptr[i] << endl;    //Displays the test number and the score
        }
    }

    void display()
    {
        cout << "The Name: " << getName();
		cout << "The ID: " << getID();
		showScore();
		cout << endl;
		cout << endl;
    }

    ~Student() 
    {
        delete[]testptr;
    }
};

int main()
{
    Student studentA;
	Student studentB(4);
	Student studentC("Joe", 40, 5);


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


	studentB.setName("John");
	studentB.setID(30);
	studentB.setScore(0, 70);
	studentB.setScore(1, 80);
	studentB.setScore(2, 90);
	studentB.setScore(3, 100);

	studentC.setScore(0, 90);
	studentC.setScore(1, 91);
	studentC.setScore(2, 92);
	studentC.setScore(3, 93);
	studentC.setScore(4, 94);
	studentC.setScore(5, 95);
	studentC.setScore(4, 105);
	studentC.setScore(5, 105);


	studentA.display();
	studentB.display();
	studentC.display();

	system("pause");
	return 0;
}


Usually link errors have to do with the IDE rather than your code. Do you have a console window open from a previous run attempt?
closed account (Syv9216C)
No I don't. Not sure whats wrong.
closed account (Syv9216C)
My code runs, thanks to all who helped.
Usually link errors have to do with the IDE rather than your code. Do you have a console window open from a previous run attempt?


Eh??
^ it's IDE-specific. Stuff like Eclipse and Xcode don't have console windows pop up by default, but VS and CodeLite do. Looks like OP is using VS, and that happens to me all the time.
Last edited on
I suggest you read about linkers.
Topic archived. No new replies allowed.