Linker error

Sep 18, 2008 at 2:21pm
I must be making a silly beginner error but I don't what it is.
The link error is:

LNK2019: unresolved external symbol "public: __thiscall collection::collection(char const * const,char const * const,enum category)" (??0collection@@QAE@QBD0W4category@@@Z) referenced in function _main

When I put the main function on Collection.cpp file it just works.

The code is:

Collection.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
#ifndef COLLECTION_H
#define COLLECTION_H

#include <iostream>
#include <ctime>

enum source {
		file,
		internet
	};
	enum category {
		learning,
		music,
		software
	};

class collection {
public:
	collection(char const name[],char const code[],category cat);
	void printout();
private:
	char const *_name;
	char const *_code;
	unsigned int _Nrecords;
	source _source;
	category _category;
	time_t _creationDate;
	time_t _modificationDate;
	time_t _onSystemDate;
};

#endif 


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

inline collection::collection(char const name[],char const code[],category cat) {
	_name = &name[0];
	_code = &code[0];
	_Nrecords = 0;
	_source = file;
	_category = cat;

	time_t seconds;
	seconds = time(NULL);

	_creationDate = seconds;
	_modificationDate = seconds;
	_onSystemDate = seconds;
	
}

void collection::printout() {

	std::cout << "Time: " << _onSystemDate << "Source: " << _source << "Category: " << _category;
	std::cout << "name: " << _name << "code: " << _code;
}


User.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef USER_H
#define USER_H

#include "Collection.h"

class user {
public:
	user() {}
	void seteditablecollection(category cat,collection *col);
	void addactivecollection(collection *col);
private:
	collection *_editableCollection[3];
	// To change collection to list of collections
	collection *_activeCollections[3];
};

#endif USER_H 



User.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Collection.h"
#include "User.h"

inline void user::seteditablecollection(category cat, collection* col) {
	_editableCollection[cat] = col;
}


// Alter later with list
inline void user::addactivecollection(collection *col) {
	_activeCollections[0] = col;
}


int main() {
	user u;
	category cat = learning;
	collection c1("John","FH13",cat);
	//u.seteditablecollection(learning,&c1);
}
Sep 18, 2008 at 5:29pm
The problem might have something to do with it being an inline function...I don't use them much, but is there is a specific reason why you are using it? Couldn't you just make it a normal function?
Sep 18, 2008 at 7:12pm
First, do you really need those functions inlined? Those you know the pros and cons of inlining? Second, the keyword inline is only a hint to the compiler - there is no guarantee that your function will get inlined.

And finally your problem is due to the inline c'tor of Collection class. When you define an inline function you must implement it in the header file. This also means the User class's member functions will cause the same problem when you call them.

See this article for more info: http://msdn.microsoft.com/en-us/library/78t98006.aspx

Berk Celebisoy
Software Developer - Microsoft Corp.
Last edited on Sep 18, 2008 at 9:06pm
Sep 19, 2008 at 9:10am
Thanks a lot.
I'll see the article too.
Topic archived. No new replies allowed.