Thread: Linker errors with Visual C++

  1. #1
    Registered User codegirl's Avatar
    Join Date
    Jun 2003

    Linker errors with Visual C++

    I am using Visual C++ .NET 2003 and included some header files that I wrote for an earlier project. I will be using these header files in several future projects, so instead of adding a copy to each project, I added the path of the header files to the "Additional Include Directories" under Project Properties (C/C++ | General).

    When I try to use the classes in the header files, for example one class is called MyVector, I get the following error:

    nofb error LNK2019: unresolved external symbol "public: __thiscall MyVector::~MyVector(void)" (??1MyVector@@QAE@XZ) referenced in function "public: __thiscall Scene::Scene(void)" (??0Scene@@QAE@XZ)

    But I noticed that this error only happens for functions that are defined in MyVector.cpp and not for functions that are defined in MyVector.h (such as simple "set" and "get" functions). I've spent all night playing with the settings and searching MSDN -- does anyone have any suggestions? Thanks!!

    Here is an excerpt of my code if that helps...

    // ====== Scene.h======
    #ifndef SCENE_H_
    #define SCENE_H_
    #include <MyVector.h>
    class Scene {
    	MyVector *points;
    	int numPoints;
    // ====== Scene.cpp ======
    #include "Scene.h"
    Scene::Scene() {
    	numPoints = 14 * 3;
    	// The following functions yield linker errors (LNK2019)
    	points = new MyVector[numPoints];
    	// The following functions do not yield linker errors
    	points[0].setElement(0,2.0f);  // test build errors
    	float i = points[0].getElement(0);
    	int j = points[0].getLength();
    } // end default constructor
    Scene::~Scene() {
    	delete [] points;  // Yields linker errors
    } // end deconstructor
    // ====== MyVector.h ======
    #ifndef MYVECTOR_H_
    #define MYVECTOR_H_
    class MyVector {
    	MyVector();    // Implemented in MyVector.cpp
    	~MyVector();  // Implemented in MyVector.cpp
    	void normalize(); // Implemented in MyVector.cpp
    	int getLength() { return length; }
    	float getElement(int i) { return v[i]; }
    	void setElement (int i, float c) { v[i] = c; }
    	float *v;
    	int length;
    My programs don't have bugs, they just develop random features.

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    You must first add the file to your project through the project menu -> add existing item. Then the compiler can compile the .CPP file and generate the .obj file the linker needs.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    You have not shown us what is in MyVector.Cpp... Are you certain you have the MyVector destructor function coded there? You have told the compiler (in the MyVector.H file) that you are making your own destructor for the MyVector class. If you do not have this function defined in MyVector.Cpp then the class MyVector cannot be completely built and could be the cause of this error.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Yes, I have defined all the other functions (destructor, constructor) in MyVector.cpp. I've used MyVector in another project where the files were included in the project as velius described, but I thought there was a way to do it without adding all the files to the project. (Or else some of my projects are going to have a ton of files in them!)

    I didn't think that the contents of MyVector.cpp were relavent to the question, but here is a portion of the class:

    #include <cmath>
    using std::sqrt;
    #include "MyVector.h"
    #include "VectorErr.h"
    MyVector::MyVector() {
    	length = 3;
    	v = new float[length];
    	for (int i = 0; i < length; i++) {
    		v[i] = 0;
    	} // end for
    } // end default constructor
    MyVector::~MyVector() {
      delete [] v;
    } // end deconstructor
    void MyVector::normalize() {
    	MyVector *original = new MyVector(length);
    	(*original) = (*this);
    	float denom = 0;
    	for (int i = 0; i < length; i++) {
    		for (int j = 0; j < length; j++) {
    			denom += original->getElement(j) * original->getElement(j);
    		} // end for
    		v[i] = original->getElement(i) / sqrt(denom);
    		denom = 0;
    	} // end for
      delete original;
    } // end normalize
    My programs don't have bugs, they just develop random features.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    I think normally you would just add the .cpp files to the project like velius said. However, if you didn't want to add them and the class doesn't change (much), then you can just compile it once in its own project and link to it from this other project. For example, I have some classes that I use in multiple places with multiple projects. Those classes are compiled into their own lib, and that lib is linked to by the other projects. That way if I ever do make changes to the .cpp files for the original classes, I only have to change them in one place, compile them all once, and re-link all the other projects that use them.

    I hope this is what you are looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Functions in C
    By shoobsie in forum C Programming
    Replies: 15
    Last Post: 11-17-2005, 01:47 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. why am i getting linker errors?
    By dantestwin in forum C++ Programming
    Replies: 11
    Last Post: 07-08-2004, 10:34 AM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM