6

[Solved]This problem somehow resolved itself on about the 5-6 clean and rebuild, no code was changed

I have a class with a default constructor, and a constructor that takes 8 arguments.

from another class i am trying to call the constructor and pass 8 parameters however when i try to do this i am getting a LNK2019 Error.

The thing thats confusing me though is if i call the default constructor nothing the program compiles and runs fine... everything has the correct includes and must be working because i can call the default constructor, i am using QStrings as some of the arguments but QString is included so it cant be that... any other reason anyone knows of why i would geta LNK2019 error for a constructor taking arguments and not when its the default one??

Car.h

#include <QString>
class car
{
public:

    car();
    car(int car_id, QString something, QString something_else, QString something3, int an_int, int another_int, int another_int_i, QString something4);
};

car.cpp

car::car()
{
}

car::car(int car_id, QString something, QString something_else, QString something3, int an_int, int another_int, int another_int_i, QString something4)
{
}

obviously i have just removed context and values etc but makes no difference on structure

DatabaseController.cpp

#include "DatabaseController.h"
#include "car.h"
void DatabaseController::DoSomething()
{
    car *pcar = new car(0, "", "", "", 0, 0, 0, "");
}

interface.cpp

#include "DatabaseController.h"
void interface::on_btn_clicked()
{
    DatabaseController DC;
    DC.DoSomething();
}

FULL ERROR:

DatabaseController.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall car::car(int,class QString,class QString,class QString,int,int,int,class QString)" (??0car@@QAE@HVQString@@00HHH0@Z) referenced in function "public: void __thiscall DatabaseController::getAll(class QString)" (?getAll@DatabaseController@@QAEXVQString@@@Z)
Community
  • 1
  • 1
AngryDuck
  • 4,358
  • 13
  • 57
  • 91
  • 4
    Show your code please (and keep it reasonably short): [SSCCE](http://sscce.org/). **EDIT** And the full error. – BoBTFish Apr 12 '13 at 10:55
  • 1
    ok give me a minute, im just trying to get a cut down version that includes all necessary parts still – AngryDuck Apr 12 '13 at 10:57
  • 4
    linking error has nothing to do with include, even if you include QString, you may get a link error if the library for QString is not specified. – Min Lin Apr 12 '13 at 10:57
  • 1
    Can you show us your `.pro` file? – SpongeBobFan Apr 12 '13 at 11:03
  • i could but theres really nothing to see, sources and headers with all of my files included. also nothing is declared and not used iv checked that. the program compiles fine if i comment out the line calling the constructor or just call the default constructor so it has to be something to do with that – AngryDuck Apr 12 '13 at 11:07
  • sorry someone just edited my post and removed the error ill post it again – AngryDuck Apr 12 '13 at 11:09
  • 1
    have you defined car()? – g19fanatic Apr 12 '13 at 11:12
  • I think, you've missed an implementation of your non-default constructor. Maybe `.cpp` file with it is not included to project in `.pro` file, or you're just misstyped it's header. – SpongeBobFan Apr 12 '13 at 11:15
  • Are you sure QString is declared before you call the constructor? Are you sure that the big constructor is in the same .cpp file with the default, or that it is in a .cpp with is compiled as a separated file in your project? – qPCR4vir Apr 12 '13 at 11:21
  • 1
    If you cannot find the answer by yourself, please provide minimal project with `.pro`, `main.cpp` that just use your class' constructor, and your class' `.cpp` and `.h` files (all trimmed to just reproduse the problem). – SpongeBobFan Apr 12 '13 at 11:23
  • @g19fanatic yea i have, i show it in the code above – AngryDuck Apr 12 '13 at 11:51
  • ill sort out code sample now give me a minute – AngryDuck Apr 12 '13 at 11:51
  • there you go code is up now – AngryDuck Apr 12 '13 at 12:00
  • anyone still even looking at this thread? cant help but feel its died and only after i put up code that could help someone find an answer because for the life of me i cant see anything wrong with this – AngryDuck Apr 12 '13 at 12:16
  • Try adding two dummy constructors, one taking just `int`, one taking just `QString`, then calling these instead of the 8-param one. Does the error happen in both cases? – Angew is no longer proud of SO Apr 12 '13 at 12:36
  • ok................its just started working, i have changed no code at all, i did a clean build and ran and it worked (note that id cleaned and rebuilt this about 10 times and had it still not work) – AngryDuck Apr 12 '13 at 12:41
  • @AngryDuck One option would be to edit the question to append that it's sorted itself out, and flag it for closing. – Angew is no longer proud of SO Apr 12 '13 at 12:49
  • ok thanks for the advice ill do that – AngryDuck Apr 12 '13 at 12:54

3 Answers3

11

I know this is a really late response but I struggled for a long time with the same issue.

QT doesn't always parse the c++ files correctly when doing a clean->rebuild. Luckily to force it to do so just manually delete the build files and it will run from scratch.

It worked for me and I hope it helps a few people!

user2448431
  • 185
  • 2
  • 15
2

Have you tried to rebuild the project? In some circumstance this really can produce linkage errors similar like you described.

Okay, could you please copy paste the same code as you have in your project? :)

car *car = new car(0, "", "", "", 0, 0, 0, "");

Here, for example, you can't name instance of "car" with the same name. You should use another identifier, for example:

car *pCar = new car(0, "", "", "", 0, 0, 0, "");
Max Go
  • 2,092
  • 1
  • 16
  • 26
  • ok dont worry about trying to disect code now thanks all the same and, this was a mistake in putting it on here that wasnt in my code "Car" was a trivial example my code has nothing to do with cars etc but it was for showing my code without context and that error was only here i know not to name instances the same as the class – AngryDuck Apr 12 '13 at 12:58
  • anyway turns out you were right, well kind of at least more so than anyone else, sorted itself out after a clean and rebuild (which makes no sense seeing as how id done that a few times to no effect anyway ill accept answer) – AngryDuck Apr 12 '13 at 12:59
  • from the side, issue looks like you had a lot of different params in constructor and added them incrementally, step by step compile the code. From my experience, some compilers don't like such changes and can drive you wild :) Interesting, what compiler are you using. – Max Go Apr 12 '13 at 13:06
1

I was facing the same issue and none of the suggested answers worked for me. Eventually running qmake did the trick for me.

  1. Right click project name
  2. Run qmake
  3. Clean
  4. Rebuild

I thought this might help someone who's facing similar issue.

medasumanth
  • 381
  • 4
  • 10