error LNK2018: unresolved external symbol (referring to constructors)

This is the first time I have used classes in C++. This program is fairly simple, I am to create a Student class with 4 instance variables. Then create a default and non-default constructor as well as a member function to display the instance variables. In the main body of my program I declare two objects using the two different constructors, then call the member function in order to display the data. There is something wrong, and as I am a beginner I cannot figure it out for the life of me. I receive two LNK2019: unresolved external symbol errors that are referring to both of my constructors being referenced in my main function. Any help would be greatly appreciated.

Student.h file

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
// class for college student
class Student
{

public:

	// instance/member variables
	std::string studentName;
	int phoneNumber;
	int studentNumber;
	double currentGPA;

	// member function to display instance variables
	void displayValues();

public:
	
	// default constructor that assigns deault values to instance variables
	Student() : studentName ("Laura"), phoneNumber (6514923845), studentNumber (0002), currentGPA (3.8) {}
	
	// non default constructor that takes 4 variables as parameters and sets to instance variables
	Student(std::string studentName, int phoneNumber, int studentNumber, double currentGPA);
	
	// default deconstrucor
	~Student();
};


Student.cpp file

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
#include "Student.h"
#include <iostream>
#include <string>

using namespace std;

// main function
int main()
{

	// declare default object
	Student student_one;

	// declare non default object
	Student student_two("Numen", 9523937467, 0002, 3.5);

	// call displayValue()
	student_one.Student::displayValues();

	// again call displayValue()
	student_two.Student::displayValues();

}

// member function to display instance variables
void Student::displayValues()
{
	cout << studentName << endl;
	cout << phoneNumber << endl;
	cout << studentNumber << endl;
	cout << currentGPA << endl;
}
LNK2018: unresolved external symbol often means you are calling a function that has not been defined.

non-default constructor declaration at line 22 of student.h, does not have any definition.

// non default constructor that takes 4 variables as parameters and sets to instance variables
Student(std::string studentName, int phoneNumber, int studentNumber, double currentGPA);


No implementation here. You could try adding {} but that would be weird, since you'll most likely want to take some operations here.


~Student();

here too. You can actually get rid of this if you wont do anything before the object(in this case the class) destroys.

Hope this helps.
Last edited on
Hi,

It might help a little, if I mention to name the header & .cpp files the same as the class. That is Student.h and Student.cpp. Student.cpp has the implementation code for the class functions. Name the file with main in it something else.

:+)
^The man was right...hahaha..I didnt even take a good look at your filenames.. -_-
Got it! Thank you much!
Topic archived. No new replies allowed.