error LNK2019; linking issues

I am a beginner programmer working on an assignment for school. At least most of my code is correct, but I can't get it to run, so I can't start testing things to see what is and isn't working. I'm sure there are some things wrong, but I'm not trying to have anyone do my homework, I would just really like to understand how to link properly and get it running.
I have to use a user made static library and link it to the project. I believe I have done this through project/properties/linker/input/additional dependencies, and adding C:\Users\Josh\Documents\Visual Studio 2010\Projects\studystaticlibrary\Debug\studystaticlibrary.lib
I also added all the project folders into project/properties/c++/general/additional include directories.
The header file shows up under external dependencies, and auto-complete works when using the class in the header.
However, I receive this error message about 10 times for all the class methods except the get methods:

1>Assignment 4.obj : error LNK2019: unresolved external symbol "public: void __thiscall StudentWeeklyActivity::Write(class std::basic_ostream<char,struct std::char_traits<char> > &)" (?Write@StudentWeeklyActivity@@QAEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _wmain
1>Assignment 4.obj : error LNK2019: unresolved external symbol "public: void __thiscall StudentWeeklyActivity::Read(class std::basic_istream<char,struct std::char_traits<char> > &)" (?Read@StudentWeeklyActivity@@QAEXAAV?$basic_istream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _wmain


and here is my code:

static library: StudentWeeklyActivity.h

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
#ifndef STUDENTWEEKLYACTIVITY_H
#define STUDENTWEEKLYACTIVITY_H
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


class StudentWeeklyActivity {
	string *FirstName;
	string LastName;
	int *credits, *sleeping, *working;
	int StudyHomework;
public:
	string GetFirstName() const;
	void SetFirstName(string first);
	string GetLastName() const;
	void SetLastName(string last);
	int GetCredits() const;
	void SetCredits(int cred);
	int GetSleeping() const;
	void SetSleeping(int sleep);
	int GetWorking() const;
	void SetWorking(int work);
	int GetStudyHomework() const;
	void SetStudyHomework(int study);
	StudentWeeklyActivity();
	~StudentWeeklyActivity();
	string GetFullName();
	void Read(istream &i);
	void Write(ostream& o);
	void Report(ostream& o);
};

#endif 


static library: StudentWeeklyActivity.cpp

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

string StudentWeeklyActivity::GetFirstName() const {
	return *FirstName;
}

void StudentWeeklyActivity::SetFirstName(string first){
	*FirstName=first;
}

string StudentWeeklyActivity::GetLastName() const {
	return LastName;
}

void StudentWeeklyActivity::SetLastName(string last) {
	LastName=last;
}

int StudentWeeklyActivity::GetCredits() const {
	return *credits;
}

void StudentWeeklyActivity::SetCredits(int cred) {
	*credits=cred;
}

int StudentWeeklyActivity::GetSleeping() const {
	return *sleeping;
}

void StudentWeeklyActivity::SetSleeping(int sleep) {
	*sleeping=sleep;
}

int StudentWeeklyActivity::GetWorking() const {
	return *working;
}

void StudentWeeklyActivity::SetWorking(int work){
	*working=work;
}

int StudentWeeklyActivity::GetStudyHomework() const {
	return StudyHomework;
}

void StudentWeeklyActivity::SetStudyHomework(int study) {
	StudyHomework=study;
}

StudentWeeklyActivity::StudentWeeklyActivity(){
	FirstName=new string;
	credits=new int;
	sleeping=new int;
	working=new int;
	*FirstName="";
	LastName="";
	*credits, *sleeping, *working, StudyHomework=0;
}

StudentWeeklyActivity::~StudentWeeklyActivity(){
	delete FirstName;
	delete credits;
	delete sleeping;
	delete working;
}

string StudentWeeklyActivity::GetFullName(){
	string fullname;
	fullname=GetFirstName() + " " + LastName;
	return fullname;
}

void StudentWeeklyActivity::Read(istream &i) {
	i>>*FirstName>>LastName>>*credits>>StudyHomework>>
	*sleeping>>*working;
}

void StudentWeeklyActivity::Write(ostream& o) {
	o<<*FirstName<<endl<<LastName<<endl<<*credits<<StudyHomework<<
	*sleeping<<*working;
}

void StudentWeeklyActivity::Report(ostream& o) {
	o<<"Student Activity Report"<<endl<<"-----------------------"<<endl;

	o<<endl<<"Name: "<<GetFullName()<<endl;
	o<<endl<<setw(12)<<"Activity"<<setw(20)<<"Hours"<<endl;
	o<<setw(12)<<"--------"<<setw(20)<<"-----"<<endl;
	o<<setw(12)<<"Credits"<<setw(20)<<*credits<<endl;
	o<<setw(12)<<"Studying"<<setw(20)<<StudyHomework<<endl;
	o<<setw(12)<<"Sleeping"<<setw(20)<<*sleeping<<endl;
	o<<setw(12)<<"Working"<<setw(20)<<*working<<endl;
	o<<setw(12)<<"Other"<<setw(20)<<168-*credits-*sleeping-*working-StudyHomework<<endl;
	o<<setw(12)<<"Total"<<setw(20)<<168<<endl<<endl;

	//Analysis

	o<<"Analysis"<<endl<<"--------"<<endl;

	int studydiff, sleepdiff, workdiff = 0;
	studydiff=*credits * 2 - StudyHomework;
	sleepdiff=56-*sleeping;
	workdiff=*working-20;

	o<<endl<<"Studying difference: "<<studydiff*-1<<endl;
	if (studydiff<=0)
		o<<"OK"<<endl;
	else if (1<=studydiff<=4)
		o<<"Slightly less than the optimal amount of studying/homework time being spent."<<endl;
	else if (5<=studydiff<=9)
		o<<"Significantly less than the optimal amount of studying/homework time being spent."<<endl;
	else if (10<=studydiff<=19)
		o<<"Not enough time being spent on studying/homework. Grades may be affected."<<endl;
	else if (studydiff>=20)
		o<<"Unsatisfactory. Grades will most likely be affected."<<endl;
	else
		o<<"Error producing analysis."<<endl;

	o<<endl<<"Sleeping Difference: "<<sleepdiff*-1<<endl;
	if (sleepdiff<=0)
		o<<"OK"<<endl;
	else if (1<=sleepdiff<=6)
		o<<"Slightly less than optimal."<<endl;
	else if (7<=sleepdiff<=14)
		o<<"Significantly less than optimal."<<endl;
	else if (sleepdiff>=14)
		o<<"Unsatisfactory. Student does not get enough sleep."<<endl;
	else
		o<<"Error producing analysis."<<endl;

	o<<endl<<"Working difference: "<<workdiff*-1<<endl;
	if (workdiff<=0)
		o<<"OK"<<endl;
	else if (1<=workdiff<=4)
		o<<"Slightly too much work."<<endl;
	else if (5<=workdiff<=9)
		o<<"This amount of work could affect grades."<<endl;
	else if (10<=workdiff<=19)
		o<<"Hard to do this amount of work and be a full-time student."<<endl;
	else if (workdiff>=20)
		o<<"Way too much work for a full-time student. Grades will most likely be affected."<<endl;
	else 
		o<<"Error prdocing analysis."<<endl;

}


sorry this is so long, I have to put the main cpp in another post, just over the limit.
Last edited on
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
[code]#include "stdafx.h"
#include "StudentWeeklyActivity.h"
#include <iostream>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	StudentWeeklyActivity *a;
	a=new StudentWeeklyActivity;
	string first, last, file, ofile;
	ofstream out;
	ifstream in;
	
	int choice, credits, sleep, work, study;
	
	do
	{
	cout<<"Student Time Analysis Program"<<endl;
	cout<<"1. Get Data From User and Show Report on Console"<<endl;
	cout<<"2. Get Data From User and Save Report to File"<<endl;
	cout<<"3. Get Data From file and Show Report on Console"<<endl;
	cout<<"4. get Data From File and Save Report to File"<<endl;
	cout<<"5. Generate Data File"<<endl;
	cout<<"6. Exit"<<endl;
	cin>>choice;
	}
	while (choice != 6);

	switch (choice) {
	case 1:
		cout<<"Enter first name: ";
		cin>>first;
		a->SetFirstName(first);
		cout<<endl<<"Enter last name: ";
		cin>>last;
		a->SetLastName(last);
		cout<<endl<<"Enter credits: ";
		cin>>credits;
		a->SetCredits(credits);
		cout<<endl<<"Enter time studying: ";
		cin>>study;
		a->SetStudyHomework(study);
		cout<<endl<<"Enter time sleeping: ";
		cin>>sleep;
		a->SetSleeping(sleep);
		cout<<endl<<"Enter time working: ";
		cin>>work;
		a->SetWorking(work);
		cout<<endl;
		a->Report(cout);
		break;
	case 2:
		cout<<"Enter first name: ";
		cin>>first;
		a->SetFirstName(first);
		cout<<endl<<"Enter last name: ";
		cin>>last;
		a->SetLastName(last);
		cout<<endl<<"Enter credits: ";
		cin>>credits;
		a->SetCredits(credits);
		cout<<endl<<"Enter time studying: ";
		cin>>study;
		a->SetStudyHomework(study);
		cout<<endl<<"Enter time sleeping: ";
		cin>>sleep;
		a->SetSleeping(sleep);
		cout<<endl<<"Enter time working: ";
		cin>>work;
		a->SetWorking(work);
		cout<<endl<<"What would you like to call the output file name? ";
		cin>>file;
		file=file+".txt";
		out.open(file);
		a->Report(out);
		out.close();
		break;
	case 3:
		cout<<"Enter input file name: ";
		cin>>file;
		file=file+".txt";
		in.open(file);
		a->Read(in);
		in.close();
		//Write report to standard output
		break;
	case 4:
		cout<<"Enter input file name: ";
		cin>>file;
		file=file+".txt";
		cout<<endl<<"Enter output file name: ";
		cin>>ofile;
		ofile=ofile+".txt";
		in.open(file);
		a->Read(in);
		in.close();
		out.open(ofile);
		a->Report(out);
		out.close();
	case 5:
		cout<<"Enter first name: ";
		cin>>first;
		a->SetFirstName(first);
		cout<<endl<<"Enter last name: ";
		cin>>last;
		a->SetLastName(last);
		cout<<endl<<"Enter credits: ";
		cin>>credits;
		a->SetCredits(credits);
		cout<<endl<<"Enter time studying: ";
		cin>>study;
		a->SetStudyHomework(study);
		cout<<endl<<"Enter time sleeping: ";
		cin>>sleep;
		a->SetSleeping(sleep);
		cout<<endl<<"Enter time working: ";
		cin>>work;
		a->SetWorking(work);
		cout<<endl<<"What would you like to call the output file name? ";
		cin>>file;
		file=file+".txt";
		out.open(file);
		a->Write(out);
		out.close();
		break;
	case 6:
		break;
	default:
		cout<<"Invalid choice, please try again."<<endl;
	}
		
	return 0;
}
[/code]
pity bump?
You never call any of your get methods, so it's not surprising that there is no linker error about them - the linker never goes looking for them.

The linker errors that you get mean the linker cannot find the compiled code of those functions. This means that you're not compiling them, or if you are, you're not telling the linker where to find them. The problem is not your code (except that you're trying to feed string objects to the iostream functions open and close, which do not take strings; they take char-pointers) - it is your IDE. Since you're trying to make a lib, you have to compile StudentWeeklyActivity.cpp as a library (i.e. a *.lib file) and then when you compile the main cpp separately, you have to tell the IDE where to find that *.lib file, and that you want to use it.

If you put all your code in one big cpp file, there would be no linker errors (that's a terrible idea, but it would prove that the problem is not your code - it's that your linker can't find the compiled StudentWeeklyActivity.cpp binary). You think you've done it - you clearly haven't.
Last edited on
Topic archived. No new replies allowed.