0

I am struggling with

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Book::Book(void)" (??0Book@@QEAA@XZ) referenced in function main

main.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl Book::update(class Order)" (?update@Book@@QEAAXVOrder@@@Z) referenced in function main

From what I have read, it usually is because of missing selectors, but I can't see myself missing anything. Also, no static variables, parameters seem correct, ... going through the issue list below didn't help figure out the issue.

https://msdn.microsoft.com/en-us/library/799kze2z.aspx

#include <QCoreApplication>
#include <order.h>
#include <book.h>
#include <iostream>
#include <vector>

std::vector<Order> md_orders;


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

    Order order;
    order.setIsBid( true );
    order.setSize( 500 );
    order.setPrice( 10.00 );

    double price = order.getPrice();

    Book book;
    book.update( order );

    std::cout << price;


    return a.exec();
}

book.h

#ifndef BOOK_H
#define BOOK_H
//#include <order.h>
#include <vector>

class Book
{
public:
    Book();
    Order getBest( double, bool );
    void update( Order );
    double getBestPrice( bool );
private:
    std::vector<Order> _orders;
};

#endif // BOOK_H

book.cpp

#include "book.h"
#include <limits>
//#include <order.h>

#include <iostream>

int imax = std::numeric_limits<int>::max();

Book::Book()
{

}



void Book::update( Order order ) {

    std::cout << order.getSize();

}

I am new to cpp. I seem to be doing the same with the book class as with the order class, where it works fine. This is very confusing. Any pointers?

So, out of curiosity, I created a new projected and added a class TestA and run it, all went fine. Then I created class TestB exactly the same way and it breaks with the same error for TestB, but not TestA

#include <QCoreApplication>
#include "testa.h"
#include "testb.h"

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


    TestA A;
    TestB B;

    return a.exec();
}

testa.h

#ifndef TESTA_H
#define TESTA_H


class TestA
{
public:
    TestA();
};

#endif // TESTA_H

testb.h

#ifndef TESTB_H
#define TESTB_H


class TestB
{
public:
    TestB();
};

#endif // TESTB_H

throws this

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl TestB::TestB(void)" (??0TestB@@QEAA@XZ) referenced in function main

That is all on qt creator. It just seems very weird behaviour

chrise
  • 4,039
  • 3
  • 39
  • 74
  • 1
    You need to compile and link *both* `.cpp` files, something like: `cl main.cpp book.cpp`. – Jerry Coffin Sep 22 '16 at 05:21
  • 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) – Richard Critten Sep 22 '16 at 05:29
  • Hi Jerry, I don't understand what you mean. Could you please elaborate a bit on this? – chrise Sep 22 '16 at 05:32
  • Oh, I think I see what you mean. I don't compile from command line. I am using qt creator. Files are in the same project – chrise Sep 22 '16 at 05:40

2 Answers2

1

In your main.cpp,

Notice how you have include book.h Compiler will not look for book.h in local directory and couldn't find definition of book in standard lib location.

Instead of #include <book.h> use #include"book.h"

Adding further.

You haven't defined below method in book.cpp and from main function you are trying to access them. Reason for all your pain.

Order getBest( double, bool );
double getBestPrice( bool );
HadeS
  • 2,020
  • 19
  • 36
  • changed to #include "book.h". but did not fix the issue. but I learned what the difference is. thanks for that – chrise Sep 22 '16 at 05:38
0

You should do the following (provided you run compiler and linker manually):

  1. Compile main.cpp
  2. Compile book.cpp
  3. Link both files into a single executable

All of above can be done in one command if your compiler allows it.

Since I'm not familiar with MSVC compiler, I'll show how I would do this with gcc:

gcc -c -o main.o main.cpp # Compiles main.cpp into main.o
gcc -c -o book.o book.cpp # Compiles book.cpp into book.o
gcc -o my_app main.o book.o # Creates the my_app executable from main.o and book.o

or

gcc -o my_app main.cpp book.cpp # Does all of above in one command
GMichael
  • 2,726
  • 1
  • 20
  • 30