Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jkeppens

LNK2001 error

This topic is 5758 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I extended the STL vector class as followed : CVector.h
    
#ifndef _CVector_h_

#define _CVector_h_

 
#include <vector> 

#include <algorithm> 

using namespace std;
 
 

template <class T>

class CVector : public vector<T> {
 

public:

	void add(T t) {
		iterator pos = find(begin(), end(), t);
		if (pos != end()) {
			push_back(t);
		}
	}

	void remove(T t) {
		iterator pos = find(begin(), end(), t);
		if (pos != end()) {
			erase(pos);;
		}
	}

};

#endif
  
I have the following test program : test.cpp
  
#include <iostream> 

#include <string>

using namespace std;


#include "CVector.h"
 
class Test {

public:

	Test(string name) { _name = name; };
	string getName() { return _name; }

private:

	string _name;

};

int main(int argc, char* argv[]) {

	Test t1("t1");
	Test t2("t2");
	Test t3("t3");

	CVector<Test> v;

	v.add(t1);
	v.add(t2);
	v.add(t3);
	
	CVector<Test>::iterator i;
	for (i = v.begin(); i != v.end(); i++) {
		Test temp = (Test)*i;
		cout << temp.getName() << endl;
	}

	v.remove(t1);
	v.remove(t2);
	v.remove(t3);

	cin.get();
	return 0;
}
      
I get following errors : test.obj : error LNK2001: unresolved external symbol "public: void __thiscall CVector::remove(class Test)" (?remove@?$CVector@VTest@@@@QAEXVTest@@@Z) test.obj : error LNK2001: unresolved external symbol "public: void __thiscall CVector::add(class Test)" (?add@?$CVector@VTest@@@@QAEXVTest@@@Z) Anyone see the problem? Jeroen [edited by - jkeppens on September 15, 2002 12:19:03 PM] [edited by - jkeppens on September 15, 2002 12:19:52 PM]

Share this post


Link to post
Share on other sites
Advertisement
Are you sure that''s the code that you are compiling?
Test needs a
bool operator==(const Test& rhs)
overload, and then it ought to compile & link fine. remove & add shouldn''t be unresolved.

However that code has other "issues".

Share this post


Link to post
Share on other sites
It''s not a code problem (apart from the operator== override thing which will crop up one you solve this first problem). You haven''t told your linker where the object module for your compiled CVector is, it''s not enough just to include cvector.h

Make sure both source files are part of the same project/makefile.
Or, Assuming you are compiling cvector.cpp and test.cpp separately, when you make your test program tell your linker where cvector.obj is (use ''add to project'' in MSVC)

Share this post


Link to post
Share on other sites
I found the error... stupid me.

I had 2 projects in my workspace and both had a "CVector.h" file. I was editing the file in the unactive project, which offcourse had no effect in the active project which had an old version of "CVector.h".

I got a list of errors afterwards, due to the missing < & == overloaded operators.

Thanks for the feedback!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!