-1

I am having some trouble with the implementation of a constructor and I cannot figure out what is the problem. I have spent a lot of time with this problem: it would be grateful some kind of advice.

The main code is:

#include "stdafx.h"
#include "Rocket.h"

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
    Rocket* rocket;
    rocket=new Rocket();
    //LLA* position=rocket->positionLLA;
    return 0;
}

Rocket.h

#pragma once

#include "LLA.h"

class Rocket {
public: 
    Rocket(); // Default constructor
    LLA* positionLLA;
};

Rocket.cpp

#include "stdafx.h"

#include "Rocket.h"
#include <iostream>
using namespace std;

Rocket::Rocket() // Default constructor
{
     // Initialise the position
     positionLLA=new LLA();
}

The error says:

error LNK2019: unresolved external symbol "public: __thiscall Rocket::Rocket(void)" (??0Rocket@@QAE@XZ) referenced in function _main.

I know the error has something to do with not having declared a variable, but I think I have declared all classes and constructors.

PS: I am using Visual Studio 2008 in order to add dependencies.

David Duran
  • 1,786
  • 1
  • 25
  • 36
  • The error message means that it can't find the *definition* of the constructor. You *do* include the `Rocket.cpp` source file in the project? – Some programmer dude Jul 26 '14 at 18:32
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) You probably missed to add `Rocket.cpp` to your project correctly. – πάντα ῥεῖ Jul 26 '14 at 18:34
  • did you try to compile rocket.cpp by itself? what is your IDE? – Samer Jul 26 '14 at 18:35
  • I am using visual studio express 2010. What do you mean by including the rocket.cpp source file in the project. What I have is a project Main and then another project Rocket, where I have Rocket.cpp and LLA.cpp. The dependencies are correct. – David Duran Jul 26 '14 at 18:39
  • I have compiled rocket.cpp alone and it is fine – David Duran Jul 26 '14 at 18:40
  • _' What I have is a project Main and then another project Rocket'_ That might be your actual problem, that you have separate projects. Are you linking your main project against the library produced from the other one? – πάντα ῥεῖ Jul 26 '14 at 18:41
  • The error LNK2019 occurs due to a linking problem. The compiler could not link your Rocket header file with your Rocket.cpp file. It is not the constructor issue, your constructor is fine. This is how you define the header file. #ifndef _ROCKET_H_ #define _ROCKET_H #include #pragma once class Rocket{ public: //... code ... // }; endif now make sure that LLA.h is in the same location or folder with the Rocket.h and Rocket.cpp. Your main function needs to be define in your Rocket.cpp file, I hope that`s where you are calling Rocket class otherwise the main might not find its location. – Juniar Jul 26 '14 at 23:40

2 Answers2

3

I am assuming LLA is correctly defined in your .h file

Are you compiling Rocket.cpp into Rocket.o and main.cpp into main.o and then linking the two object files together?

Your error seems to imply that the linker cannot obtain symbol information from Rocket.o, which usually means Rocket.o was not found

Oh, as a detail, since Rocket is using an LLA*, you don't need to include LLA.h in Rocket.h, you can simply forward-declare

class LLA;
Enrico Granata
  • 3,303
  • 18
  • 25
  • By Rocket.o, you are referring to Rocket.obj. Because I don't see any file with .o extension. If you are referring to the object, in my directory, I see that main.obj and rocket.obj are inside a Debug folder, which is at the same level as the Main.cpp and Rocket.cpp respectively. This seems to be the same as other projects I have made and they work. – David Duran Jul 26 '14 at 19:18
  • Yes .obj (sorry, Unix junkie here) Well, since all these files exist, you need to ensure that the linker command line uses all of them as arguments. Not sure how to do it with Visual Studio (Xcode has a nice UI that lets you see the steps of the build process, command-line arguments and all). Find your linker invocation and ensure *ALL* relevant .obj files are passed to it. If not, there's your problem. – Enrico Granata Jul 26 '14 at 19:26
  • You are right on the reason.I explain, I have been programming a lot of time with VS2008 and to add a static library to an exe, I was only supposed to add a dependency in "Project Dependencies". Since in VS2010, the same button exists, I guessed that the same procedure was applicable. But that's not the case. So to solve the problem I have had to add a reference (in Framework and references) from the main to the library Rocket. – David Duran Mar 06 '17 at 09:44
-2

You might want to add #ifndef, #define, and #endif to your header files. Having multiple includes of the same thing in different files can lead to complications.

Sagar V
  • 12,158
  • 7
  • 41
  • 68
Dacer
  • 7
  • 2